PC Review Forums Newsgroups Microsoft DotNet Microsoft Dot NET Framework Forms Re: How to add eventHandlers at runtime?

Reply

Re: How to add eventHandlers at runtime?

 
Thread Tools Rate Thread
Old 02-09-2003, 02:16 PM   #1
Scott Meddows
Guest
 
Posts: n/a
Default Re: How to add eventHandlers at runtime?


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.



  Reply With Quote
Old 06-09-2003, 09:07 PM   #2
Mattias Sjögren
Guest
 
Posts: n/a
Default Re: How to add eventHandlers at runtime?

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.
  Reply With Quote
Old 14-07-2005, 10:12 AM   #3
Ammukamal
Junior Member
 
Join Date: Jul 2005
Posts: 1
Trader Rating: (0)
Default

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);
Ammukamal is offline   Reply With Quote
Reply



Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off