Form/Mouse Basics

Posted on 2006-05-10
Last Modified: 2010-04-16
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.
Question by:potatoes44
    1 Comment
    LVL 85

    Accepted Solution

    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";
                        label2.Text = "Over Control: " + c.Name;
                    label1.Text = p.ToString() + " --> Cursor OUTSIDE form";
                    label2.Text = "N/A";

    Featured Post

    Why You Should Analyze Threat Actor TTPs

    After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

    Join & Write a Comment

    Introduction                                                 Was the var keyword really only brought out to shorten your syntax? Or have the VB language guys got their way in C#? What type of variable is it? All will be revealed.   Also called…
    Introduction Hi all and welcome to my first article on Experts Exchange. A while ago, someone asked me if i could do some tutorials on object oriented programming. I decided to do them on C#. Now you may ask me, why's that? Well, one of the re…
    Migrating to Microsoft Office 365 is becoming increasingly popular for organizations both large and small. If you have made the leap to Microsoft’s cloud platform, you know that you will need to create a corporate email signature for your Office 365…
    Internet Business Fax to Email Made Easy - With eFax Corporate (, you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…

    746 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    18 Experts available now in Live!

    Get 1:1 Help Now