Передача параметров обработчику событий - C#
Формулировка задачи:
при запуске программы создаётся массив пикчербоксов и заполняется свойствами. там же создаётся обработчик событий для каждого пикчербокса, но один на всех, а нужно чтобы разные.
вот нужно чтобы вместо красной единички ObjectDialog(myObj[1]) передавался индекс i. насколько я знаю, через параметры обработчика нельзя передать.
MyObject[] myObj = new MyObject[50];
ObjectState[] objState = new ObjectState[30];
PictureBox[] IndicatorImage = new PictureBox[50];
private void LoadObjects()
{
IniFile ini = new IniFile(Application.StartupPath.ToString() + "\\objects.cfg");
Int32 count = ini.GetInt32("EventCount", "Count", 10);
for (int i = 0; i < count; i++)
{
myObj[i] = new MyObject();
IndicatorImage[i] = new PictureBox();
myObj[i].objectState = objState[0];
myObj[i].Name = ini.GetString(i.ToString(), "Name", "NOT_READED");
myObj[i].Info = ini.GetString(i.ToString(), "Info", "NOT_READED");
myObj[i].Index = i + 1;
myObj[i].pictureBox = IndicatorImage[i];
IndicatorImage[i].Name = myObj[i].Name;
IndicatorImage[i].Anchor = System.Windows.Forms.AnchorStyles.None;
IndicatorImage[i].BackColor = System.Drawing.Color.Transparent;
IndicatorImage[i].Image = global::Map_5_Client.Properties.Resources.obj;
IndicatorImage[i].Size = new System.Drawing.Size(32, 28);
IndicatorImage[i].Location = new System.Drawing.Point(ini.GetInt32(i.ToString(), "LocationX", 0), ini.GetInt32(i.ToString(), "LocationY", 0));
IndicatorImage[i].Click += new EventHandler(this.IndicatorImage_Click);
pictureBox1.Controls.Add(IndicatorImage[i]);
}
}
private void IndicatorImage_Click(object sender, EventArgs e)
{
PictureBox currentPictureBox = (PictureBox)sender;
ObjectDialog(myObj[1]);
}Решение задачи: «Передача параметров обработчику событий»
textual
Листинг программы
int index = Array.IndexOf(IndicatorImage, currentPictureBox);