Solved

Adding an Event handler Dynamically

Posted on 2012-03-28
6
492 Views
Last Modified: 2012-06-22
Hi

I have coded a form contents that add controls in it at run time dynamically, and now I want to add event handlers (such as on-click event) to some of those dynamically-added controls at run time, how do I do that?
0
Comment
Question by:alfardan
6 Comments
 
LVL 17

Expert Comment

by:sonawanekiran
ID: 37780491
You can do this like

Button cmd = new Button();
cmd.Text = "Click Me";
//Attach click event handler
cmd.Click += new EventHandler(MyMethod);

protected void MyMethod(object sender, EventArgs e)
    {
       
    }
0
 
LVL 17

Accepted Solution

by:
sonawanekiran earned 250 total points
ID: 37780495
Here is VB.net code


Dim cmd As New Button
cmd.ID = "btnDynamicButton"
cmd.Text = "Click Me"
AddHandler cmd.Click, AddressOf Dynamic_Method
0
 
LVL 20

Expert Comment

by:BuggyCoder
ID: 37780519
0
Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

 

Author Comment

by:alfardan
ID: 37780620
Great

Now in my method itself, how would I know which control was clicked in order to action the clicked event accordingly.

For example, if I clicked on a control that has the name "Label1" then I will let the system execute a certain code, and if another control with the name "Picture7" then I will execute another code.

Note: the dynamically-created controls at run time differ in type between Picture Box and Label control, and at their creation time dynamically, they were given unique names
0
 
LVL 40

Assisted Solution

by:Jacques Bourgeois (James Burger)
Jacques Bourgeois (James Burger) earned 250 total points
ID: 37780712
The sender parameter in the event points to the Control that calls the event.

If the control variable is visible in the event, you can check the following way.

If sender Is Label1 then
   'Code for Label1
End if

Otherwise, you will have to find another way to identify the Control

If DirectCast(sender,Control).Text = "Text on the control"
If DirectCast(sender, Control).Name = "NameOfTheControl"

If you want to go by the name, be sure to spedify It. The Name property of controls you add dynamically is not filled in automatically.
0
 
LVL 20

Expert Comment

by:BuggyCoder
ID: 37780784
The control that was clicked will have its event handler called...

I mean if button1 is clicked then button1_clicked would get called which will have sender parameter on which if you do gettype you will get an instance of Button class....

Since you know the event handlers you have attached to these controls, you need not to check for the control in the event handler as you can statically  and safely use the control name there....

private void Button1_click(object sender, EventArgs args)
{
Button b = (Button)sender;
}

private void CheckBox1_click(object sender, EventArgs args)
{
CheckBox b = (CheckBox)sender;
}

Open in new window

0

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

A long time ago (May 2011), I have written an article showing you how to create a DLL using Visual Studio 2005 to be hosted in SQL Server 2005. That was valid at that time and it is still valid if you are still using these versions. You can still re…
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
Microsoft Active Directory, the widely used IT infrastructure, is known for its high risk of credential theft. The best way to test your Active Directory’s vulnerabilities to pass-the-ticket, pass-the-hash, privilege escalation, and malware attacks …
Two types of users will appreciate AOMEI Backupper Pro: 1 - Those with PCIe drives (and haven't found cloning software that works on them). 2 - Those who want a fast clone of their boot drive (no re-boots needed) and it can clone your drive wh…

839 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question