Search code examples
c#winformsclickpicturebox

How to make sub for Picture box made during Run time in WinForms


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);
        }
     }

Solution

  • 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");
    }