eHow launches Android app: Get the best of eHow on the go.

How To

How to Capture the Mouse Click Event X and Y Coordinates in Microsoft Visual C# (C Sharp)

Member
By Derek Hardenbergh
User-Submitted Article
(0 Ratings)
Capture the Mouse Click Event X and Y Coordinates in Microsoft Visual C# (C Sharp)
Capture the Mouse Click Event X and Y Coordinates in Microsoft Visual C# (C Sharp)

Get the mouse's X and Y coordinates during a click event in Microsoft Visual C#.

Difficulty: Moderately Easy
Instructions

Things You'll Need:

  • Microsoft Visual C# 2008 Express (free)
  1. Step 1
     

    Note: This article assumes you have installed Microsoft Visual C# 2008 Express Edition. You may download it for free from here:

    http://www.microsoft.com/express/download/

    Open Microsoft Visual C#. Click on "Project..." to the right of Create in the Recent Projects area of the Start Page.

    The New Project window will open. Click on "Windows Forms Application", enter a Name, and click OK.

    By default, the only form in the project will be called "Form1" and you will be in Design mode for that form.

  2. Step 2
     

    The form will be displayed in the design view.

  3. Step 3
     

    Open the Properties window for the form. If you do not see the Properties window, go to the toolbar and select View->Properties Window.

  4. Step 4
     

    In the toolbar for the Properties window, click the button with the lightning bolt icon on it. This will show you the events for the form.

    Find the MouseClick event and double click the empty cell to the right.

  5. Step 5
     

    You will now be in the Form1_MouseClick event.

    Enter the following text:

    MessageBox.Show("X = " + e.X + ", Y = " + e.Y);

    This will cause a MessageBox to appear and the X and Y coordinates will be displayed in the box. Note that the second argument to the

    MouseClick event is of the type MouseEventArgs. This object has X and Y properties that return the X and Y coordinates in pixels.

  6. Step 6
     

    Go up to the toolbar and run the program by clicking on the Start Debugging (f5) play button.

    NOTE: If you got any kind of error after clicking the play button, you probably made a syntax error when typing the code. Reread the code

    until you find and correct the error and try again.

  7. Step 7
     

    The form will take a moment and then pop up.

    Click somewhere on the form and the message box will pop-up and tell you exactly where you clicked. Cool, huh?

Tips & Warnings
  • Play around with the Help. Tons of good stuff in there.
  • You might get addicted to C#. Watch out Java and C++ folks!!!
Subscribe

Post a Comment

Post a Comment

Related Ads

  • Have you done this? Click here to let us know.
I Did This
Get Free Computers Newsletters

Copyright © 1999-2010 eHow, Inc. Use of this web site constitutes acceptance of the eHow Terms of Use and Privacy Policy .   en-US Portions of this page are modifications based on work created and shared by Google and used according to terms described in the Creative Commons 3.0 Attribution License. † requires javascript

eHow Computers
eHow_eHow Technology and Electronics