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 9137345
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T09:05:32+00:00 2026-06-17T09:05:32+00:00

I am creating a number of transparent controls on a form which each display

  • 0

I am creating a number of transparent controls on a form which each display a hex shape, they are grouped as a map, so that the hexes are edge to edge. Of course each hex is contained within a square control, and so they overlap with each neighbor hex. I want to have able to click inside a hex to cause it to become highlighted. However, unless I would click in the middle of the hex where there is no overlap, depending on the stack order of the controls the click event would be picked up by whichever control is on top, not necessarily the control I want. So as I see it, I need to do two things:

  1. I need to ensure that the cursor is inside a hex shape (there is a small gap between the edge of each hex). So if I click in one of the corners of the control, that control would not respond to that click since its outside of the hex.
  2. When I click, I need to determine which control owns the hex I am clicking on.
  • 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-06-17T09:05:33+00:00Added an answer on June 17, 2026 at 9:05 am

    I cannot give you the code because I would need to spend some time working the exact code out, but I do have a suggestion of how you can achieve it…

    1) Work out all the controls that the mouse could potentially be clicking on. Perhaps you can do this by calculating the mouse position relative to all the controls and looking for overlapping points

    2) Iterate though all the potential candidates and calculate the the distance between the mouse point and the centre point of each control (this might help). The correct control will be the one with the shortest distance

    You are going to need to put your maths head on for this one!


    SOLUTION:

    This works and I have tested it. What I have: A UserControl that draws the shape, this is called “ClickControl”. All of my ClickControls are inside a Panel called mainPanel. Each ClickControl has the same MouseClick event registered to it, in this case the control_MouseClick event. With all that in mind, here is the example code:

    void control_MouseClick(object sender, MouseEventArgs e)
    {
        //get mouse point relative to panel
        var mousePoint = panelMain.PointToClient(Cursor.Position);
        int startX = mousePoint.X;
        int startY = mousePoint.Y;
    
        //store the best match as we find them
        ClickControl selected = null;
        double? closestDistance = null;
    
        //loop all controls to find the best match
        foreach (Control c in panelMain.Controls)
        {
            ClickControl control = c as ClickControl;
            if (control != null)
            {
                //calculate the center point of the control relative to the parent panel
                int endX = control.Location.X + (control.Width / 2);
                int endY = control.Location.Y + (control.Height / 2);
    
                //calculate the distance between the center point and the mouse point
                double distance = Math.Sqrt(Math.Pow(endX - startX, 2) + Math.Pow(endY - startY, 2));
    
                //if this one is closer then we store this as our best match and look for the next best match
                if (closestDistance == null || closestDistance > distance)
                {
                    selected = control;
                    closestDistance = distance;
                }
            }
        }
    
        //`selected` is now the correct control
    }
    

    I am sure there is plenty of optimising that can be done if you have performance issues, but this is a working start at the very least!

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am working on a project which requires creating a number of JButtons. Each
I am creating a windows service that starts a number of slave processes. In
I am creating a user-boards applicationin rails in which a user owns any number
We're creating a number of MATLAB MEX files that use our communications library. This
I have a situation where I'm creating a number of IDisposable objects that encapsulate
In my current project, I am creating a number of user controls, some of
Does anyone have any good resources that show creating an unlimited number of threads
I am creating a number of Powershell cmdlet's and am noticing that after I
How would I go about creating a random number generator which has a bias
I am creating a number of custom controls for a Silverlight 4 project. I

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.