Hey there, whoever whose reading this, I'd like to know how to obtain the coordiantes when a user clicks the form in relation to the form, meaning if the top left hand corner of the window is clicked, it should be x=0, y=0...
I should think this is an easy problem to solve, but I'm just a beginner... Could anybody lend a hand? :p

currently I'm using:

            Point p = Main_Window.MousePosition;
            label1.Text = "X: " + p.X;
            label2.Text = "Y: " + p.Y;

Which only shows me the coordiantes in relation to the computer screen.

Cheers!

Recommended Answers

All 7 Replies

"Main_Window" is the name of my form, just in case anyone is wondering.

See the PointToClient method. In this case, Main_Window is the control.

What you showed was an event list. What gusano79 meant was that a control(e.g. a form) has a PointToClient method Did you follow the example in his link completely?

Oh yeah I misinterpreted him.
Even then I still do not know how to use the method, despite reading up the article from the link.

This is what I came up with to test out if it's working.

            Point P = PointToClient(new Point(e.X, e.Y));
            label1.Text = "X: " + PointToClient(p.X);
            label2.Text = "Y: " + PointToClient(p.Y);

I do not think it's supposed to be new point e but I couldn't figure out what else could be used.

Could you tell me what am I missing out on?

You could implement one or both of these mouse events of the form:

       private void Form1_MouseMove(object sender, MouseEventArgs e)
        {
            Xlbl.Text = "X: " + e.X.ToString();
            Ylbl.Text = "Y: " + e.Y.ToString();
        }

        private void Form1_MouseClick(object sender, MouseEventArgs e)
        {
            Xlbl.Text = "X: " + e.X.ToString();
            Ylbl.Text = "Y: " + e.Y.ToString();
        }

The e.X and e.Y are already "PointToCliented"
ToString is needed because else you cannot append an integer to a string.

The code for MouseMove worked like a charm... Thanks for your help, Daniel! :)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.