Solved

Dynamic Buttons and Events

Posted on 2004-03-28
5
337 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
[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
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

Technology Partners: 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

Introduction This article series is supposed to shed some light on the use of IDisposable and objects that inherit from it. In essence, a more apt title for this article would be: using (IDisposable) {}. I’m just not sure how many people would ge…
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
If you're a developer or IT admin, you’re probably tasked with managing multiple websites, servers, applications, and levels of security on a daily basis. While this can be extremely time consuming, it can also be frustrating when systems aren't wor…
Monitoring a network: why having a policy is the best policy? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the enormous benefits of having a policy-based approach when monitoring medium and large networks. Software utilized in this v…

717 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