0

So I have a button, and when the button is clicked, a picture box is created. I just wanted to know how I can make a message box appear when I click on that newly created picturebox.

 private void Button1_Click(object sender, EventArgs e)
    {
        for (int i = 0; i < 1; i++)
        {
            PictureBox p = new PictureBox();
            flowLayoutPanel1.Controls.Add(p);
        }
     }
Tom Holmes
  • 1,196
  • 8
  • 11

1 Answers1

0

you have to add a click event to your picturebox

p.MouseClick += p_MouseClick;

after adding a event this function will be called on that event -

void p_MouseClick(object sender, MouseEventArgs e)
{      
       MessageBox.Show("clicked");
}
piedpiper
  • 520
  • 5
  • 18
  • Add a note on the `sender` object: it's the actual PictureBox that was clicked, so you can cast `sender` to PictureBox and access its properties (e.g., `(sender as PictureBox).Image = new Bitmap(...);`). – Jimi Jun 21 '19 at 07:17