如何在C#中从子窗口访问和更改父窗口控件的值

发布于 2024-10-02 04:52:00 字数 1036 浏览 4 评论 0原文

你好 如何从子窗口更改父窗口中文本框的文本值。

即我的父窗口有 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

过潦 2024-10-09 04:52:00

或简单:
ParentWindow 中

ChildWindow child = new ChildWindow(); 
child.Owner = this;
child.ShowDialog();

在子窗口的

this.Owner.Title = "Change";

,这非常酷

or simple:
in the ParentWindow

ChildWindow child = new ChildWindow(); 
child.Owner = this;
child.ShowDialog();

in the child window

this.Owner.Title = "Change";

this works pretty cool

冬天旳寂寞 2024-10-09 04:52:00

您正在创建另一个“父”窗口(不可见)并更改其文本。孩子需要访问“真正的”父母。您可以通过在父级 Button1_click 中设置的子级属性来执行此操作。

例如,

在子类中

public parent ParentWindow {get;set;}

,在父级button1_click中

child myform = new child();
child.ParentWindow = this;
m.ShowDialog();

,在子级button1_click中

ParentWindow.changeText(textbox2.Text)

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

public parent ParentWindow {get;set;}

in parent button1_click

child myform = new child();
child.ParentWindow = this;
m.ShowDialog();

in child button1_click

ParentWindow.changeText(textbox2.Text)
始终不够 2024-10-09 04:52:00

不要创建新的父级。引用表单本身的父级。

    private void button1_Click(object sender, EventArgs e)
    {
        parent mytexts = this.Parent as parent;
        mytexts.changeText(textbox2.Text);
    }

这就是您首先创建孩子的方式:

    private void button1_Click(object sender, EventArgs e)
    {
        //Display modal dialog
        child myform = new child();
        myform.ShowDialog(this);  // make this form the parent
    }

Don't create a new parent. Reference the parent of the form itself.

    private void button1_Click(object sender, EventArgs e)
    {
        parent mytexts = this.Parent as parent;
        mytexts.changeText(textbox2.Text);
    }

And this is how you first create the child:

    private void button1_Click(object sender, EventArgs e)
    {
        //Display modal dialog
        child myform = new child();
        myform.ShowDialog(this);  // make this form the parent
    }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文