473,480 Members | 1,536 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

Dynamic Method names

Ref: Win2000, .NET 1.1

I want to place series of buttons on a form, my own button class. Would
like to create descriptor array to describe buttons - each needs: text,
x and y position, and method invoked when button clicked.

I've been able to create descriptor with 'text, x, y' just fine (no
brainer). However, I designate button click method as follows:

this.button.Click += System.EventHandler(MyMethod);

public void MyMethod()
{
... code
}

This works fine.
Now, I don't know how to deal with is situation like following, which is
what I need to do:
string bMethod = "MyMethod";

this.button.Click += System.EventHandler(bMethod);
I understand the argument to 'System.EventHandler' cannot be string. But
I don't understand Delegates and Interfaces well enough to know how to
convert string to proper type.

Thanks,

Jim
Nov 15 '05 #1
1 2629
Jim,

You don't need interfaces. Assuming that you have the name of the
method and a reference to the object the method is on, you can create a
delegate using the static CreateDelegate method on the Delegate class.

// Create the delegate for the button click event.
// pobjObject is the object that the method in "bMethod" is on.
EventHandler pobjHandler = (EventHandler)
Delegate.CreateDelegate(typeof(EventHandler), pobjObject, bMethod);

Once you have that, you can add it to the Click event as you would
normally.

Hope this helps.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com
"Jim Witt" <JW***@BayTechEng.com> wrote in message
news:1067868183.801428@rh9cache2...
Ref: Win2000, .NET 1.1

I want to place series of buttons on a form, my own button class. Would
like to create descriptor array to describe buttons - each needs: text,
x and y position, and method invoked when button clicked.

I've been able to create descriptor with 'text, x, y' just fine (no
brainer). However, I designate button click method as follows:

this.button.Click += System.EventHandler(MyMethod);

public void MyMethod()
{
... code
}

This works fine.
Now, I don't know how to deal with is situation like following, which is
what I need to do:
string bMethod = "MyMethod";

this.button.Click += System.EventHandler(bMethod);
I understand the argument to 'System.EventHandler' cannot be string. But
I don't understand Delegates and Interfaces well enough to know how to
convert string to proper type.

Thanks,

Jim

Nov 15 '05 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

7
1818
by: Mark | last post by:
I want to create textboxes dynamically but with dynamic names also. i am retrieveing a load of values from a table in SQL into a DataReader. Then i want to create textboxes from those variables. ...
9
2603
by: Gibby Koldenhof | last post by:
Hiya, Terrible subject but I haven't got a better term at the moment. I've been building up my own library of functionality (all nice conforming ISO C) for over 6 years and decided to adopt a...
5
3736
by: swarsa | last post by:
Hi All, I realize this is not a Palm OS development forum, however, even though my question is about a Palm C program I'm writing, I believe the topics are relevant here. This is because I...
3
3943
by: Leo J. Hart IV | last post by:
OK, here's another question for the experts: I am building a multi-step (3 steps actually) form using a panel for each step and hiding/displaying the appropriate panel/panels depending on which...
4
2005
by: =?Utf-8?B?SGF5U2VlZA==?= | last post by:
Is there some way to use Generics in dynamic code using the Type.GetType("MyClassName") as an argument? List<Type.GetType("MyClassName") oList = new List<Type.GetType("MyClassName") > ...
0
6908
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
7045
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
1
6741
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
6944
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
4483
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
2985
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1300
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
1
563
muto222
php
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
182
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.