How to exit the modal loop after calling "ShowDialog()"

key li 0 Reputation points
2024-02-17T11:40:31.7+00:00

form1.ShowDialog() I have run this code and the code after will not run until the form1 is closed. I wonder how to quit the modal loop without closing form1 using another code. For example : I press a button (named "Button1" ) of form1, then the eventhandler (maybe defined in the class of form1 ) quit the modal loop without closing form1. I wonder the code to solve this problem. Looking forward to your reply!

Windows Forms
Windows Forms
A set of .NET Framework managed libraries for developing graphical user interfaces.
1,891 questions
Windows
Windows
A family of Microsoft operating systems that run across personal computers, tablets, laptops, phones, internet of things devices, self-contained mixed reality headsets, large collaboration screens, and other devices.
5,389 questions
.NET
.NET
Microsoft Technologies based on the .NET software framework.
3,845 questions
{count} votes

2 answers

Sort by: Most helpful
  1. Hui Liu-MSFT 48,531 Reputation points Microsoft Vendor
    2024-02-19T06:33:21.9+00:00

    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.

    0 comments No comments

  2. Olaf Helper 44,501 Reputation points
    2024-02-19T06:52:22.37+00:00

    I wonder how to quit the modal loop without closing form1

    But that's the way how modal dialogs works.

    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.