Solved

Adding an Event handler Dynamically

Posted on 2012-03-28
6
489 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
Comment Utility
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
Comment Utility
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
Comment Utility
0
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 

Author Comment

by:alfardan
Comment Utility
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
Comment Utility
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
Comment Utility
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

Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

Join & Write a Comment

Suggested Solutions

In my previous two articles we discussed Binary Serialization (http://www.experts-exchange.com/A_4362.html) and XML Serialization (http://www.experts-exchange.com/A_4425.html). In this article we will try to know more about SOAP (Simple Object Acces…
This document covers how to connect to SQL Server and browse its contents.  It is meant for those new to Visual Studio and/or working with Microsoft SQL Server.  It is not a guide to building SQL Server database connections in your code.  This is mo…
Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.
When you create an app prototype with Adobe XD, you can insert system screens -- sharing or Control Center, for example -- with just a few clicks. This video shows you how. You can take the full course on Experts Exchange at http://bit.ly/XDcourse.

772 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

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now