?
Solved

Adding an Event handler Dynamically

Posted on 2012-03-28
6
Medium Priority
?
501 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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 1000 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
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 

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 1000 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

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

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

Welcome my friends to the second instalment and follow-up to our Minify and Concatenate Your Scripts and Stylesheets (http://www.experts-exchange.com/Programming/Languages/.NET/ASP.NET/A_4334-Minify-and-Concatenate-Your-Scripts-and-Stylesheets.html)…
This article describes relatively difficult and non-obvious issues that are likely to arise when creating COM class in Visual Studio and deploying it by professional MSI-authoring tools. It is assumed that the reader is already familiar with the cla…
In this video you will find out how to export Office 365 mailboxes using the built in eDiscovery tool. Bear in mind that although this method might be useful in some cases, using PST files as Office 365 backup is troublesome in a long run (more on t…
In this video, Percona Solution Engineer Dimitri Vanoverbeke discusses why you want to use at least three nodes in a database cluster. To discuss how Percona Consulting can help with your design and architecture needs for your database and infras…
Suggested Courses

762 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