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

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

问题描述

我有一个 flowLayoutPanel,我正在以编程方式向其中添加新的 panelLayouts.每个 panelLayout 中都有一个图片框.一切都很好,但是我需要检测何时单击该图片框.如何在图片中添加事件?我似乎只能找到 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.

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

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