将点击事件添加到图片框 vb.net
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2760892/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
add on click event to picturebox vb.net
提问by Matt Facer
I have a flowLayoutPanel which I am programatically adding new panelLayouts to. Each panelLayout has a pictureBox within it. It's all working nicely, but I need to detect when that picture box is clicked on. How do I add an event to the picture? I seem to only be able to find c# examples....
我有一个 flowLayoutPanel,我正在以编程方式向其中添加新的 panelLayouts。每个panelLayout 中都有一个pictureBox。一切正常,但我需要检测何时单击该图片框。如何在图片中添加事件?我似乎只能找到 c# 示例....
my code to add the 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 somewhere when I'm creating the image I add an onclick event. I need to get the index for it also if that is possible! Thanks for any help!
所以我假设在创建图像时我添加了一个 onclick 事件。如果可能的话,我也需要获取它的索引!谢谢你的帮助!
回答by Hans Passant
Use the AddHandler statement to subscribe to the Click event:
使用 AddHandler 语句订阅 Click 事件:
AddHandler pic.Click, AddressOf pic_Click
The sender argument of the pic_Click() method gives you a reference to the picture box back:
pic_Click() 方法的 sender 参数为您提供了对图片框的引用:
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 info about the specific control, like an index, then you can use the Tag property.
如果您需要有关特定控件的其他信息(例如索引),则可以使用 Tag 属性。
回答by ohSkittle
Substitute PictureBox1 with the name of your control.
用您的控件名称替换 PictureBox1。
Private Sub PictureBox1_Click(sender As Object, e As EventArgs) Handles PictureBox1.Click
'executes when PictureBox1 is clicked
End Sub