Link to home
Start Free TrialLog in
Avatar of cossy74
cossy74

asked on

Dynamic Buttons and Events

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?
ASKER CERTIFIED SOLUTION
Avatar of TheAvenger
TheAvenger
Flag of Switzerland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
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
Avatar of Jarodtweiss
Jarodtweiss

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
Oops... Forget it...
I haven't seen I was surfing the C# section... :">
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