0

我的代码:

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

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

如果有人不明白:
当我的鼠标悬停在我添加到表单中的面板时,我如何更改面板的背景颜色?

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

1 回答 1

1

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

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
于 2012-06-20T18:17:28.620 回答