Hi,@ key li. Welcome to Microsoft Q&A. The ShowDialog method in Windows Forms is designed to create a modal dialog box that suspends the execution of the code in the caller until the dialog box is closed. The modal loop is an integral part of this mechanism.
When you use ShowDialog() to display a form, it runs as a modal dialog, and the code following ShowDialog() won't execute until the dialog is closed.
As David said: If you want to perform actions after showing the dialog without waiting for it to close, you could use Show() instead of ShowDialog().
private void button1_Click(object sender, EventArgs e)
{
// Show the dialog
using (var dialogForm = new DialogForm())
{
// Subscribe to the FormClosed event
dialogForm.FormClosed += DialogForm_FormClosed;
// Show the dialog modally
dialogForm.ShowDialog();
}
// Code here will continue executing after the dialog is closed
// You can perform any additional actions or checks here
// ...
button2.PerformClick();
// Raise the DialogClosed event
OnDialogClosed(EventArgs.Empty);
}
private void DialogForm_FormClosed(object sender, FormClosedEventArgs e)
{
// Handle the FormClosed event if needed
// This method will be called when the dialog is closed
}
protected virtual void OnDialogClosed(EventArgs e)
{
// Raise the DialogClosed event
DialogClosed?.Invoke(this, e);
}
private void button2_Click(object sender, EventArgs e)
{
var dialogForm = new DialogForm();
dialogForm.Show();
MessageBox.Show("string");
}
If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment". Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.