在sub中设置按钮?

时间:2015-03-29 21:19:57

标签: vb.net

所以,我有一点问题。我正在制作一个应用程序(在visual basic中),我有一种方法可以在你将鼠标悬停在它上面时设置颜色。现在,我想为所有按钮执行此操作,但通过创建一个可以为我执行此操作的子程序,可以使它更容易一些。问题是,我的子如何告诉哪个按钮初始化自定义悬停颜色?这是我的代码。

 Private Sub initButton(ByVal color As Color)
    Button1.TabStop = False
    Button1.FlatStyle = FlatStyle.Flat
    Button1.FlatAppearance.BorderSize = 0
    Button1.FlatAppearance.BorderColor = color
    Button1.FlatAppearance.CheckedBackColor = color
    Button1.FlatAppearance.MouseDownBackColor = color
    Button1.FlatAppearance.MouseOverBackColor = color
 End Sub

现在,如何将Button1设置为我想要初始化的按钮?无论如何把它作为一个论点?如果您找到答案,请回复。

1 个答案:

答案 0 :(得分:3)

正如Plutonix建议的那样,使用按钮的悬停事件将是第一个合理的选择。您需要在Sub中添加一个参数,以传递悬停的按钮。

 Private Sub Button1_MouseHover(sender As Object, e As EventArgs) 
   Handles Button1.MouseHover,Button2.MouseHover 'add more buttons....
    initButton(CType(sender, Button),Color.Blue)
 End Sub

 Private Sub initButton(hoverButton As Button, ByVal color As Color)
     hoverButton.TabStop = False
     hoverButton.FlatStyle = FlatStyle.Flat
     hoverButton.FlatAppearance.BorderSize = 0
     hoverButton.FlatAppearance.BorderColor = color
     hoverButton.FlatAppearance.CheckedBackColor = color
     hoverButton.FlatAppearance.MouseDownBackColor = color
     hoverButton.FlatAppearance.MouseOverBackColor = color
 End Sub