I am initializing an array of PictureBox. The matter is, I cannot add individual EventHandler with each. I have this code in a for loop:
picBoxs[i] = new PictureBox();
picBoxs[i].Anchor = AnchorStyles.None;
picBoxs[i].Location = new Point(x, y);
picBoxs[i].Size = new Size(104, 104);
picBoxs[i].SizeMode = PictureBoxSizeMode.Normal;
picBoxs[i].BorderStyle = BorderStyle.FixedSingle;
//this.picBoxs[i].Click += new System.EventHandler(this.picBoxs_Click); //Single EventHandler for all PictureBox(s)
this.picBoxs[i].Click += new System.EventHandler(this.picBoxs[i]_Click); //Generates error
I want picBoxs1_Click(), picBoxs2_Click(), picBoxs3_Click() …(and so on). Because I need EventHandler for each PictureBox. I can make general EventHandler, picBox_Click() without any problem, but doing this I get a method for every PictureBox, I cannot do different stuffs after clicking different PictureBox. Any idea will greatly be appreciated.
Thanks in advance…
Edit:
I want, when I click on a PictureBox, a message box will appear telling me an index of a 2D array. Say my picturebox(s) are arranged like this:
picBox0 picBox1 picBox2
picBox3 picBox4 picBox5
after clicking picBox2: MessageBox will say “[0][2]”, same as clicking picBox4: MessageBox will say “[1][1]” and so on…
Use a single event handler, like this:
And within that event handler you can look up information about your picturebox: