从C#中的子窗口访问父窗口中的控件
我有一个主“父”窗口,其中包含一个按钮和一个文本框。 我有另一个窗口“子”窗口,当我在文本框中输入一些文本并单击主窗口上的按钮时,该窗口会触发。现在子窗口包含另一个文本框和一个按钮。我需要做的是在子窗口的文本框中输入一些文本,然后当我点击子窗口上的按钮时,父窗口上的文本框应该用我从子窗口输入的文本进行更新。 这是示例:
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
1) 在Form2(子表格)中:
添加一个属性来获取TextBox中写入的文本:
并将按钮
DialogResult
属性设置为Ok
,以知道用户在关闭表单时按Ok,而不是按Ok关闭按钮。2) 在表格1(家长)中:
检查用户是否按下了“确定”按钮,添加从 Form2 中的属性 theText 获取值。
祝你好运!
1) In Form2 (The child one):
Add a property to get the text which wrote in the TextBox:
And set the button
DialogResult
property toOk
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.
Good luck!