Solved

Dynamic Buttons and Events

Posted on 2004-03-28
5
332 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
ID: 10701919
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
ID: 10701926
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
ID: 10702257
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
ID: 10702269
Oops... Forget it...
I haven't seen I was surfing the C# section... :">
0
 
LVL 9

Expert Comment

by:msdixon
ID: 10704674
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Article by: Najam
Having new technologies does not mean they will completely replace old components.  Recently I had to create WCF that will be called by VB6 component.  Here I will describe what steps one should follow while doing so, please feel free to post any qu…
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
Video by: Mark
This lesson goes over how to construct ordered and unordered lists and how to create hyperlinks.
As a trusted technology advisor to your customers you are likely getting the daily question of, ‘should I put this in the cloud?’ As customer demands for cloud services increases, companies will see a shift from traditional buying patterns to new…

910 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

20 Experts available now in Live!

Get 1:1 Help Now