Skip to content

Wanna get the coordinates where i click on the form

Raheel Imtiaz

Asked Jan 1, 2006, 8:20 AM EST3 answers
Share
LinkedInX
Hi

i am making a chess game as a project in my OOP course. so the first thing to start with will be to get the coordinates of the form where i click on it. it will help me a lot to start making the movement functions coz i would have the coordinates of the point from where i'll pick up any piece and to the place where i wannna put it.

anyone who knows how to do this please help!

3 Answers

Know the answer? Post it. Somebody with the same question will find it here.

  • 0
    You must make event handler, that takes object and MouseEventArgs as parameters, and register it with your forms OnClick event. MouseEventArgs object has mouse position info you need, as well information about the button pressed.
    0
  • 0
    thanks for giving me that advice, it is good, your approach seems more logical to me. i really did like but can you tell me in detail about how to register my event handler with the forms onclick event
    0
  • 0
    I don't quite think that is right, the click event on the form does not expose the MouseEventArgs.

    The event you need is the mousedown event. It is very simple to set this up. I will show you the code that is needed in case you need it:-

    in the constructor/initialise component:-

    this.MouseDown+=new MouseEventHandler(Form1_MouseDown);

    and the event handler

    private void Form1_MouseDown(object sender, MouseEventArgs e)

    {

    if(e.Button == MouseButtons.Left)
    {
    MessageBox.Show(String.Concat(
    "Location of mouse when LEFT button is pressed:- ",
    e.X, ", ", e.Y));
    }

    }

    0