Solved

Adding an Event handler Dynamically

Posted on 2012-03-28
6
495 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
How our DevOps Teams Maximize Uptime

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us. Read the use case whitepaper.

 

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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

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…
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
Nobody understands Phishing better than an anti-spam company. That’s why we are providing Phishing Awareness Training to our customers. According to a report by Verizon, only 3% of targeted users report malicious emails to management. With compan…
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…

713 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