Sign in to follow this  

C# Mouse Input

This topic is 4343 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I am pretty brand new to C#, and I have been trying to understand how to use the input, but I am just not getting it. I see functions that handle the mouse input, I just have no idea where it gets called. Something like this:
private void Form1_MouseDown(object sender, System.Windows.Forms.MouseEventArgs e)
{
	labelMousePosition.Text = "Mouse Position: X: " +  e.X + " Y: " + e.Y;
}

The code in this function should do what I want it to do, I just do not know how to have the program actually use this function. -Chris

Share this post


Link to post
Share on other sites
There are two basic ways that your method can be called.

If you are subclassing a Control or Form you can override one of the OnMouseXXX() methods to capture the mouse event:
protected override void OnMouseDown(MouseEventArgs e){
//do something with e.
}

If you are not subclassing, or don't feal like playing with overrides you can add your method to one of the control's events usint the += operator:
yourControl.MouseDown += Form1_MouseDown;

If you are using an IDE typically it will handle this for you setting up the method and adding it to the event when you double click the event in the editor.

The event will only be fired by the control that you add your handler to so be careful to add it to the right one.

Share this post


Link to post
Share on other sites

This topic is 4343 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this