Ok, try this solution.
At first I created a following delegate somewhere in a namespace scope:
public delegate void OnCloseForm( bool login_accepted );
Then I created a login form:
partial class AuthorizationForm
{
private System.ComponentModel.IContainer components = null;
private OnCloseForm cfd;
protected override void Dispose(bool disposing)
{
if (disposing && (components != null))
{
components.Dispose();
}
base.Dispose(disposing);
}
#region Windows Form Designer generated code
private void InitializeComponent()
{
}
#endregion
private System.Windows.Forms.Button btnLogin;
private System.Windows.Forms.Button btnCancel;
private System.Windows.Forms.Label lblLogin;
private System.Windows.Forms.TextBox tbLogin;
private System.Windows.Forms.TextBox tbPasswd;
private System.Windows.Forms.Label lblPasswd;
}
Also I set in designer window btnAccept as Accept property and btnCancel as Cancel property of AuthorizationForm.
public partial class AuthorizationForm : Form
{
public OnCloseForm Result{
set { this.cfd = value; }
}
public string Login
{
get { return this.tbLogin.Text; }
}
public string Password
{
get { return this.tbPasswd.Text; }
}
public AuthorizationForm()
{
InitializeComponent();
this.cfd = null;
}
private void btnLogin_Click(object sender, EventArgs e)
{
this.DialogResult = DialogResult.OK;
this.Close();
if (this.cfd != null) this.cfd.Invoke(true);
}
private void btnCancel_Click(object sender, EventArgs e)
{
this.DialogResult = DialogResult.Cancel;
this.Close();
if (this.cfd != null) this.cfd.Invoke(false);
}
}
Then I added following code to application's main frame:
public partial class ExampleForm : Form
{
public ExampleForm()
{
InitializeComponent();
}
private void btnAuthorize_Click(object sender, EventArgs e)
{
this.Hide();
AuthorizationForm aform = new AuthorizationForm();
aform.Result = OtherFormClosing;
aform.Show();
}
public void OtherFormClosing(bool login_accepted){
this.Show();
string acc_text = (login_accepted ? "Login entered!" : "Authorization canceled!");
MessageBox.Show(acc_text);
}
}
One can do anything in delegate and there will be no any need to catch FormClosing event to destroy detached forms randomly.