Solved

Dynamic Buttons and Events

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

Independent Software Vendors: 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

Suggested Solutions

Title # Comments Views Activity
C# MVC Insert Multiple Row into DB 2 93
how to remove duplicate code from my project 5 64
locate sql commands in C# visual studio Project 6 67
Example code 13 41
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
This article aims to explain the working of CircularLogArchiver. This tool was designed to solve the buildup of log file in cases where systems do not support circular logging or where circular logging is not enabled
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…
I've attached the XLSM Excel spreadsheet I used in the video and also text files containing the macros used below. https://filedb.experts-exchange.com/incoming/2017/03_w12/1151775/Permutations.txt https://filedb.experts-exchange.com/incoming/201…

737 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