将数据从子窗体传递给父窗体
问题描述:
我有一个子窗体中的Web浏览器控件,它从显示的网页中捕获一些数据。我需要在浏览器窗体中使用这些数据传递给父窗体,但不必启动它的新实例,因为它已经打开。将数据从子窗体传递给父窗体
父窗体需要从浏览器接收这些数据,并使用解析页面时设置的变量更新一些文本框。
我有这样的父窗体:
private void browserToolStripMenuItem_Click(object sender, EventArgs e)
{
System.Threading.Thread t = new System.Threading.Thread(new System.Threading.ThreadStart(RunBrowser));
t.SetApartmentState(ApartmentState.STA);
t.Start();
}
public static void RunBrowser()
{
Application.Run(new BrowserForm());
}
我在子窗体尝试了很多东西,但我不能让它在所有的工作。我能得到的最好的结果是将一个变量传递给父级,并通过MessageBox显示它,但它拒绝更新TextBox。
顺便说一句我一直在试图解决这个问题,现在已经将近12个小时了,这是我在这里问的唯一原因。
答
我解决了它最后,但不是以理想的方式。
在父我打开它是这样的:
private void BrowserToolStripButton_Click(object sender, EventArgs e)
{
using (BrowserForm form = new BrowserForm())
{
form.ShowDialog(this);
}
}
我在父母也是这个方法:
public void SendStringsToParent(string s, string s2, string s3)
{
textBox.Text = s;
textBox2.Text = s2;
textBox3.Text = s3;
}
在儿童
然后(浏览器)的形式我有这样的:
private void Button1_Click(object sender, EventArgs e)
{
string stringToSend = "sending these";
string stringToSend2 = "strings to the";
string stringToSend3 = "parent form";
MainForm parent = (MainForm)this.Owner;
parent.SendStringsToParent(stringToSend, stringToSend2, stringToSend3);
}
这是行得通的,虽然我不得不解决它是一种模态形式的事实。如果有任何方法可以做到这一点,同时仍然完全控制这两种形式,我很乐意听到某人的声音。
答
请检查此方法..
但是,如果您传递私人数据,这不会有所帮助。
在浏览器页面:
保护无效的button1_Click(对象发件人,EventArgs的) {
string modulename = "Agile Software Development ";
string url;
url = "page2.aspx?module=" +modulename;
Response.Redirect(url);
}
在父页面
串RetrievedValue;保护无效Page_Load(object sender,EventArgs e) {
this.TextBox1.Text = Request.QueryString["module"];
// RetrievedValue = this.TextBox1.Text;
}