Solved

Dynamic Buttons and Events

Posted on 2004-03-28
5
331 Views
Last Modified: 2010-04-16
Hi,

I have decalre a Button object in the form without actually droping the control onto the form.
At run time i decide whether or not to add the button.

How can i write an event handler for this button?
0
Comment
Question by:cossy74
5 Comments
 
LVL 20

Accepted Solution

by:
TheAvenger earned 500 total points
Comment Utility
Example: if you new button is btn and you want to handle Click, then:

btn.Click += new EventHandler (MyMethod);

where MyMethod has the standard for button click event handlers definition:

private void MyMethod (object sender, EventArgs e)
{
  // Do something
}
0
 
LVL 19

Expert Comment

by:Desp
Comment Utility
I dont have VS.Net Right now but you can try this code:

Button DynamicButton1= new Button();
b.Name="DynamicButton1"+counterint
Controls.Add(DynamicButton1);
DynamicButton1.Click+=new System.EventHandler(this.DynamicButton1_Clicked); // change your function name here

and put your DynamicButton1_Clicked somewhere in the same class
0
 
LVL 4

Expert Comment

by:Jarodtweiss
Comment Utility
To complete the comments :
VB.NET syntax :

dim myButton as new Button
AddHandler myButton.Click, AddressOf me.Button_Click

if you want later to remove your handler :
RemoveHandler myButton.Click, AddressOf me.Button_Click

private sub Button_Click(sender as object, e as EventArgs)
end Sub
0
 
LVL 4

Expert Comment

by:Jarodtweiss
Comment Utility
Oops... Forget it...
I haven't seen I was surfing the C# section... :">
0
 
LVL 9

Expert Comment

by:msdixon
Comment Utility
you could always drag a new button on the form, then set the Visible property to false, and add your event handler... then, if you need the button, set the Visible property to true
0

Featured Post

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
asp.net mvc5 3 35
C# Export DataGridView 4 37
c# code 19 56
Hide Tab Page 3 18
Bit flags and bit flag manipulation is perhaps one of the most underrated strategies in programming, likely because most programmers developing in high-level languages rely too much on the high-level features, and forget about the low-level ones. Th…
Introduction Hi all and welcome to my first article on Experts Exchange. A while ago, someone asked me if i could do some tutorials on object oriented programming. I decided to do them on C#. Now you may ask me, why's that? Well, one of the re…
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…
This video shows how to remove a single email address from the Outlook 2010 Auto Suggestion memory. NOTE: For Outlook 2016 and 2013 perform the exact same steps. Open a new email: Click the New email button in Outlook. Start typing the address: …

771 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

10 Experts available now in Live!

Get 1:1 Help Now