本文介绍了MouseLeave未触发C#WinForms的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个带有2个按钮的用户控件,只有当鼠标位于控件区域内时,该控件才可见.

I have a user control with 2 buttons, that should only be visible when the mouse is inside the area of the control.

我正在显示这样的按钮:

I'm showing the buttons like this:

private void Node_MouseEnter(object sender, EventArgs e)
{
    btn1.Show();
    btn2.Show();
}

并像这样隐藏:

protected override void OnMouseLeave(EventArgs e)
{
    if (this.ClientRectangle.Contains(this.PointToClient(Control.MousePosition)))
        return;
    else base.OnMouseLeave(e);
}

private void Node_MouseLeave(object sender, EventArgs e)
{
    btn1.Hide();
    btn2.Hide();
}

问题在于,有时(随机情况下)不会触发MouseLeave事件,并且即使鼠标位于控件之外,按钮仍保持可见.

The problem is that sometimes (random situations) the MouseLeave event is not fired, and the buttons stay visible, even with the mouse outside the control.

是否有可能多个事件发生冲突?

Is it possible that multiple events get in conflict ?

推荐答案

作为链接状态:

因此,解决方案是仅侦听MouseEnter事件,并且在触发该事件时,我向其他控件发送通知,以隐藏其按钮.

So, the solution was to listen only for the MouseEnterevent, and when this event is fired, i send a notification to the other controls, to hide its buttons.

这不是最优雅的解决方案,但它可以按预期工作.

Is not the most elegant solution, but it works as expected.

这篇关于MouseLeave未触发C#WinForms的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-15 05:10