如何识别当前在mouseenter中选择的动态添加的图片框

时间:2014-02-06 06:55:36

标签: vb.net

我动态创建了5个Picture Box并添加了事件MouseEnter和MouseLeave,问题是当我将光标悬停在其中一个图片框上时,另一个也会触发事件

Private Sub Form_TEST_Load(sender As Object, e As EventArgs) Handles MyBase.Load

    Dim ImgSelect As PictureBox
    Dim x As Integer = 1

    While Not x = 5

        ImgSelect = New PictureBox
        ImgSelect.Name = "image0" & x.ToString
        ImgSelect.BackColor = Color.Black
        ImgSelect.Size = New Size(203, 312)
        ImgSelect.BorderStyle = BorderStyle.FixedSingle
        ImgSelect.Visible = True
        ImgSelect.BringToFront()
        ImgSelect.SizeMode = PictureBoxSizeMode.StretchImage

        FlowLayoutPanel1.Controls.Add(ImgSelect)

        x += 1

        AddHandler ImgSelect.MouseEnter, AddressOf ImgSelect_MouseEnter
        AddHandler ImgSelect.MouseLeave, AddressOf ImgSelect_MouseLeave

    End While

End Sub

2 个答案:

答案 0 :(得分:0)

解决方案:

您已为流程布局面板中的所有控件添加创建一个事件  此声明绑定了imgselecet事件,你有一个控件img选择tha为什么所有图片框上的fir事件......

 AddHandler ImgSelect.MouseEnter, AddressOf ImgSelect_MouseEnter
    AddHandler ImgSelect.MouseLeave, AddressOf ImgSelect_MouseLeave

或在全球范围内撰写本声明并删除处理程序

 Dim WithEvents ImgSelect As New PictureBox

答案 1 :(得分:0)

在鼠标输入事件处理程序中,您可以获得从sender参数触发事件的UI控件:

Private Sub ImgSelect_MouseLeave(sender As Object, e As System.EventArgs)
    Dim CurrentImage As PictureBox = DirectCast(sender, PictureBox)
    ......
End Sub