Click here to Skip to main content
16,023,224 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Ok I am new to programming C# and I know this is simple to most of you but I am learning and couldn't find an answer out there. Here is the problem. I have a form and has bunch of objects on it like labels, picturebox and etc. I need to get the mouse coordinates and I am able to get it using
private void TestApp_MouseClick(object sender, MouseEventArgs e)
        {

            Point p = new Point(e.X, e.Y);
            Point p2 = PointToClient(p);

            Clipboard.SetText("X = " + p2.X + ", Y = " + p2.Y);
        }

and copy it a clipboard. The problem is this doesn't give me the coordinates when it is on an object :( I want the coordinates as (0,0) top left of the form. If some one can help me out I would really appreciate it.

Thank you,
Posted

There are two things to address.

First, I think you'll have some trouble with that code as PointToClient is meant to work with cursor location relative to the screen.

You might want this instead (which will work on multiple monitors, from any control):
private void TestApp_MouseClick(object sender, MouseEventArgs e)
{
    Point p2 = PointToClient(Cursor.Position);
    string coords = string.Format("({0},{1})", p2.X, p2.Y);
    Clipboard.SetText(coords);
}


Secondly, you'll need an event handler for each of the controls on your form, or one event handler to handle them all. Go with the second one...here's how:

    • Go to your form designer, click in the form (not on a control) and press CTRL-A. This will select all controls.

    • In your properties window, click on the lightning bolt to see the events.

    • Click in the MouseClick event to get the drop down. It should contain the name of the function you've already written.

    • Select the function you've written!



If you want to see what Visual Studio did for you, go to the Form.Designer.cs file in Project Explorer. It will have added a line like the following for each of your controls:
this.button1.MouseClick += new System.Windows.Forms.MouseEventHandler(this. TestApp_MouseClick);


Basically, it wires up an event handler for you when you select a method from the drop down.

Cheers!
 
Share this answer
 
v4
Comments
TolgaCiftci 20-May-10 11:10am    
It worked great. Thank you soooo much. I was going crazy for the past couple of days :)
Hey! Don't worry! There is nothing bad in posting a question!

Your problem is this: when you click the mouse on an area of your form the Click event is sent to the form, but if you do it over a child control (Label, TextBox, etc.) the event is sent to that object, and if you want the parent form to catch the event you need to explicitly bind the event for each control.

On the designer select each control on your form and in the property window select Actions, then for each control add an handler for the Click event. By default the designer add a different handler for each control named controlName_Click, but you can set the same handler for all the controls by manually typing the handler name in the text-box on the right of the event name.
 
Share this answer
 
Comments
TolgaCiftci 20-May-10 10:31am    
So I basically create an empty click event?
Member 12600313 23-Jun-16 10:19am    
Can you translate this to Visual Basic? I'm trying to click on buttons in an external Windows application on a 2nd monitor, and haven't found a way to control mouse clicks on the 2nd monitor. So far, I've tried several ways to control mouse position and clicks but it only puts the mouse on the primary monitor.

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



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900