Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

c#

winforms

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)

like image 290
user2610173 Avatar asked Jul 25 '13 10:07

user2610173


3 Answers

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.

like image 155
Andy Avatar answered Oct 11 '22 17:10

Andy


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

like image 25
Kamil Budziewski Avatar answered Oct 11 '22 15:10

Kamil Budziewski


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.
like image 22
Andres A. Avatar answered Oct 11 '22 17:10

Andres A.