Search code examples
c#pictureboxmouseclick-event

Click on a picturebox and go to website


I have a pictureBoxes that are being created at run time and I would like to be able to click on that box and go to a webpage after the program completes. How can I create a click event for something like this?

This is what I am thinking:

    PictureBox PB = new PictureBox();
                    PB.Name = "PB" + i.ToString();
                    PB.Location = new Point(51 * i, 331);
                    PB.Size = new Size(50, 50);
                    PB.ImageLocation = Sub1;
                    Controls.Add(PB);

    PB.Click +=new EventHandler(PB_Click); 


    protected void PB_Click(object sender, EventArgs e) 

{

MessageBox.Show("You clicked the mouse over the PictureBox"); 

}

Is this on the right track?


Solution

  • enter image description hereAfter fooling around a little I figured it out and thought I would post my solution, maybe someone else could benefit. I decided to take the easy route and just open up the link using webrowser control on the form.

    ![private void FrmWeb_Btn_Click(object sender, EventArgs e)
            {
    
    
                PictureBox PB = new PictureBox();
    
                PB.ImageLocation =  "https://si0.twimg.com/profile_images/378800000038434114/f676cbea6f8500c9c15529e1d5e548c1_reasonably_small.jpeg";
                PB.Size = new Size(100, 100);
                Controls.Add(PB);
    
                PB.Click +=new EventHandler(PB_Click); 
    
    
            }
    
            protected void PB_Click(object sender, EventArgs e)
            {
    
                webBrowser1.Navigate("http://twit.tv/");
    
            }][2]