Click here to Skip to main content
15,908,673 members
Please Sign up or sign in to vote.
3.00/5 (2 votes)
See more:
Hi all ,
I have one query regarding how to add event for controls in C#.
Suppose I want to write code in Mouse over event of label control .
I did double clicked on the lable but it opened the click event.
I have also tried the upper right event drop down list. but didn't found
any evnet it only contain that event which are in code below.

Please suggest me the way.
Posted
Updated 25-Apr-11 21:00pm
v2

Dear Rahul
You have two methods
Method 1) Use property window and select events, Double click the desired event; It will autogenerate the code and tthus you perfrom the necessary action
Method 2) Handle the even using code
In the window(form designer.cs) handle the necessary event ie
label1.MouseEnter +=new System.EventHandler(label1_MouseEnter);

now in the cs file add method
private void label1_MouseEnter(object sender, EventArgs e)

If you wan to create your own Event Handiling on a user control then refer below link
http://www.akadia.com/services/dotnet_delegates_and_events.html[^]

Thanks And Regards
Vipin Kumar Mallaya G
 
Share this answer
 
Comments
Sergey Alexandrovich Kryukov 26-Apr-11 13:28pm    
This is correct my 5, but...
Look how much the maintainability can be improved with the anonymous. Please also pay attention for your naming conventions.

Please see my answer.
--SA
On your Label control do:

// Get event, when mouse enter Label control
this.label_HelpText.MouseEnter += new System.EventHandler(this.HelpText_MouseEnter);


// Stop event when mouse enter Label control
this.label_HelpText.MouseEnter -= this.HelpText_MouseEnter;
 
Share this answer
 
v2
Comments
Sergey Alexandrovich Kryukov 26-Apr-11 13:29pm    
This is correct my 5, but...
Kim, this is correct, my 5, but...
Look how much the maintainability can be improved with the anonymous. Please also pay attention for your naming conventions.

Please see my answer.
--SA
Kim Togo 27-Apr-11 2:49am    
Thanks SA for the 5.
I can see that I have messed up some naming conventions, thanks SA for pointing that out.
I generally always tries to follow Design Guidelines for Developing Class Libraries (http://msdn.microsoft.com/en-us/library/ms229042.aspx) and Design Guidelines, Managed code and the .NET Framework (http://blogs.msdn.com/b/brada/archive/2005/01/26/361363.aspx)
In addition to other answers, I recommend to always prefer anonymous methods, even for a beginner. By many reasons, it's much more supportable. Also, names like "label_HelpText_MouseEnter" violate (good) Microsoft naming conventions. (Yes, even though the names are auto-generated by Microsoft; this is not a good excuse not to rename them).

So, use:
myLabel.MouseEnter += (sender, eventArgs) => {
    DoSomething((Label)sender);
};


The convenience of lambda expression show above is that you don't even need to specify types of the argument as they are inferred from the type if the event.

If C# v.2.0 is used, lambda expressions are not available. Nevertheless, use anonymous delegates in explicit form:
myLabel.MouseEnter += delegate(object sender, MouseEventArgs eventArgs) {
    DoSomething((Label)sender);
};


Why all this is more supportable? Because you don't have to follow full signature of the handler with both event arguments. Look at DoSomething — is uses only the label argument; the type cast is done in a "real" anonymous handler. In this way, you separate a handler from a semantic part of the handler.

Also, you can use multi-cast feature of the event by adding several different handlers in different places. It is very useful in many cases.

The code like the ones shown above can be called anywhere before the event is actually uses. With Forms, I prefer calling it in a single setup method called from the Form constructor at its end.

—SA
 
Share this answer
 
v2
Comments
Kim Togo 27-Apr-11 2:59am    
My 5. Really good answer. And lambda expression is very useful.
But the only problem with anonymous delegates, is that you cannot remove it from the event invocation list?

You can do " myLabel.MouseEnter += " but not " myLabel.MouseEnter -= "
Sergey Alexandrovich Kryukov 27-Apr-11 3:37am    
Than you, Kim.

It looks like you're right, this is a limitation. I don't remember myself removing from invocation list; to me this is a sign of a bad design. You can always have some flag like isHandlerEnabled (that would create a real closure if this is class instance member!).

Anyway, named methods are not gone, can still be used.
--SA
Michel [mjbohn] 27-Apr-11 3:11am    
And again I learned s.th from your answer. My 5
Sergey Alexandrovich Kryukov 27-Apr-11 3:37am    
Really? Well, thank you very much. My pleasure.
--SA
Hi,
there is a special guide on MSDN for this (How to subscribe and unsubscribe from the events):
http://msdn.microsoft.com/en-us/library/ms366768%28v=vs.80%29.aspx[^]
Regards
 
Share this answer
 
click on the label control and go to properties there you can find the list of events available for label,double click on any of the event you want and write the code in that event..mouse hover event is available for label..

hope it helps!!
 
Share this answer
 
Go to the properties for that label. You will see a "lightning" symbol as well.
Click on the symbol - you will see a list of all events you can subscribe to.

Hopefully, you will find all events you want there.
 
Share this answer
 

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