Solved

Adding an Event handler Dynamically

Posted on 2012-03-28
6
491 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

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.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Crystal reports vb.net 2 41
Creating a route in asp.net webforms 2 30
Regular expression help 2 25
How to open form using item in Listbox. 8 21
More often than not, we developers are confronted with a need: a need to make some kind of magic happen via code. Whether it is for a client, for the boss, or for our own personal projects, the need must be satisfied. Most of the time, the Framework…
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…
This video shows how to quickly and easily add an email signature for all users on Exchange 2016. The resulting signature is applied on a server level by Exchange Online. The email signature template has been downloaded from: www.mail-signatures…
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…

832 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