如何在C#中从子窗口访问和更改父窗口控件的值
你好 如何从子窗口更改父窗口中文本框的文本值。
即我的父窗口有 textbox1 和按钮,子窗口有 textbox2 和按钮。 当我在子窗口的文本框2中输入一些文本时,我需要更新文本框1的值。
我做了一些简单的函数来执行此操作,逻辑上它是正确的,但它不起作用我不知道为什么..parent.cs
child.cs
namespace digdog
{
public partial class parent : Form
{
public parent()
{
InitializeComponent();
}
public void changeText(string text)
{
textbox1.Text = text;
}
private void button1_Click(object sender, EventArgs e)
{
//Display modal dialog
child myform = new child();
myform.ShowDialog();
}
}
}
任何
namespace digdog
{
public partial class child : Form
{
public child()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
parent mytexts = new parent();
mytexts.changeText(textbox2.Text);
}
}
}
想法都会受到赞赏 提前致谢
Hi
how can I change text value of text box in parent window from child window..
i.e I have parent window have textbox1 and button and child window has textbox2 and button.
I need to update the value of textbox1 when I enter some text in child window's textbox2.
i did some simple function to do this logically its correct but its not working I have no idea why..
parent.cs
namespace digdog
{
public partial class parent : Form
{
public parent()
{
InitializeComponent();
}
public void changeText(string text)
{
textbox1.Text = text;
}
private void button1_Click(object sender, EventArgs e)
{
//Display modal dialog
child myform = new child();
myform.ShowDialog();
}
}
}
child.cs
namespace digdog
{
public partial class child : Form
{
public child()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
parent mytexts = new parent();
mytexts.changeText(textbox2.Text);
}
}
}
any ideas will be appreciated
thanks in advance
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
或简单:
ParentWindow 中
在子窗口的
,这非常酷
or simple:
in the ParentWindow
in the child window
this works pretty cool
您正在创建另一个“父”窗口(不可见)并更改其文本。孩子需要访问“真正的”父母。您可以通过在父级 Button1_click 中设置的子级属性来执行此操作。
例如,
在子类中
,在父级button1_click中
,在子级button1_click中
You are creating another 'parent' window (which is not visible) and changing its text. The 'real' parent needs to be accessed by the child. You could do this via a property on the child that is set in the parents button1_click.
e.g.
in child class
in parent button1_click
in child button1_click
不要创建新的父级。引用表单本身的父级。
这就是您首先创建孩子的方式:
Don't create a new parent. Reference the parent of the form itself.
And this is how you first create the child: