将点击事件添加到Picturebox vb.net [英] add on click event to picturebox vb.net

查看:116
本文介绍了将点击事件添加到Picturebox vb.net的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个flowLayoutPanel,我以编程方式向其中添加了新的panelLayouts.每个panelLayout中都有一个pictureBox.一切正常,但是我需要检测一下何时单击该图片框.如何在图片中添加事件?我似乎只能找到c#示例....

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....

我添加图像的代码如下...

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)

因此,我假设在创建图像时添加了onclick事件.如果可能的话,我还需要获取索引!感谢您的帮助!

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!

推荐答案

使用AddHandler语句订阅Click事件:

Use the AddHandler statement to subscribe to the Click event:

    AddHandler pic.Click, AddressOf pic_Click

pic_Click()方法的sender参数为您提供了对图片框的引用:

The sender argument of the pic_Click() method gives you a reference to the picture box back:

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

如果您需要有关特定控件的其他信息(例如索引),则可以使用Tag属性.

If you need additional info about the specific control, like an index, then you can use the Tag property.

这篇关于将点击事件添加到Picturebox vb.net的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆