473,835 Members | 1,737 Online
Bytes | Software Development & Data Engineering Community
+ 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.Cli ck += System.EventHan dler(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.Cli ck += System.EventHan dler(bMethod);
I understand the argument to 'System.EventHa ndler' 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 2663
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.Create Delegate(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.co m
"Jim Witt" <JW***@BayTechE ng.com> wrote in message
news:1067868183 .801428@rh9cach e2...
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.Cli ck += System.EventHan dler(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.Cli ck += System.EventHan dler(bMethod);
I understand the argument to 'System.EventHa ndler' 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
1836
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. I want to dim various textboxes with different names depending on what I retrieve from SQL. I tried the following: dim dtrcondet.item("contact_detail_description") as new textbox() but this does not work
9
2638
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 more OO approach to fit my needs. Altough I used an OO approach previously for some subparts of the library it became somewhat difficult to maintain all those parts since they really are the same thing coded for each part (code duplication). So...
5
3765
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 believe the problem centers around my handling of strings, arrays, pointers and dynamic memory allocation. Here is the problem I'm trying to solve: I want to fill a list box with a list of Project Names from a database (in Palm this is more...
3
3989
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 step you're on. This all works fine, but I ran into some trouble when I started creating controls dynamically in my code-behind file. Each panel contains a table which is filled with various radio buttons, text fields and the such which are...
4
2022
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") > .... or is there a way to declare a generic using strings that hold class
0
9802
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9652
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
1
10558
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10226
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9343
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
6961
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 then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
1
4430
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 we have to send another system
2
3990
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
3086
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 can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.