Question

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.

Was it helpful?

Solution

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);
}

OTHER TIPS

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

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top