topofsteel
topofsteel

Reputation: 1277

Events from controls created at run-time

I am populating a flowLayoutPanel with pictureBoxes at run-time with the following code

        for (int i = 0; i < immageArray.Length; i++)
        {
            Image image = Image.FromFile(immageArray[i]);

            pictureBoxArray[i] = new PictureBox();
            pictureBoxArray[i].Image = image;
            pictureBoxArray[i].Width = 256;
            pictureBoxArray[i].Height = 256;
            pictureBoxArray[i].SizeMode = PictureBoxSizeMode.Zoom;

            flowLayoutPanel1.Controls.Add(pictureBoxArray[i]);
        }

How can do I create the event/events for controls that don't exist yet at design time?

Upvotes: 1

Views: 65

Answers (1)

Kamil Budziewski
Kamil Budziewski

Reputation: 23087

Try this:

pictureBoxArray[i].MouseDown += new MouseEventHandler(pictureBox_MouseDown);

...

private void pictureBox_MouseDown(object sender, MouseEventArgs e) 
{  
    ....
}

pictureBox_MouseDown is your mouseDown event handler, of course you can attach any event not only MouseDown and you can do it for any control created at runtime.

here is the list of events for PictureBox

Upvotes: 1

Related Questions