Winform - determine if the user has left user control

I'm struggling with something that, in my opinion, should be easy (ish). I have a window shape and a flowgridlayout panel at the bottom of the form. Inside this form, I dynamically populate it with the X-number of user controls. The controls are of the same type.

The goal is when the user hovers over the user control, it opens another shape and positions it where the mouse is. When the mouse leaves the form, the open form disappears.

It is almost excellent. The problem is that the user control has something like a shortcut or text field inside it. It is believed that he left UC, so the form disappears.

My thought was to use X and Y to determine if it is inside UC, but I cannot figure it out.

May I ask:

1) What is the best approach to this? 2) How can I encode it, since UCs are dynamic, I cannot know exactly where they will be.

thanks

EDIT

I am trying to understand mouse pointers but not getting there. The code below is in the UC SmallTagBox_MouseLeave event:

Point loc = this.Location; Point p = this.PointToScreen(this.Location); Point p2 = this.PointToScreen(this.Parent.Location); Point ms = MousePosition; Rectangle screenBounds = new Rectangle(this.PointToScreen(this.Location), this.Size); if (!screenBounds.Contains(ms)) { thw.Close(); thw = null; } 
  • loc {X = 275 Y = 3} System.Drawing.Point
  • p {X = 808 Y = 908} System.Drawing.Point
  • p {X = 808 Y = 908} System.Drawing.Point
  • p2 {X = 545 Y = 1542} System.Drawing.Point
  • ms {X = 574 Y = 914} System.Drawing.Point
  • screenBounds {X = 808 Y = 908 Width = 62 Height = 29} System.Drawing.Rectangle

I do not understand how p2 (its parent) can have a larger Y value relative to the screen.

+4
source share
4 answers

Binding all the MouseEnter and MouseLeave controls, and then figuring out, still inside the form, is pretty painful. A simple timer can also do the job:

  public partial class Form1 : Form { private Timer mTimer; public Form1() { InitializeComponent(); mTimer = new Timer(); mTimer.Interval = 200; mTimer.Tick += mTimer_Tick; mTimer.Enabled = true; } private void mTimer_Tick(object sender, EventArgs e) { if (!this.DesktopBounds.Contains(Cursor.Position)) this.Close(); } } 
+3
source

Idea 1) When the MouseLeave event fires, you can check the mouse coordinates (relative to the screen) and check whether they are all within your user control. If so, you should assume that the mouse must go through the control to go beyond, and you can safely ignore the event this time.

Idea 2) Attach MouseEnter event MouseEnter to child controls. Then, when the mouse enters one, you will know and can ignore the usercontrol MouseLeave event. Then, when the child MouseLeave event occurs, check the usercontrol MouseEnter .

0
source

I think I would add an event handler for MouseLeave for each control you have, and use the Parent property to find the user control you are using. I agree it will be a little painful though.

0
source

You can also scroll through all the child controls (recursive) in your control and attach the MouseEnter and MouseLeave events to them.

You need to do some accounting if the mouse is under your control or some kind of subsidiary management.

0
source

Source: https://habr.com/ru/post/1237794/


All Articles