添加事件处理程序从父窗体在C#中的子窗体控件 [英] Adding an event handler for a control in child form from parent form in C#
问题描述
我有两种形式。一个是与一个按钮和一个文本框父窗体。在单击该按钮将打开一个对话框子窗体从而有一个文本框和一个按钮。现在,我想是时候曾经在子窗体文本框中的文本自动更改父窗体的文本框的文本更改。为了获得这个我所做的是,
I have two forms. One is a parent form with a button and a text box. On click the button a dialog opens the child form which in turn has a textbox and a button. Now what i want is when ever the text in the child form textbox changes the text in the parent form textbox changes automatically. To acquire this what i did is,
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";
}
我添加上述code在父窗体和子窗体Form3。但没有任何反应,父窗体的文本框仍甚而不在子窗体changng文本后更改。笏我做错了这里。
I added the above code in the parent form and the child form is Form3. But nothing happens , the parent form textbox still doesnt change even after changng text in the child form. Wat am i doing wrong here.
推荐答案
您可以在子窗体中添加事件,并超越它时,文本改变。然后创建父窗体事件处理程序,并在父母的形式更改文本。
在子窗体:
You can add event in child form and rise it when text changed. Then create event handler in parent form and change text in parent form. In child form:
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;
}
希望它帮助。
这篇关于添加事件处理程序从父窗体在C#中的子窗体控件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!