Assign Event handlers dynamically?
Messages   Related Types
This message was discovered on ASPFriends.com 'winforms-cs' list.


Jeff Woodie
I'm trying to convert an existing app, and for some reason the original app
uses a menuing system that is stored in a database. Probably works great
for the existing app, but in the .NET/C# app that I'm doing, I have a
problem.

Basically in the database, all the menu items are stored, along with a
parent item, caption, and what to do on the Click event (this is FoxPro).
I've turned this into a SQL table and I can create the MainMenu that matches
the menu structure just fine. What I can't do is assign the EventHandler
delegate for each Click event. I'm not even sure if it's possible, as all
code samples (and the object interfaces) expect something like this:

menuItem1.Click += new System.EventHandler(this.someMethod);

What I really want to do is this:
string methodName = "this.File_Exit_Click";
menuItem1.Click += new System.EventHandler(methodName);

that way, I could create that string out of the dynamic values in the
database, and knowing that naming convention, just create the correct
functions to match in the code.

Is there anyway to make a variable out of "this.someMethod"? Seems to only
take a literal value there. I've traced up through the object model for
Events/Delegates and it doesn't appear so, but I thought I'd give it one
last shot here before I called it off. I've thought about creating a
delegate object explicitly and trying to assign the right values to it, but
the object model doesn't match what I want to do. Seems like a nice thing
to be able to do, to completely create a menu dynamically, but I can't seem
to get it right. I could create all of these Event Handler links explicitly
in the code and do everything else dynamically, but that seems kinda silly.

Anyway, any help is much appreciated.

Jeff Woodie

Reply to this message...
 
    
Ben Licht
Hey Jeff,

try the following:

Delegate temp =
Delegate.CreateDelegate(typeof(EventHandler),this,"Method2");
button1.Click += (EventHandler)temp;

In the above, I attached this to a button on the form. The form is the
"this" object specified by line one. Obviously, you'd replace "Method2"
with the name of the method you wish to call.

Hope this helps,

Ben
----- Original Message -----
From: "Jeff Woodie" <Click here to reveal e-mail address>
To: "winforms-cs" <Click here to reveal e-mail address>
Sent: Friday, May 24, 2002 12:54 AM
Subject: [winforms-cs] Assign Event handlers dynamically?

> I'm trying to convert an existing app, and for some reason the original
app
[Original message clipped]

Reply to this message...
 
    
Per Erik Lorentzen
The code should be something like this:
menuItem1 =3D new System.Windows.Forms.MenuItem();

string methodName =3D "File_Exit_Click"; // Fetch this string from a =
Table

Delegate clickDelegate =3D Delegate.CreateDelegate(typeof(EventHandler), =
this, methodName);

menuItem1.Click +=3D (EventHandler)clickDelegate;

The method=20
private void File_Exit_Click(object sender, System.EventArgs e)

resides in the same class. If it should reside in another class, replace =
this with the appropriate object.

Med hilsen
Per Erik Lorentzen
SK Data as
Click here to reveal e-mail address

----- Original Message -----=20
From: "Jeff Woodie" <Click here to reveal e-mail address>
To: "winforms-cs" <Click here to reveal e-mail address>
Sent: Friday, May 24, 2002 6:54 AM
Subject: [winforms-cs] Assign Event handlers dynamically?

> I'm trying to convert an existing app, and for some reason the =
original app
> uses a menuing system that is stored in a database. Probably works =
great
[Original message clipped]

Reply to this message...
 
    
selva raj
hi,

i want assign event handlers dynamically by using asp.net with code behind. i am using following coding but the event is not fire.

Public Sub bn_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles bn.Click
Response.Write("Hai")
End Sub

hell me
selva.
Reply to this message...
 
 
System.Delegate
System.EventArgs
System.EventHandler
System.Object
System.Windows.Forms.MainMenu
System.Windows.Forms.MenuItem




Ad
MBR BootFX
Best-of-breed application framework for .NET projects, developed by Matthew Baxter-Reynolds and MBR IT
 
 Copyright © Matthew Baxter-Reynolds 2001-2008. '.NET 247 Software Development Services' is a trading style of MBR IT Solutions Ltd.
Contact Us - Terms of Use - Privacy Policy - www.dotnet247.com