Click here to Skip to main content
15,881,715 members
Articles / Programming Languages / C#
Tip/Trick

Get Mouse Click relative Position

Rate me:
Please Sign up or sign in to vote.
5.00/5 (4 votes)
12 Aug 2010CPOL 21.8K   3   1
If you need to get the position the mouse was clicked, in relation to a specific UI Element, this might help
Today, I was faced with a requirement where I had to respond to the user clicking on a cell in a manually built grid. Gird your loins - the cell itself was a label in a border in a grid in a stack panel in a grid on the page. Because of the way this particular label was displayed, there was white space to the left of the actual text.

My layout produced some unexpected issues. I tried setting the mouse-up handler for the label, and that didn't work for white space in the cell. The same for the border. Finally, I put the handler in the grid for the entire row, and while the white space problem was solved, the mouse up event was fired for anything in the row that wasn'rt already handled by another handler (buttons).

So in my handler, I do this:

C#
private void FMMetric_MouseLeftButtonUp(object sender, MouseButtonEventArgs e)
{
    // make sure the event came from a grid element
    if (sender is Grid)
    {
        // get the grid
        Grid element = sender as Grid;
        // get the first child (if possible). It *should* be a border
        if (element.Children != null && 
            element.Children.Count >= 1 && 
            element.Children[0] is Border)
        {
            Border firstChild = element.Children[0] as Border;
            // now see where the user clicked the mouse in relation to the 
            // discovered border
            Point point = e.GetSafePosition(firstChild);
            // if the X value is <= the actual rendered width of the element, 
            // we can process the mouse click and navigate to the appropriate 
            // page.
            if (point.X <= firstChild.ActualWidth)
            {
                // ... put code here ...
            }
        }
    }
}

Of course the example above is specific to my code, but you should be able to see the technique.

You can use this approach to find the mouse position in relation to any given UI element on the page.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Software Developer (Senior) Paddedwall Software
United States United States
I've been paid as a programmer since 1982 with experience in Pascal, and C++ (both self-taught), and began writing Windows programs in 1991 using Visual C++ and MFC. In the 2nd half of 2007, I started writing C# Windows Forms and ASP.Net applications, and have since done WPF, Silverlight, WCF, web services, and Windows services.

My weakest point is that my moments of clarity are too brief to hold a meaningful conversation that requires more than 30 seconds to complete. Thankfully, grunts of agreement are all that is required to conduct most discussions without committing to any particular belief system.

Comments and Discussions

 
GeneralReason for my vote of 5 Thank you for sharing this tip Pin
linuxjr11-Aug-10 6:00
professionallinuxjr11-Aug-10 6:00 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.