【发布时间】:2015-07-28 19:51:38
【问题描述】:
我最近一直在开发一个应用程序,发现自己陷入了一个简单但烦人的问题。
我想在输入其父控件时使特定控件可见/不可见,并能够在此控件上执行事件(例如:单击)。问题是,当我输入要显示的控件时,鼠标悬停甚至在父级上都不起作用。这导致我要显示的控件闪烁(鼠标悬停工作 -> 控件显示 -> 鼠标悬停不再工作 -> 控件被隐藏 -> 鼠标悬停工作 -> 等等)。
我找到了这个“解决方案”来帮助我获得一些“稳定”的东西。
// Timer to make the control appearing properly.
private void Timer_Elapsed(object o, ElapsedEventArgs e)
{
try
{
ItemToHideDisplay.Visible = true;
var mousePoint = this.PointToClient(Cursor.Position);
if (mousePoint.X > this.Width ||
mousePoint.X < 0 ||
mousePoint.Y > this.Height ||
mousePoint.Y < 0)
{
HideDisplayTimer.Stop();
ItemToHideDisplay.Visible = false;
base.OnMouseLeave(e);
}
}
catch
{
// We don't want the application to crash...
}
}
protected override void OnMouseEnter(EventArgs e)
{
HideDisplayTimer.Start();
base.OnMouseEnter(e);
}
基本上,当我进入对象时,计时器会启动并每 50 毫秒检查一次鼠标是否在父对象中。如果是,则显示控件。如果不是,则停止计时器并隐藏控件。
这行得通。耶。但我觉得这个解决方案很丑。
所以我的问题是:是否有另一种方法,另一种解决方案比这个更漂亮?
如果我不够清楚,请告诉我:)
提前致谢!
编辑:嘿,我想我自己找到了!
诀窍是用这个覆盖父控件的 OnMouseLeave:
protected override void OnMouseLeave(EventArgs e)
{
var mousePoint = this.PointToClient(Cursor.Position);
if (mousePoint.X > this.Width ||
mousePoint.X < 0 ||
mousePoint.Y > this.Height ||
mousePoint.Y < 0)
{
base.OnMouseLeave(e);
}
}
这样在进入我已经显示的控件时(进入父控件),鼠标离开事件是不会触发的! 有效!
感谢您的回答。我猜你可以继续发表你的想法,因为我在互联网上没有看到很多解决方案:)
【问题讨论】: