Archived

This topic is now archived and is closed to further replies.

RandomXT

C# On Mouse Click (HELP)

Recommended Posts

RandomXT    122
Hi I just switched to C# from VB6.0 and in VB I could create simple functions such as: Form1_On_Mouse_Click(blah blah button, x, y){ label1.text = x; } and it would update the label to the x pos of the mouse everytime a button was clicked. How do I do this in C# it seems all I read everywhere is about the windows event loop and how I have to use messages such as WM_Mouse something... Why can''t I do it the other way.... Please help Same with Keyboard... I would like to be able to say If keyboard_uparrow_pressed then picture.x++; or something Please Help Thanks RandomXT

Share this post


Link to post
Share on other sites
Zipster    2365
Forms have a pretty good set of event handlers. I''d suggest you read up on events first, so you have a better idea on how they operate.

If you have a form object (we can call it Form1), and you want to handle the mouse click event, you first need to create an event handler function. It looks like this:

public void MyClickHandler(object sender, EventArgs e)
{
// My code here...
}

Unfortunately, EventArgs is an empty base class that other events can derive from for their custom arguments. That means you still have to get the mouse position manually. This shouldn''t be too difficult using Cursor.Position and PointToClient though, which get the screen position of the cursor and convert it to client coordinates, respectively.

Then, preferably in InitializeComponent, the event handler can be added to the form like this:

Form1.Click += new EventHandler(MyClickHandler);


If you used the form designer, then the function prototype and the addition of the handler should be created automatically. In the form properties window, you should see a little yellow lighting icon, where all the event handlers are kept. Double-clicking on any of the property boxes for events will generate what you need. All you do is fill in the code.

The same applies for keyboard events, however you get a KeyEventArgs object that contains key information.

Share this post


Link to post
Share on other sites
Guest Anonymous Poster   
Guest Anonymous Poster
The MouseDown and MouseUp event args contain cursor position information.

Share this post


Link to post
Share on other sites
Zipster    2365
You could use MouseDown and MouseUp, but those are more for if you want to customize mouse clicking/dragging behavior (hence the coordinates, so you can calculate deltas). MouseClick better replicates the clicking behavior you''d expect. For example, if you click a button control, but move the mouse and release it outside the button, that doesn''t count as a click (what you''d expect using MouseClick), where MouseDown would register that.

It''s up to you, though. I''m just saying that in terms of your user interface, MouseClick will be better for users that expect certain behavior from Windows controls.

Share this post


Link to post
Share on other sites