2016-07-15 179 views
2

我有兩種形式,稱爲BillingForm(父窗體)和SearchProduct(子窗體)。如何將值從子窗體傳遞給父窗體?

BillingForm代碼

private void textBoxProductNo_KeyDown(object sender, KeyEventArgs e) 
{ 
    if(e.KeyCode==Keys.F9) 
    { 
     using(SearchProduct sp=new SearchProduct) 
     { 
      sp.ShowDialog(this); 
     } 
    } 
} 

public void updatedText(string fromChildForm) 
{ 
    textBoxProduct.text=fromChildForm; 
} 

SearchProduct形式的代碼(子窗體)

private void dataGridView1_KeyDown(object sender,KeyEventArgs e) 
{ 
    if(e.KeyCode==Keys.Enter) 
    { 
     BillingForm bf=(BillingForm)this.Owner; //Error appear here 
     bf.updatedText("Hello World"); 
     this.close(); 
    } 
} 

我收到一條錯誤消息。

「」類型的未處理的異常在BillingSoftware.exe發生
其他信息:無法轉換類型「BillingForm.MDIParent」爲類型「BillingForm.BillingForm

+1

可能的複製[我如何通過一個數值從孩子回父窗體?](http://stackoverflow.com/questions/280579/how -DO-I-傳遞一個值從 - 一個孩子背到了父換米) – itsme86

+0

@ itsme86我看到了編碼。我只用了相同的編碼。但我得到異常。請看我的錯誤消息 – Faisal

+0

BillingForm是MDI表單嗎? – itsme86

回答

0

嘗試的目的是通過父爲構造和使用它的變量

using(SearchProduct sp = new SearchProduct(this)) 
{ 
    sp.ShowDialog(this); 
} 

//In SearchProduct class 
public BillingForm MyParent {get; private set;} 

public SearchProduct(BillingForm parent) 
{ 
    this.MyParent = parent; 
} 

private void dataGridView1_KeyDown(object sender,KeyEventArgs e) 
{ 
    if (e.KeyCode == Keys.Enter) 
    { 
     BillingForm bf = this.MyParent; 
     bf.updatedText("Hello World"); 
     this.close(); 
    } 
} 
相關問題