2012-08-12 57 views
-1

我有兩種形式。我用這段代碼展示我的第二種形式:焦點返回表單

Form2 f = new Form2(); 
f.ShowDialog(); 

我需要了解何時焦點返回到主窗體。我試過Activate事件,但事實並非如此。

回答

4

對ShowDialog()的調用阻止父窗體。
當從ShowDialog的代碼中的exit()您目前的形式再次變得活躍,

例如:

using(Form2 f = new Form2()) 
{ 
    // At the moment of the call of ShowDialog, the Net Framework start its work to 
    // pass the focus to the first control in the Form2 instance 
    if(DialogResult.OK == f.ShowDialog()) 
    { 
     // Form confirmed, do your stuff 
    } 
} 

// At the end of the using block the parent form is again the active form with the focus on 
// the button that has started this process (or the last control that had focus) 
+0

謝謝。它有一點改變。使用(Form2 f = new Form2()){f.ShowDialog();} MessageBox.Show(「焦點返回到主窗體」);' – 2012-08-13 14:05:25

1

當你鍵入

Form2 f = new Form2(); 
f.ShowDialog(); 

等待,直到您手動關閉Form2的實例然後返回到主窗體。如果使用ShowDialog()方法,會發生這種情況。

在某些情況下,如果您只想彈出另一個窗體幾秒鐘並返回主窗體,則應使用formInstance.Show()方法。 Show()方法不會等待第二個窗體關閉,它會立即執行並將該控件傳遞給Show()語句後的下一個語句。

希望你能理解它。