Vb.net AddHandler MouseHover

时间:2012-06-20 18:10:21

标签: vb.net winforms addhandler

我的代码:

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Dim top As Integer = 0
        For i = 0 To 10
            Dim inLine As Integer = 8
            Dim left As Integer = 0
            For x = 0 To inLine
                Dim s As New Panel
                s.BackColor = Color.Black
                s.Width = 10
                s.Height = 10
                s.Left = left
                s.Top = top
                left = left + 20
                AddHandler s.MouseHover, AddressOf Panel1_MouseHover
                Me.Controls.Add(s)
            Next
            top = top + 20
        Next

    End Sub

        Private Sub Panel1_MouseHover(ByVal sender As Object, ByVal e As System.EventArgs)
   /////---- Some code!! :/ 
        End Sub

我的代码将面板添加到我的中,我希望当鼠标悬停在面板上时 面板改变背景颜色。

如果有人不理解:
当我的鼠标悬停在我添加到表单的面板上时,我如何更改面板颜色?

Private Sub Panel1_MouseHover(ByVal sender As Object, ByVal e As System.EventArgs)
       /////---- Some code to change the panel color !! :/ 
End Sub

1 个答案:

答案 0 :(得分:1)

我认为您缺少的部分是事件处理程序方法的sender参数将始终是引发事件的控件。在使用它之前,我会将其转换为正确的类型,以便您充分利用intellisense和编译器类型检查的优势。

Private Sub Panel1_MouseHover(ByVal sender As Object, ByVal e As System.EventArgs)
    Dim panel As Panel = CType(sender, Panel)
    panel.BackColor = Color.White
End Sub