我在窗體應用程序中有一個窗體,我想要在主窗體頂部顯示,關閉它,然後立即使用MessageBox.Show()
顯示一個對話框。但是,當消息框顯示時,第一種形式仍然顯示,並且直到我在消息框上單擊確定後纔會消失。我試圖等待在窗體的VisibleChanged
事件的事件處理程序中顯示消息框,甚至在窗體和主窗體上都調用Refresh()
。在顯示消息框之前,有什麼方法可以確定第一個窗體何時完全消失?如何確保Windows窗體「完全」關閉?
編輯:
下面是一些演示如何顯示窗體的代碼。
static class Program
{
// The main form is shown like this:
static void Main()
{
Application.Run(new MainForm());
}
}
public class Class1
{
// _modalForm is the first form that is displayed that won't fully go away
// when it is closed.
ModalForm _modalForm;
BackgroundWorker _worker;
public Class1()
{
_modalForm = new ModalForm();
_worker = new BackGroundWorker();
_worker.RunWorkerCompleted += backgroundWorker_RunWorkerCompleted
}
public void Method1()
{
_worker.RunWorkerAsync();
// The first form is shown.
_modalForm.ShowDialog();
}
// This code runs in the UI thread.
void backgroundWorker_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
{
_modalForm.VisibleChanged += new EventHandler(_modalForm_visibleChanged);
_modalForm.Close();
}
void _modalForm_visibleChanged(object sender, EventArgs e)
{
// When the message box is shown, the other form is still visible
// and remains so until I click OK.
MessageBox.Show("The other form was just closed.");
// Note: I originally tried to use the FormClosed event instead of
// VisibleChanged. Then I tried Deactivate, in attempt to use an event
// that occurred later thinking that might do the trick. VisibleChanged
// is the latest event that I found.
//
}
我認爲答案取決於你如何顯示主窗體。你能發表一些簡單的代碼來證明你不喜歡的行爲嗎? – Karmastan 2010-07-06 17:15:29