-1

I want to fetch the position of Control in which click event is performed

for this I have tried following code

Xpos = Cursor.Position.X;
Ypos = Cursor.Position.Y;

but this is giving current position of cursor. then I tried following code

MouseEventArgs m = (MouseEventArgs)e;
Xpos=m.X;
Ypos=m.Y;

but this position is not with respect to whole screen.

How I can get position of control in which click event is performed?

edit

As the link provided for duplicate ,,,,It provides position of point where click action is performed,,,,It does not provide the position of control in which click is performed.

dhilmathy
  • 2,800
  • 2
  • 21
  • 29
  • Which event do you use to get `MouseEventArgs`? – vasily.sib Aug 10 '18 at 10:46
  • Is it `WindowsForms` or `WPF` application? – dhilmathy Aug 10 '18 at 10:47
  • 1
    Possible duplicate of [Getting The Location Of A Control Relative To The Entire Screen?](https://stackoverflow.com/questions/4998076/getting-the-location-of-a-control-relative-to-the-entire-screen) – dhilmathy Aug 10 '18 at 10:48
  • do you want position with respect to primary screen or your window screen? – er-sho Aug 10 '18 at 10:59
  • @ershoaib window screen –  Aug 10 '18 at 11:01
  • @dhilmathy : point will give the position where click is performed but I want control postion in which click is performed –  Aug 10 '18 at 11:13
  • Use ((Control)sender).Location to get the position of the control that was clicked. Please edit your question to make it clear that this is what you want. – Hans Passant Aug 10 '18 at 12:21

3 Answers3

0

Can you try this? It should return what you're expecting (Left Top Point of the control you click).

XAML:

<TextBlock Text="Sample" Width="100" Height="50" 
           MouseLeftButtonDown="TextBlock_MouseLeftButtonDown"/>

XAML.cs

private void TextBlock_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
{
    var control = sender as FrameworkElement;
    var positionToScreen = control.PointToScreen(new Point(0, 0));
}
dhilmathy
  • 2,800
  • 2
  • 21
  • 29
  • Their is a button in form to close the form It throws execption "System.ObjectDisposedException: 'Cannot access a disposed object. Object name: 'Button'.'" in line var positionToScreen –  Aug 10 '18 at 12:19
  • @vk2s Are you closing the window on button click? Before that, is it `WindowsForms` or `WPF` application? – dhilmathy Aug 10 '18 at 12:44
  • WindowsForms and yes closing on buttonclick –  Aug 10 '18 at 12:48
  • Try accessing the position before window is getting closed. If you can share your C# code part, it'll be easier for getting a correct answer. – dhilmathy Aug 10 '18 at 12:50
0

If your project is in WindowsFormApplicaiton then

If your control is button like in screen shot

enter image description here

Then you can access its location wrt to its primary screen also and form screen also

Below is the code

private void button1_Click(object sender, EventArgs e)
{
    //This code gives you the location of button1 wrt your primary working screen
    Point location = this.PointToScreen(button1.Location);
    int x1 = location.X;
    int y1 = location.Y;

    MessageBox.Show($"X: {x1}, Y: {y1}");


    //This code gives you the location of button1 wrt your forms upper-left corner        
    Point relativeLoc = new Point(location.X - this.Location.X, location.Y - this.Location.Y);
    int x2 = relativeLoc.X;
    int y2 = relativeLoc.Y;

    MessageBox.Show($"X: {x2}, Y: {y2}");


    //This code gives you the location of button1 wrt your forms client area
    Point relativeLoc1 = new Point(button1.Location.X, button1.Location.Y);
    int x3 = relativeLoc1.X;
    int y3 = relativeLoc1.Y;

    MessageBox.Show($"X: {x3}, Y: {y3}");
}

In above code I used this, you can use any forms object instead as your need

Edit:

If you don't know where your control will be clicked then you have to register one event for all of your controls inside forms like

In below code i used MouseHover event but you can used any event as your need

First of all register MouseHover event for all of your controls inside Form1_Load like

private void Form1_Load(object sender, EventArgs e)
{
    foreach (Control c in this.Controls)
        c.MouseHover += myMouseHoverEvent;
}

Then your custom MouseHover event is

private void myMouseHoverEvent(object sender, EventArgs e)
{
    Control control = sender as Control;

    int x = control.Location.X;
    int y = control.Location.Y;

    MessageBox.Show($"X: {x}, Y: {y}");
}

Try once may it help you

er-sho
  • 9,581
  • 2
  • 13
  • 26
0

This code should solve your problem if you let this event handler handle the MouseClick event of all the controls you are interested in. If you want to include the position inside the clicked control. Then add the position included in the mouse event args (Se comment in code).

private void control_MouseClick(object sender, MouseEventArgs e)
{
        Control control = sender as Control;
        int posX = control.Location.X;
        int posY = control.Location.Y;
        //Add e.X respectivly e.Y if you want to add the mouse position within the control that generated the event.

        while (control.Parent != null)
        {
            control = control.Parent;
            posX += control.Location.X;
            posY += control.Location.Y;
        }

        ((Label)sender).Text = "X: " + posX + " Y: " + posY;
}