views:

29

answers:

2

Hi.

How do you make an event dynamically? Like for example, I'm making a notepad with tab support for practice, and for every new tab, a text box is made dynamically. How can I make an event (TextChanged for example) for these text boxes?

Thanks.

A: 

dynamicTextBox.TextChanged += (sender, args) => { your callback code goes here };

Edgar Sánchez
Where do I put that code?
Iceyoshi
+1  A: 

Create an TextBox object, assign the event on it and add to the tab control.

private void button1_Click(object sender, EventArgs e)
{
    tabControl1.TabPages.Add("t1", "new 1");

    var tb = new TextBox();
    tb.TextChanged += (bs, be) =>
    {
        MessageBox.Show("Text has been changed");
    };

    tabControl1.TabPages["t1"].Controls.Add(tb);
}
BrunoLM
This code works. Thanks everyone for the help
Iceyoshi
@Iceyoshi: Consider marking this answer as accepted if it solves your question. The checkmark on the left.
BrunoLM