vb.net 鼠标悬停事件

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/31717199/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-17 19:21:16  来源:igfitidea点击:

Mouse hovering event

vb.net

提问by user3223293

I want to do an mouse hovering event, when the mouse is over an button I want to change button text color and font size, I have try this code but doesn't work:

我想做一个鼠标悬停事件,当鼠标悬停在按钮上时,我想更改按钮文本颜色和字体大小,我尝试过此代码但不起作用:

 Private Sub Command1_MouseMove(Button As Integer, Shift As Integer, X As Single, Y As Single)
Command1.ForeColor.MediumBlue()
    Command1.FontSize = 10
End Sub

Private Sub Form_MouseMove(Button As Integer, Shift As Integer, X As Single, Y As Single)
Command1.ForeColor.White()
    Command1.FontSize = 8
End Sub

Can anyone give me a suggestion i have search on Google and try different ways with mouse event handler but didn't work.

任何人都可以给我一个建议,我在谷歌上搜索并尝试使用鼠标事件处理程序的不同方法,但没有用。

回答by miroxlav

First, instead of tracking every mouse move, you can rely on MouseEnterand MouseLeaveevents of the button.

首先,您可以依赖按钮的MouseEnterMouseLeave事件,而不是跟踪每次鼠标移动。

Second, do not forget to add Handles <Control>.<Event>clause at the declaration of your event-handling procedures.

其次,不要忘记Handles <Control>.<Event>在事件处理程序的声明处添加子句。

Result:

结果:

Private Sub Command1_MouseEnter(sender As Object, e As EventArgs) _
Handles Command1.MouseEnter
    Command1.FontSize = 10
End Sub

Private Sub Command1_MouseLeave(sender As Object, e As EventArgs) _
Handles Command1.MouseLeave
    Command1.FontSize = 8
End Sub

Also please do not forget that some users are preferring keyboard control. This means that

另外请不要忘记有些用户更喜欢键盘控制。这意味着

  1. You might want to equip the button with an accelerator.

    Command1.Text = "&Launch"(now Alt+Lactivates the button)

    Note: accelerator character for winformsis &, for wpfis _.

  2. You might want to make your entry/leave effect also when the button receives/looses keyboard focus (focus is moved using Taband Shift+Tabkey).

  1. 您可能希望为按钮配备加速器。

    Command1.Text = "&Launch"(现在Alt+L激活按钮)

    注意:winforms 的加速器字符是&wpf_.

  2. 当按钮接收/松开键盘焦点(使用TabShift+Tab键移动焦点)时,您可能还希望使您的进入/离开效果。

回答by Andrew Mortimer

You can try making your changes into MouseEnter and MouseLeave

您可以尝试将更改更改为 MouseEnter 和 MouseLeave

    Private Sub RightButton_MouseEnter(sender As System.Object, e As System.EventArgs) Handles RightButton.MouseEnter
        RightButton.ForeColor = Color.AliceBlue
        RightButton.Font = New Font(RightButton.Font, 12)
    End Sub

    Private Sub RightButton_MouseLeave(sender As System.Object, e As System.EventArgs) Handles RightButton.MouseLeave
        RightButton.ForeColor = Color.White
        RightButton.Font = New Font(RightButton.Font, 10)
    End Sub