从C#中的子窗口访问父窗口中的控件

发布于 2024-10-02 04:46:19 字数 1379 浏览 3 评论 0原文

我有一个主“父”窗口,其中包含一个按钮和一个文本框。 我有另一个窗口“子”窗口,当我在文本框中输入一些文本并单击主窗口上的按钮时,该窗口会触发。现在子窗口包含另一个文本框和一个按钮。我需要做的是在子窗口的文本框中输入一些文本,然后当我点击子窗口上的按钮时,父窗口上的文本框应该用我从子窗口输入的文本进行更新。 这是示例:

Form1.cs

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace childform
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            Form2 tempDialog = new Form2(this);
            tempDialog.ShowDialog();
        }

        public void getText(string text)
        {
            textbox1.Text = text;
        }

    }
}

Form2.cs

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace childform
{
    public partial class Form2 : Form
    {
        private Form1 m_parent;

        public Form2(Form1 frm1)
        {
            InitializeComponent();
            m_parent = frm1;
        }

        private void button1_Click(object sender, EventArgs e)
        { 
            m_parent.getText(textbox1.text);
        }
    }
}

知道如何做到这一点吗?

I have a main "parent" window contains a button and a textbox.
I have another window "child" window which fires when I enter some text in the textbox and click the button on the main window. now the child window contains another textbox and a button. what I need to do is to enter some text in the textbox on child window then when I hit the button on the child window the textbox on parent window should get updated with the text I entered from the child window..
here is the sample:

Form1.cs

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace childform
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            Form2 tempDialog = new Form2(this);
            tempDialog.ShowDialog();
        }

        public void getText(string text)
        {
            textbox1.Text = text;
        }

    }
}

Form2.cs

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace childform
{
    public partial class Form2 : Form
    {
        private Form1 m_parent;

        public Form2(Form1 frm1)
        {
            InitializeComponent();
            m_parent = frm1;
        }

        private void button1_Click(object sender, EventArgs e)
        { 
            m_parent.getText(textbox1.text);
        }
    }
}

any idea how to do this?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(1

不必你懂 2024-10-09 04:46:19

1) 在Form2(子表格)中:
添加一个属性来获取TextBox中写入的文本:

Public string TheText
{
     get { return textbox1.Text; }
}

并将按钮DialogResult属性设置为Ok,以知道用户在关闭表单时按Ok,而不是按Ok关闭按钮。

2) 在表格1(家长)中:
检查用户是否按下了“确定”按钮,添加从 Form2 中的属性 theText 获取值。

private void button1_Click(object sender, EventArgs e)
{
   Form2 tempDialog = new Form2();
   if (tempDialog.ShowDialog() == DialogResult.Ok)
      textbox1.Text = tempDialog.TheText;
}

祝你好运!

1) In Form2 (The child one):
Add a property to get the text which wrote in the TextBox:

Public string TheText
{
     get { return textbox1.Text; }
}

And set the button DialogResult property to Ok to know that the user press Ok when he closes the form, not the close button.

2) In Form1 (The parent):
Check if the user pressed Ok button, add take the value from the property theText in Form2.

private void button1_Click(object sender, EventArgs e)
{
   Form2 tempDialog = new Form2();
   if (tempDialog.ShowDialog() == DialogResult.Ok)
      textbox1.Text = tempDialog.TheText;
}

Good luck!

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文