2016-10-17 39 views
2

我有兩種形式,我打開和ShowDialog的第二種形式是這樣的:呼叫事件,當第二個窗體關閉

Recieving_Stock_Form f = new Recieving_Stock_Form(); 
f.Username = Username; 
f.Role = Role; 
f.Date = monthCalendar1.SelectionStart.ToString(@"yyyy\/MM\/dd"); 
f.ShowDialog(); 

現在,當我關閉第二種形式我想觸發第一個事件形式 EG

void txtStockCount_TextChanged(object sender, EventArgs e) 

任何想法,我可以看一下這個還是怎麼辦呢?

感謝

回答

1

ShowDialog是模態,因此會阻止executiong,所以當你關閉它,執行接下來的事情。

你可以這樣做:在Form1

Recieving_Stock_Form f = new Recieving_Stock_Form(); 
f.Username = Username; 
f.Role = Role; 
f.Date = monthCalendar1.SelectionStart.ToString(@"yyyy\/MM\/dd"); 
f.ShowDialog(); 
// it will continue here when the form is closed. 
txtStockCount_TextChanged(this, EventArgs.Empty); // or assign the variable directly. 
+2

這是最快和最簡單的答案。謝謝:) – Cleaven

4

,假設你的代碼是這樣

Recieving_Stock_Form f = new Recieving_Stock_Form(); 

您可以添加代碼

f.Form_Closing += ExampleHandler; 

ExampleHandler(object sender, FormClosingEventArgs e) 
{ 
    //Do stuff 
} 
+1

爲什麼要在模態表單上註冊一個「關閉」事件? –

+0

因爲它是以第一種形式執行的,而不是第二種 –

+0

但是'ShowDialog'是模式的,意味着它將阻止執行直到它被關閉。所以你總是知道它何時關閉。 –

2

有兩個事件,你可以處理 - FormClosedFormClosing,取決於您的決定ñ。

f.FormClosed += f_FormClosed; 

private void f_FormClosed(object sender, FormClosedEventArgs e) 
{ 
    //Call your function or do whatever you want 
} 
+0

謝謝你的幫助:) – Cleaven

+1

不客氣! – SeM

2

我寧願沒有欺騙表格;相反,我建議從Recieving_Stock_Form的值賦給txtStockCount

using (Recieving_Stock_Form f = new Recieving_Stock_Form()) { 
    f.Username = Username; 
    f.Role = Role; 
    f.Date = monthCalendar1.SelectionStart.ToString(@"yyyy\/MM\/dd"); 

    // == DialogResult.OK - let user have a chance to cancel his/her input 
    if (f.ShowDialog() == DialogResult.OK) { 
    //TODO: put the right property here 
    txtStockCount.Text = f.StockCount.ToString(); 
    } 
} 
+0

謝謝你的幫助:) – Cleaven