I’m trying to make something of a Tile-Map editor in C# using picture-boxes (the simplest way i could think to throw tiles into a form)
The code that Generates the picture boxes is:
public void Generate_Tiles(int width, int height)
{
for (int x = 0; x < width; x++)
{
for (int y = 0; y < height; y++)
{
System.Windows.Forms.PictureBox picturebox = new PictureBox();
picturebox.BackColor = SystemColors.ActiveCaption;
picturebox.Cursor = Cursors.Cross;
picturebox.Location = new System.Drawing.Point((x*32) + 4, (y*32) + 4);
picturebox.Name = x+y+"tile";
picturebox.Size = new System.Drawing.Size(32, 32);
picturebox.TabIndex = 0;
picturebox.TabStop = false;
picturebox.Click += new System.EventHandler(TileBox_Clicked));
map.Controls.Add(picturebox);
}
}
MessageBox.Show("Done");
}
That part works, as long as i use a small number of picture boxes at a time (8 by 8 seems to be the maximum that it wants to display in a decent amount of time)
I want to perform some action when the user clicks on a specified picture-box, which is why i have a onclick method, that’s where i run into problems, all the picture-boxes are called… picture-box. As far as i can tell, there’s no way for me to tell which picture-box the user clicked.
I will probably need to remake the way the dynamic form works anyway, since i cant get very many picture boxes, but I think the main problem will still be there, as long as i want it to be dynamic (which i do), not all of the tile-maps will be the same size.
Ive never done anything like this, and I’ve looked for ways to override the onclick event… which i couldn’t find, and i couldn’t find a good tile-engine that’s up to date to use (except for XNA, but that’s a little over the top for a simple tile-editor, i think)
Im likely going in the opposite direction then what i need to be doing.
The sender in your event handler will be the
PictureBoxwhich was clicked.