2013-04-01 161 views
0

我有一個子窗體中的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個小時了,這是我在這裏問的唯一原因。

回答

1

我解決了它最後,但不是以理想的方式。

在父我打開它是這樣的:

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); 
    } 

這是行得通的,雖然我不得不解決它是一種模態形式的事實。如果有任何方法可以做到這一點,同時仍然完全控制這兩種形式,我很樂意聽到某人的聲音。

0

請檢查此方法..

但是,如果您傳遞私人數據,這不會有所幫助。

在瀏覽器頁面:

保護無效的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; 

}

相關問題