Click here to Skip to main content
Sign Up to vote bad
good
See more: C#
Tell me procedure to handle a event on mouse click
Posted 2 Nov '12 - 16:26
Edited 2 Nov '12 - 18:59
_Amy38.5K

Comments
Sergey Alexandrovich Kryukov - 2 Nov '12 - 23:08
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:
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  
Comments
ridoy - 3 Nov '12 - 7:04
+5
Sergey Alexandrovich Kryukov - 3 Nov '12 - 18:10
Thank you, --SA

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

  Print Answers RSS
Your Filters
Interested
Ignored
     
0 Sergey Alexandrovich Kryukov 435
1 OriginalGriff 315
2 Arun Vasu 293
3 CPallini 213
4 Zoltán Zörgő 194
0 Sergey Alexandrovich Kryukov 10,005
1 OriginalGriff 7,654
2 CPallini 4,171
3 Rohan Leuva 3,447
4 Maciej Los 2,974


Advertise | Privacy | Mobile
Web04 | 2.6.130523.1 | Last Updated 3 Nov 2012
Copyright © CodeProject, 1999-2013
All Rights Reserved. Terms of Use
Layout: fixed | fluid