PC Review
Forums
Newsgroups
Microsoft DotNet
Microsoft Dot NET Framework Forms
Re: How to add eventHandlers at runtime?
Forums
Newsgroups
Microsoft DotNet
Microsoft Dot NET Framework Forms
Re: How to add eventHandlers at runtime?
![]() |
Re: How to add eventHandlers at runtime? |
|
|
Thread Tools | Rate Thread |
|
|
#1 |
|
Guest
Posts: n/a
|
Do you have any sample code for this? I'm looking for the same thing, but
the samples don't show how to do this dynamically. "Mattias Sjögren" <mattias.dont.want.spam@mvps.org> wrote in message news:e0%234KEUbDHA.4020@tk2msftngp13.phx.gbl... > >How to attach a delegate to a Control's event(Lets say click) dynamically I > >am trying to use EventInfo Clas but in AdEventHandler method takes 2nd > >parameter as delegate so how can i attch an method to be calledto that > >delegate object. > > You can use Delegate.CreateDelegate() to create the delegate for your > handler method. > > > > Mattias > > -- > Mattias Sjögren [MVP] mattias @ mvps.org > http://www.msjogren.net/dotnet/ > Please reply only to the newsgroup. |
|
|
|
#2 |
|
Guest
Posts: n/a
|
Scott,
>Do you have any sample code for this? I'm looking for the same thing, but >the samples don't show how to do this dynamically. using System; public delegate void MyDelegate(); class EventProvider { public event MyDelegate MyEvent; public void RaiseIt() { if ( MyEvent != null ) MyEvent(); } } class Listener { public static void Handler() { Console.WriteLine( "Event handler called" ); } public static void Main() { EventProvider ep = new EventProvider(); Delegate d = Delegate.CreateDelegate( typeof(MyDelegate), typeof(Listener), "Handler" ); ep.GetType().GetEvent( "MyEvent" ).AddEventHandler( ep, d ); ep.RaiseIt(); } } Mattias -- Mattias Sjögren [MVP] mattias @ mvps.org http://www.msjogren.net/dotnet/ Please reply only to the newsgroup. |
|
|
|
#3 |
|
Junior Member
|
Hi
For dynamic binding of an event handler, you can use ControlName.Event+=this.System.EventHandler(this.MethodName); Just like cmdButton.click+=this.system.EventHandler(this.Button_click); |
|
|
|
![]() |
|
| Thread Tools | |
| Rate This Thread | |
|
|

Main Page 

