Click here to Skip to main content
15,885,278 members
Please Sign up or sign in to vote.
1.00/5 (5 votes)
See more:
Tell me procedure to handle a event on mouse click
Posted
Updated 2-Nov-12 18:59pm
v2
Comments
Sergey Alexandrovich Kryukov 2-Nov-12 23:08pm    
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.
--SA

1 solution

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:
C#
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.

—SA
 
Share this answer
 
v2
Comments
ridoy 3-Nov-12 7:04am    
+5
Sergey Alexandrovich Kryukov 3-Nov-12 18:10pm    
Thank you,
--SA

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



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900