Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Add events to controls added dynamically

Tags:

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?

like image 254
salman Avatar asked Nov 27 '10 10:11

salman


People also ask

How to add event dynamically in js?

Attaching the event dynamicallyclassName = 'dynamic-link'; // Class name li. innerHTML = dynamicValue; // Text inside $('#links'). appendChild(li); // Append it li. onclick = dynamicEvent; // Attach the event!

How to add event handler to dynamically created button?

To attach event handlers to the dynamically created button, we need to select the button with a class of btn and add an event listener of click . We're saying that onclick of the button, the p tag with a class of moreInfo should display block .

Which page event creates or recreates the dynamic controls?

MSDN says that create Dynamic Controls in PreInit Event of Page Life Cycle.


1 Answers

// 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);
like image 143
Darin Dimitrov Avatar answered Sep 22 '22 16:09

Darin Dimitrov