Click here to Skip to main content
15,885,216 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Hello,
I have designed a calendar user control.
It generates a specific month, and all the dates are buttons.

This user control, when you click on the button (which is 1,or,2..29 or 30 or 31)
you write in an appointment, or something.
But i dont know how to make an event handler on run time, it it possible ? how to do it ?
Posted

Every event is an event at runtime because it will only be executed at runtime based on a certain condition for example when a control is clicked eg;
button1.Click += new EventHandler(button1_Click);

If you want to handle certain conditions when a control is clicked, you would do this in the body of the events method.

http://msdn.microsoft.com/en-us/library/awbftdfh.aspx[^]

http://msdn.microsoft.com/en-us/library/aa645739%28v=vs.71%29.aspx[^]

Delegates and Events[^]
 
Share this answer
 
Comments
csharpnew 1-Apr-12 14:46pm    
But i can have buttom31.Click+= sometimes,,
and sometimes i will not have it , you know,,..
how to solve this?
Dean Oliver 1-Apr-12 14:50pm    
Why not rather enable and disable the use of that particular control that the user interacts with? or assign all 3 of you buttons to one click event and handle what will happen when each one is clicked based on the name that is passed in as the sender in the body of the one event method.
Sergey Alexandrovich Kryukov 1-Apr-12 15:49pm    
Sometimes, this is not possible or makes no sense. More generally, one could have some flag like allowEvent and check this flag in the event handler. This flag could be calculated depending on all those factors of the state.
--SA
csharpnew 1-Apr-12 15:10pm    
but this will be user control, and when i drag and drop it to the form.
Will it work ?
I will handle all my events inside user control.. (so i think it will work )
And can you please write me a small code..plz
i better understsand with code,
thanks
Sergey Alexandrovich Kryukov 1-Apr-12 15:55pm    
Right, this needs some explanation.
Please see my detailed answer on this page -- should be useful.
--SA
Exposing events of the user control to the user, especially to the designer is a simple problem, but many developers have problems with it and ask questions. I have a comprehensive answer I produced recently. Please see:

A question about usercontrols, nested controls and encapsulation [^].

See also the fruitful discussion with OP (Naerling). It was an excellent and important question, so we both spent good time discussing all the detail.

—SA
 
Share this answer
 
Part of your question was if you could make an event handler at design time. Normally all you need to do create an event handler, which is really just a method that matches the signature of the event delegate:

private void Handler (object sender, EventArg arg)


The sender argument is a great feature that is included in .NET that allows the class that sent the event to be determeined. Normally you would want to cast it to the type you know it is, probably a button:

Button b = (Button) sender;


If you are handling events for more than one type and need to use the Is operator to determine the type.

Having this cast variable allows properties of the sender to be determined so that the evant handler can do what it should for the event.

The other answers provide information on how to attach an event to this event handler. Howerver, you do not need to specify the
new EventHandler 
anymore (feature not in the initual releases of C#), all you need is:

Button.Click += Handler;


There is a way to actually dynamically create an event handler, but that requires some beta software code named Roslyn to be installed.
 
Share this answer
 
v2

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