Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 161
  • Last Modified:

Form/Mouse Basics

Just wondering is there is an easy way to check if the mouse is over a control. Right now I'm using events and keeping track of the mouses's status like so:

private void panel1_MouseEnter()
{
_mouseIsInPanel = true;
}

private void panel1_MouseLeave()
{
_mouseIsInPanel = false;
}

Obviously, this is pretty simple as it is, but I was just wondering if there is an even easier way to do it, perhaps by checking some property, that I'm unaware of. Also, if there is an easy way, can this also be done for getting the mouse's current position, etc.? Thanks in advance.
0
potatoes44
Asked:
potatoes44
1 Solution
 
Mike TomlinsonMiddle School Assistant TeacherCommented:
This will work for all controls contained directly by the form.  For nested controls, you need a more sophisticated approach:

        private void timer1_Tick(object sender, EventArgs e)
        {
            Point p = Cursor.Position;
            Rectangle r = new Rectangle(this.Location, this.Size);
            if (r.Contains(p))
            {
                label1.Text = p.ToString() + " --> Cursor INSIDE form";
               
                // convert from screen to client coordinates
                p = this.PointToClient(p);

                // get the control under the cursor
                Control c = this.GetChildAtPoint(p);
                if (c == null)
                {
                    label2.Text = "Not over a Control";
                }
                else
                {
                    label2.Text = "Over Control: " + c.Name;
                }
            }
            else
            {
                label1.Text = p.ToString() + " --> Cursor OUTSIDE form";
                label2.Text = "N/A";
            }        
        }
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Tackle projects and never again get stuck behind a technical roadblock.
Join Now