Add events to controls that were added dynamically - c#

I am working on a winforms app and I have added some controls dynamically (eg. Button). I want to add an event to that created button; how can I perform this? Also, can someone refer a C# book to me which covers all winforms topics?

// create some dynamic button
Button b = new Button();
// assign some event to it
b.Click += (sender, e) =>
{
MessageBox.Show("the button was clicked");
};
// add the button to the form
Controls.Add(b);

I totally agree with Darin's answer, and this is another syntax of adding dynamic event
private void Form1_Load(object sender, EventArgs e)
{
Button b = new Button();
b.Click += new EventHandler(ShowMessage);
Controls.Add(b);
}
private void ShowMessage(object sender,EventArgs e)
{
MessageBox.Show("Message");
}

Related

Adding event to button programmatically

I want to add an event to a programmatically generated button like this:
Button activityButton = new Button();
activityButton.Click += new EventHandler(onChangeActivityFilter);
I'm getting the following exception in the 2nd line:
Cannot implicit convert type System.EventHandler to System.Windows.RoutedEventhandler
The onChangeActivityFilter methode looks like this:
private void onChangeActivityFilter(object sender, EventArgs e)
{
}
I'd like to know what I'm doing wrong.
You need to create a instance of RoutedEventHandler:
activityButton.Click += new RoutedEventhandler(onChangeActivityFilter);
And also change the method signature:
private void onChangeActivityFilter(object sender, RoutedEventArgs e)
{
}
RoutedEvents where introduced with WPF.
You can also use lambda functions
activityButton.Click += (sender, e) =>
{
MessageBox.Show("the button was clicked");
};

Getting access to Button parent

I'm using a Button in a class. When the button is pressed, it should call a routine with the button's corresponding text. How do I convert the sender into a String_Entry? Also, I'm quite a newbie regarding object oriented/class programming, so comments are welcome.
public class String_Entry
{
public TextBox textbox;
public Button send;
// other stuff
public String_Entry()
{
textbox = new TextBox();
send = new Button();
send.Click += new System.EventHandler(this.bSend_Click);
// put in GUI, set parameters and other stuff
}
// other stuff
private void bSend_Click(object sender, EventArgs e)
{
// Trying to get the corresponding String_Entry from the Button click event
Button cntrl = (Button)sender;
String_Entry entry = (String_Entry)(cntrl.Parent);
parse.ProcessHexLine(entry);
}
}
Your solution of encapsulating a button with a textbox and the event handler is sound. It just goes wrong in the event handler:
private void bSend_Click(object sender, EventArgs e)
{
Button cntrl = (Button)sender;
String_Entry entry = (String_Entry)(cntrl.Parent);
parse.ProcessHexLine(entry);
}
Firstly, there is no point to doing anything with sender as it'll be the same as the field send. Next cntrl.Parent will give you a reference to the Form, or other container object, that contains the button, not this instance of String_Entry. To access that, use this. So you can change the event handler to:
private void bSend_Click(object sender, EventArgs e)
{
parse.ProcessHexLine(this);
}

asp.net event handler for dynamicallycreated button

I have a button created in code behind like so:
some method {
Button btnExportToExcel = new Button();
btnExportToExcel.Text = "Export To Excel";
btnExportToExcel.Click += new EventHandler(btnExportToExcel_Click);
pnlListView.Controls.Add(btnExportToExcel);
}
protected void btnExportToExcel_Click(object sender, EventArgs e)
{
//do something
}
The problem is that I can't ever get to execute the code from the event method.
Why is that?
are you adding controls to page in preinit event handler? Check
You must add the button to any controler .
protected void Page_Load(object sender, EventArgs e)
{
Button btnExportToExcel = new Button();
btnExportToExcel.Text = "Export To Excel";
btnExportToExcel.Click += new EventHandler(btnExportToExcel_Click);
//this is add the button to the form1
this.form1.Controls.Add(btnExportToExcel);
}
void btnExportToExcel_Click(object sender, EventArgs e)
{
//...
Response.Write("click me...");
}
Please add the dynamic controls in the Page's Init event handler so that the ViewState and Events are triggered appropriately.

How to add event handler for dynamically created controls at runtime?

I am working on C# windows application. my application get controls(button,text box,rich text box and combo box etc)from custom control library and placed them into form dynamically at run time. how i create event handler for that controls using delegate? and how to add business logic in particular custom control click event?
For Example:
i have user1, user2, user3, when user1 log in i want to show only "save" button. when user2 then only show "add and delete" buttons and user 3 only show "add and update" buttons.text boxes and button created as per user log in information taken from DB tables.in this scenario how i handle different event(adding,saving,updating,deleting) for button save,add,delete and update for different users when form is dynamically created controls(save,add,delete and update button object is from same button class)
With anonymous method:
Button button1 = new Button();
button1.Click += delegate
{
// Do something
};
With an anonymous method with explicit parameters:
Button button1 = new Button();
button1.Click += delegate (object sender, EventArgs e)
{
// Do something
};
With lambda syntax for an anonymous method:
Button button1 = new Button();
button1.Click += (object sender, EventArgs e) =>
{
// Do something
};
With method:
Button button1 = new Button();
button1.Click += button1_Click;
private void button1_Click(object sender, EventArgs e)
{
// Do something
}
Further information you can find in the MSDN Documentation.
var t = new TextBox();
t.MouseDoubleClick+=new System.Windows.Input.MouseButtonEventHandler(t_MouseDoubleClick);
private void t_MouseDoubleClick(object sender, MouseButtonEventArgs e)
{
throw new NotImplementedException();
}
It's adding double click eventhandler to new TextBox
I believe you could do something like this:
if (userCanAdd)
container.Controls.Add(GetAddButton());
if (userCanUpdate)
container.Controls.Add(GetUpdateButton());
if (userCanDelete)
container.Controls.Add(GetDeleteButton());
private Button GetAddButton() {
var addButton = new Button();
// init properties here
addButton.Click += (s,e) => { /* add logic here */ };
// addButton.Click += (s,e) => Add();
// addButton.Click += OnAddButtonClick;
return addButton;
}
private void OnAddButtonClick (object sender, EventArgs e) {
// add logic here
}
// The other methods are similar to the GetAddButton method.

Send an EventHandler to a RoutedEventHandleer

here is the basic model of the code I have:
private void textBlock1_Tap (object sender, System.Windows.Input.GestureEventArgs e)
{
TextBox TextBox1 = new TextBox();
TextBlock tblk = (TextBlock)sender;
ApplicationBar = new ApplicationBar();
TextBox1.LostFocus += TextBox1_LostFocus;
ApplicationBarIconButton appBarButton = new ApplicationBarIconButton(new Uri("/Assets/AppBar/check.png", UriKind.Relative));
appBarButton.Text = "Accept";
ApplicationBar.Buttons.Add(appBarButton);
appBarButton.Click +=
}
void TextBox1_LostFocus(object sender, RoutedEventArgs e)
{
//do things here
}
I need to subscribe to the click Event, and when it is triggered, I need TextBox1_LostFocus to be called and TextBox1 to be sent as a parameter. Basically what I want to do is make appBarButton.Click do the exact same thing as TextBox1.LostFocus.
Problem is, LostFocus is a RoutedEventHandler and TextBox1_LostFocus takes a RoutedEventArgs as a parameter while appBarButton.Click is an EventHandler.
I'm not very experienced in coding at all so any help is much appreciated!
RoutedEventArgs inherits EventArgs.
You can add the same handler to both events.
Better yet, you can move the code to a function and call it from two different event handlers.

Categories