Recently, I have been developing an application, and found myself stuck in a simple but annoying problem.
I would like a particular control to be visible / incomprehensible when I enter its parent element and can execute events (for example: click) of this control. The problem is that mouse hovering doesn’t even work on the parent when entering the control itself that I want to display. This causes the control that I want to display to flicker (mouseover is displayed → control → mouseover no longer works → control is hidden → mouseover works → etc.).
I found this “solution” to help me something “stable”.
// 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); }
Basically, when I enter an object, a timer starts and is checked every 50 ms if the mouse is in the parent. If so, the control is displayed. If not, the timer stops and the control is hidden.
It works. Hooray. But I find this decision very ugly.
So my question is: is there a different approach, a different solution, more beautiful than this?
Tell me if I'm not clear enough :)
Thanks in advance!
EDIT: Hey, I think I found it myself!
The trick is to override the OnMouseLeave of the parent control with this:
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); } }
Thus, when I enter the control that I displayed (by entering the parent control), the mouse stop event does not fire! He works!
Thank you for your responses. You can continue to publish your ideas, I think, because I do not see many solutions on the Internet :)