Add click event to picturebox vb.net

I have a flowLayoutPanel that I programmatically add to the new panelLayouts. Each panelLayout has a pictureBox inside it. All of this works great, but I need to determine when this picture is clicked. How to add an event to a picture? It seems I can only find C # examples ....

my code for adding an image is as follows:

' add pic to the little panel container Dim pic As New PictureBox() pic.Size = New Size(cover_width, cover_height) pic.Location = New Point(10, 0) pic.Image = Image.FromFile("c:/test.jpg") panel.Controls.Add(pic) 'add pic and other labels (hidden in this example) to the big panel flow albumFlow.Controls.Add(panel) 

So, I assume that when I create the image, I add the onclick event. I need to get an index for it, if possible! Thanks for any help!

+4
source share
2 answers

Use the AddHandler statement to subscribe to the Click event:

  AddHandler pic.Click, AddressOf pic_Click 

The sender argument of the pic_Click () method gives you a link to the image window back:

 Private Sub pic_Click(ByVal sender As Object, ByVal e As EventArgs) Dim pic As PictureBox = DirectCast(sender, PictureBox) ' etc... End Sub 

If you need additional information about a specific control, such as an index, then you can use the Tag property.

+5
source

Replace PictureBox1 with the name of your control.

 Private Sub PictureBox1_Click(sender As Object, e As EventArgs) Handles PictureBox1.Click 'executes when PictureBox1 is clicked End Sub 
0
source

Source: https://habr.com/ru/post/1308693/


All Articles