I am making a 2D randomly generated tile map, as part of my school project. I've made a picture box array dynamically to store individual tiles, and i want to be able to let the user click on a picturebox with an event raised only to that specific picturebox, but im having trouble with it. (my map is 40x40 which makes 1600 pictureboxes, i do realise that is really inefficient :) )
current_mapXY[0] = 20; //these are the sizes in x and y of the pictureboxes
current_mapXY[1] = 20;
for (int i = 0; i < 40; i++)
{
for (int i2 = 0; i2 < 40; i2++)
{
map[i, i2] = new PictureBox();
map[i, i2].SizeMode = PictureBoxSizeMode.StretchImage;
map[i, i2].Location = new System.Drawing.Point(current_mapXY[0] + (nextxy * i), current_mapXY[1] + (nextxy * i2));
map[i, i2].Size = new System.Drawing.Size(current_mapXY[0], current_mapXY[1]);
this.Controls.Add(map[i, i2]);
progressBar1.Increment(1);
}
}
Then after texturing i am trying to throw an event for each picturebox.
for (int i = 0; i < 40; i++)
{
for (int i2 = 0; i2 < 40; i2++)
{
map[i, i2].Click += (a, b) => pic_click(sender, e, (i + "." + i2));
}
}
private void pic_click(object sender, EventArgs e, string xy)
{
MessageBox.Show(xy);
}
What happens when i click a picturebox in runtime, is a messagebox with "40.40", but i want to be able to identify every picturebox. Thanks for any help in advance!