Adding an event handler for a control in a child form from the parent form in C #

I have two forms. One of them is the parent form with a button and a text field. When a button is clicked, a dialog box opens for the child form, which in turn has a text box and a button. Now I want the text in the text box of the child form to change the text in the text box of the parent form. To get this, I did the following:

Form3 f3 = new Form3();
f3.delBetInpTxt.TextChanged +=new EventHandler(delBetInpTxt_TextChanged);
public void delBetInpTxt_TextChanged(object sender, EventArgs e)
    {
        TextBox t = (TextBox)sender;
        simDelTxt.Text = t.Text + " ms";
    }

I added the above code to the parent form and the child form is Form3. But nothing happens, the text field of the parent form still does not change even after changing the text in the child form. What am I doing wrong here?

+4
source share
1 answer

. . :

public event EventHandler OnChildTextChanged;
private void textBox1_TextChanged(object sender, EventArgs e)
{
    if(OnChildTextChanged != null)
       OnChildTextChanged(textBox1.Text, null);
}

:

private void button1_Click(object sender, EventArgs e)
{
    ChildForm child = new ChildForm();
    child.OnChildTextChanged += new EventHandler(child_OnChildTextChanged);
    child.ShowDialog();
}

void child_OnChildTextChanged(object sender, EventArgs e)
{
    textBox1.Text = (string)sender;
}

, .

+15

All Articles