tags:

views:

118

answers:

4

Currently my mousedown on the form will give me the x,y cords in a label. This label though when I click on it, I do not receive the mousedown. But when I put the code into the mousedown for the label, it gives the the cords based on the origin of the label and not the entire form.

My goal is to be able to detect x,y anywhere in the form. Even if it is on a label, button.

Thanks in advance.

+3  A: 
protected override void OnMouseMove(MouseEventArgs mouseEv) 
{ 
    txtBoxX.Text = mouseEv.X.ToString(); 
    txtBoxY.Text = mouseEv.Y.ToString(); 
} 
Ruel
This is for onmousemove. I require click the mouse button down, to capture at that very x,y cordinate. But also be able to click inside of a label and get the x,y of the overall form.
RoR
+2  A: 

You can adjust by this.Location .
or use this.PointToClient(Cursor.Position) on form and each control.

pinichi
not quite sure what you mean adjust this.Location. I tried using the Location e.Location.X.ToString() but gives me the exact same answer as e.X.ToString(). What is Location?
RoR
Sorry because of my unclearly answer. I wanted to mean the Location of Control (you can use Left, Top). From control's mouse down event you can use: this.Location.X + e.X to obtain relative to form. In second way, you can use this.PointToClient(Cursor.Position) on Form mouse down, and each for every control you have, you can use a loop to attach event handle like JP Alioto suggested.
pinichi
+3  A: 

Seems a bit of a hack but ...

public partial class Form1 : Form
{
    public Form1()
    {
        InitializeComponent();

        foreach (Control c in this.Controls)
        {
            c.MouseDown += ShowMouseDown;    
        }

        this.MouseDown += (s, e) => { this.label1.Text = e.X + " " + e.Y; };

    }

    private void ShowMouseDown(object sender, MouseEventArgs e)
    {
        var x = e.X + ((Control)sender).Left;
        var y = e.Y + ((Control)sender).Top;

        this.label1.Text = x + " " + y;
    }
}
JP Alioto
So this basically goes through each of the Controls, like checkboxes, labels and anything and then c.MouseDOwn += ShowMouseDown; is taking the ShowMouseDown method and putting it into the C.MouseDown. I don't quite understand what htis line is doing, this.MouseDown += (s, e) => { this.label1.Text = e.X + " " + e.Y; };
RoR
I am new to C#, I apologize. Also what is object sender? Is this the name of the control? What is (Control)sender? Thank you for your time.
RoR
+2  A: 

You can get the location like this this.PointToClient(Cursor.Position) on form for each control.