Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 3283094
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 17, 20262026-05-17T20:00:07+00:00 2026-05-17T20:00:07+00:00

In my C# (.NET 2) app I’d like to determine which control is closet

  • 0

In my C# (.NET 2) app I’d like to determine which control is closet to the mouse.

I can think of a few ways to do this that won’t quite work right. I could use the Control.Location property, but that just gives me top/left, and the mouse might be on the other side of the control. I could calculate the center point of a control, but large controls would skew this (being near the edge of a control counts as being close to the control).

So basically I have a bunch of rectangles on a canvas and a point. I need to find the rectangle nearest to the point.

(Ideally I’d like to actually know the distance between the point and rectangle, too).

Any ideas?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-17T20:00:07+00:00Added an answer on May 17, 2026 at 8:00 pm

    You need to find the following:
    – Distance to the closest corner
    – Distance to the closest edge
    – (optionally) distance to the center

    Basically, you want the smaller of these three values. Take the min of that for two controls to determine which is closer.

    Begin when you load the form by iterating all the controls on the form and creating a collection of the class below.

    To find the closest control to a point, iterate the collection (see code at bottom). Keep track of the control with the minimum distance you’ve found so far. You can test for ContainsPoint() if you want… if you find a control where the point falls within the control bounds, you’ve got your control (so long as you don’t have overlapping controls). Else, when you get to the end of the collection, the control you found with the shortest distance to the center/edge is your control.

    public class HitControl {
    
        public Control ThisControl;
    
        private Rectangle ControlBounds;
        private Point Center;
    
        public HitControl (Control FormControl) {
            ControlBounds = FormControl.Bounds;
            Center = new Point(ControlBounds.X + (ControlBounds.Width/2), ControlBounds.Y + (ControlBounds.Height/2));
        }
    
        //  Calculate the minimum distance from the left, right, and center
        public double DistanceFrom(Point TestPoint) {
    
            //  Note:  You don't need to consider control center points unless
            //  you plan to allow for controls placed over other controls... 
            //  Then you need to test the distance to the centers, as well, 
            //  and pick the shortest distance of to-edge, to-side, to-corner
    
            bool withinWidth = TestPoint.X > ControlBounds.X && TestPoint.X < ControlBounds.X + ControlBounds.Width;
            bool withinHeight = TestPoint.Y > ControlBounds.Y && TestPoint.Y < ControlBounds.Y + ControlBounds.Height;
    
            int EdgeLeftXDistance = Math.Abs(ControlBounds.X - TestPoint.X);
            int EdgeRightXDistance = Math.Abs(ControlBounds.X + ControlBounds.Width - TestPoint.X);
    
            int EdgeTopYDistance = Math.Abs(ControlBounds.Y - TestPoint.Y);
            int EdgeBottomYDistance = Math.Abs(ControlBounds.Y + ControlBounds.Height - TestPoint.Y);
    
            int EdgeXDistance = Math.Min(EdgeLeftXDistance, EdgeRightXDistance);
            int EdgeYDistance = Math.Min(EdgeTopYDistance, EdgeBottomYDistance);
    
    
            // Some points to consider for rectangle (100, 100, 100, 100):
            //  - (140, 90):  Distance to top edge
            //  - (105, 10):  Distance to top edge
            //  - (50, 50):   Distance to upper left corner
            //  - (250, 50):  Distance to upper right corner
            //  - (10, 105):  Distance to left edge
            //  - (140, 105):  Distance to top edge
            //  - (105, 140):  Distance to left edge
            //  - (290, 105):  Distance to right edge
            //  - (205, 150):  Distance to right edge
            //  ... and so forth
    
    
            //  You're within the control
            if (withinWidth && withinHeight) {
                return Math.Min(EdgeXDistance, EdgeYDistance);
            }
    
            //  You're above or below the control
            if (withinWidth) {
                return EdgeYDistance;
            }
    
            //  You're to the left or right of the control
            if (withinHeight) {
                return EdgeXDistance;
            }
    
            //  You're in one of the four outside corners around the control.
            //  Find the distance to the closest corner
            return Math.Sqrt(EdgeXDistance ^ 2 + EdgeYDistance ^ 2);
    
    
        }
    
        public bool ContainsPoint (Point TestPoint) {
            return ControlBounds.Contains(TestPoint);
        }
    
    
    }
    
    
    
    //  Initialize and use this collection
    List<HitControl> hitControls = (from Control control in Controls
                                    select new HitControl(control)).ToList();
    
    Point testPoint = new Point(175, 619);
    double distance;
    double shortestDistance = 0;
    HitControl closestControl = null;
    
    foreach (HitControl hitControl in hitControls) {
    
        //  Optional... works so long as you don't have overlapping controls
        //  If you do, comment this block out
        if (hitControl.ContainsPoint(testPoint)) {
            closestControl = hitControl;
            break;
        }
    
        distance = hitControl.DistanceFrom(testPoint);
        if (shortestDistance == 0 || distance < shortestDistance) {
            shortestDistance = distance;
            closestControl = hitControl;
        }
    }
    
    if (closestControl != null) {
        Control foundControl = closestControl.ThisControl;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

In a 32-bit .NET app, I can use this OLEDB connection string to connect
I have an Asp.net app that simply reads an xml file and this code
I have a .NET app that I'd like to do some automated testing on.
In my ASP.NET app I have a control in the master page that raises
I have an asp.net app with an asp:button that causes a postback, this means
I have a .NET app that I would like to install on a VM
We have a NET app that gets installed to the Program Files folder. The
Just wondering if a .NET app can be compiled down to native machine code
So in a .NET app , i got about 2 million items that i
I have an ASP.NET app running in IIS that is hosting files all great

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.