Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C#
Tell me procedure to handle a event on mouse click
Posted 2-Nov-12 16:26pm
Edited 2-Nov-12 18:59pm
_Amy51K
v2
Comments
Sergey Alexandrovich Kryukov at 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

Rate this: bad
good
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.
 
—SA
  Permalink  
v2
Comments
ridoy at 3-Nov-12 7:04am
   
+5
Sergey Alexandrovich Kryukov at 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)



Advertise | Privacy | Mobile
Web01 | 2.8.141022.2 | Last Updated 3 Nov 2012
Copyright © CodeProject, 1999-2014
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