简化visual studio中的事件代码

时间:2014-07-29 15:38:02

标签: .net vb.net visual-studio

我对VB很新,我想知道是否有更好的方法来编写以下代码。我的表格顶部有6个按钮,当鼠标进入和离开时我会改变颜色。但是,对于每个鼠标移动而言,如果不为每个按钮编写单独的子,我无法找到一种方法。有关缩小此代码的任何想法吗?谢谢!

Private Sub Button1_MouseEnter(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.MouseEnter
    Button1.ForeColor = Color.White
End Sub

Private Sub Button1_MouseLeave(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.MouseLeave
    Button1.ForeColor = Color.Black
End Sub

Private Sub Button2_MouseEnter(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.MouseEnter
    Button2.ForeColor = Color.White
End Sub

Private Sub Button2_MouseLeave(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.MouseLeave
    Button2.ForeColor = Color.Black
End Sub

Private Sub Button3_MouseEnter(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.MouseEnter
    Button3.ForeColor = Color.White
End Sub

Private Sub Button3_MouseLeave(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.MouseLeave
    Button3.ForeColor = Color.Black
End Sub

Private Sub Button4_MouseEnter(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button4.MouseEnter
    Button4.ForeColor = Color.White
End Sub

Private Sub Button4_MouseLeave(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button4.MouseLeave
    Button4.ForeColor = Color.Black
End Sub

Private Sub Button5_MouseEnter(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button5.MouseEnter
    Button5.ForeColor = Color.White
End Sub

Private Sub Button5_MouseLeave(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button5.MouseLeave
    Button5.ForeColor = Color.Black
End Sub

Private Sub Button6_MouseEnter(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button6.MouseEnter
    Button6.ForeColor = Color.White
End Sub

Private Sub Button6_MouseLeave(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button6.MouseLeave
    Button6.ForeColor = Color.Black
End Sub

1 个答案:

答案 0 :(得分:2)

您可以拥有多个Handles项目,如下所示:

Private Sub Button_Enter(sender As Object, e As EventArgs) Handles _
    Button1.MouseEnter, Button2.MouseEnter, Button3.MouseEnter, Button4.MouseEnter, Button5.MouseEnter, Button6.MouseEnter

    Dim bn As Button = DirectCast(sender, Button)
    bn.BackColor = Color.White
End Sub

Private Sub Button_Leave(sender As Object, e As EventArgs) Handles _
    Button1.MouseLeave, Button2.MouseLeave, Button3.MouseLeave, Button4.MouseLeave, Button5.MouseLeave, Button6.MouseLeave

    Dim bn As Button = DirectCast(sender, Button)
    bn.BackColor = Color.Black
End Sub

或者您可以通过代码分配处理程序,如下所示:

Private Sub Button_Enter(sender As Object, e As EventArgs)
    Dim bn As Button = DirectCast(sender, Button)
    bn.BackColor = Color.White
End Sub

Private Sub Button_Leave(sender As Object, e As EventArgs)
    Dim bn As Button = DirectCast(sender, Button)
    bn.BackColor = Color.Black
End Sub

Private Sub SetButtonHandlers()
    Dim bns() As Button = {Button1, Button2, Button3, Button4, Button5, Button6}
    For Each bn In bns
        AddHandler bn.MouseEnter, AddressOf Button_Enter
        AddHandler bn.MouseLeave, AddressOf Button_Leave
    Next

End Sub

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

End Sub

如果你要添加或删除按钮,或者给它们提供有用的名称,后者更容易更新,因为它们只列在一个地方。