title: C# dialogresult wont close form
body: <p>I am developing an application which requires loggging in.</p>
  
  <p>I've developed the following forms:
  - frmMain
  - frmLogon</p>
  
  <p>Form main launches when bootstrapping the windows forms application. In the "Shown" event of form main the FrmLogon form is shown as a dialog. </p>
  
  <p>However when automatically setting the DialogResult to OK the form won't close, the program only works when manually hitting the "Logon" button, which triggers the exact same functionality.</p>
  
  <p>Ive tried to Close() the form logon, but this leaves me with an System.ObjectDisposedException.</p>
  
  <p>Any suggestions?</p>
  
  <p>FrmMain.CS</p>
  
  <pre><code>private void OnFormShown(object sender, EventArgs e)
      {
          OnClickMenuLogon(sender, e);
      }
  
  private void OnClickMenuLogon(object sender, EventArgs e)
          {
              if (IsLoggedOn())
              {
                  G.LogOff();
                  User = null;
                  OnLoggedOff();
              }
              else
              {
                  FrmLogon logon = new FrmLogon(G, true);
                  DialogResult result = logon.ShowDialog();
                  if (result == DialogResult.OK)
                  {
                      User = logon.User;
                      _hostName = User.Name + " @ " + (String.IsNullOrEmpty(logon.HostName) ? Environment.MachineName : logon.HostName);
                      OnLoggedOn();
                  }
              }
          }
  </code></pre>
  
  <p>FrmLogon.CS</p>
  
  <pre><code>private void OnLogOnSuccess(object sender, LoggedOnEventArgs e)
      {
          tbStatus.Text = $"[{DateTime.Now.ToString("HH:mm:ss")}]: Successfully logged on.\r\n" + tbStatus.Text;
          User = _g.LoggedUser;
          HostName = tbHost.Text;
          DialogResult = DialogResult.OK;
      }
  </code></pre>
  
  <p>Edit: When using</p>
  
  <pre><code>   this.DialogResult = DialogResult.OK;
  this.Close();
  </code></pre>
  
  <p>the following exception occurs:
  <a href="https://i.stack.imgur.com/bgq9N.png" rel="nofollow noreferrer">Exception screenshot</a></p>