Click here to Skip to main content
13,146,014 members (43,151 online)
Rate this:
Please Sign up or sign in to vote.
See more:
Tell me procedure to handle a event on mouse click
Posted 2-Nov-12 16:26pm
Updated 2-Nov-12 18:59pm
What do you mean by "do event"? You don't do the event, hardware does. You can only handle the event.
But first of all, you need to tag your application type or UI library you use.

1 solution

Rate this: bad
Please Sign up or sign in to vote.

Solution 1

Please see my comment to the question: you did not specify what UI library you will use so, sorry, no exact references. You will easily find them by yourself. In short, you will need to add a event handler to an invocation list of some event of some control. It can look like that:
Control myControl = // some concrete control type
Button myButton = //...
myControl.MouseDown += (sender, eventArgs) => { DoItWhenMousePressedDown(); };
myControl.MouseUp += (sender, eventArgs) => { DoItWhenMouseReleased(); };
myControl.MouseMove += (sender, eventArgs) => { DoItWhenMouseMoved(eventArgs.X, evenArgs.Y); };
myButton.Click += (sender, eventArgs)  => { DoItWhenButtonClicked(); };

This way, your methods (which you can define by yourself, or you can write some other code in "{ }" blocks shown above) DoItWhenMousePressedDown, DoItWhenMouseReleased, DoItWhenMouseMoved or DoItWhenButtonClicked will be called when the user operates mouse (or keyboard, in case of Click in correspondent way over your control.

Note that the Click event is more abstract, not directly related to hardware: it will be triggered by mouse click, be Enter or spacebar key, depending on keyboard focus.

The parameter eventArgs will carry additional information: mouse coordinates, which mouse button clicked, etc. You will need to learn if all from the MSDN help pages on relevant events. This part is different for different .NET UI libraries (again, why didn't you tag it? it's very important; and who will waste time for explaining it for all different libraries?), but there a lot of very similar feature.

So far, this is just a cookbook recipe. This is not good enough for you unless you learn very thoroughly how events work. This is very important. You should also understand very well the delegates and anonymous methods.

ridoy 3-Nov-12 7:04am
Thank you,

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month

Advertise | Privacy |
Web03 | 2.8.170915.1 | Last Updated 3 Nov 2012
Copyright © CodeProject, 1999-2017
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100