I am developing an application which requires loggging in.
I've developed the following forms: - frmMain - frmLogon
Form main launches when bootstrapping the windows forms application. In the "Shown" event of form main the FrmLogon form is shown as a dialog.
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.
Ive tried to Close() the form logon, but this leaves me with an System.ObjectDisposedException.
Any suggestions?
FrmMain.CS
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();
}
}
}
FrmLogon.CS
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;
}
Edit: When using
this.DialogResult = DialogResult.OK;
this.Close();
the following exception occurs: Exception screenshot