tycotrix,
Greetings. Your question is an excellent one. The most elegent way, in my humble opinion (some might argue otherwise) is to give the second form (the validation form) a public property that will flag whether or not the password was found to be valid. Note that this will likely entail a member variable to hold the boolean flag as well as the property to allow the "outside world" a glimpse at that flag.
I've written a bit of code (I apologize since it's a bit lengthy) illustrating how to handle this. The main form (Form1) has a single button (btnValidatePassword) which when clicked, opens the password validation dialog, as you describe it.
The password validation dialog (PasswordValidationDialog) contains a TextBox (to receive the password input) and two buttons. The first button is an OK button, the second a Cancel button.
When the user clicks the Cancel button, the password validation dialog closes, passing back a value of "DialogResult.Cancel". This is how Form1 knows that the user hit Cancel.
When the user clicks the OK button, the password validation dialog checks to see if the password is valid. It stores the results of this check in the member variable "m_isPasswordValid". Finally, it closes the form, returning the result "DialogResult.OK".
Form1, at this point knows that the user clicked OK, it asks the password validation dialog the results of the validation via the public property "IsPasswordValid".
Nuff of that, here's the code...
Code:
public partial class Form1 : Form
{
#region Event Handlers
//-------------------------
private void btnValidatePassword_Click(object sender, EventArgs e)
{
PasswordValidationDialog dlg = new PasswordValidationDialog();
// Show the dialog. When the dialog is closed, check to see if the user closed it by clicking the OK button.
if (dlg.ShowDialog(this) == DialogResult.OK)
{
if (dlg.IsPasswordValid)
{
// Password was valid.
}
else
{
// Password was invalid.
}
}
else
{
// Else the user closed the dialog by hitting the Cancel button.
}
dlg.Dispose();
}
//-------------------------
#endregion
}
//=========================
public partial class PasswordValidationDialog : Form
{
#region Member Variables
//-------------------------
private bool m_isPasswordValid = false;
//-------------------------
#endregion
#region Public Properties
//-------------------------
public bool IsPasswordValid
{
get { return m_isPasswordValid; }
}
//-------------------------
#endregion
#region Event Handlers
//-------------------------
private void btnCancel_Click(object sender, EventArgs e)
{
this.DialogResult = DialogResult.Cancel;
this.Close();
}
//-------------------------
private void btnOK_Click(object sender, EventArgs e)
{
this.AttemptValidation(txtPassword.Text);
this.DialogResult = DialogResult.OK;
this.Close();
}
//-------------------------
#endregion
#region Private Methods
//-------------------------
private void AttemptValidation(string password)
{
if (password == "This is a valid password")
m_isPasswordValid = true;
else
m_isPasswordValid = false;
}
//-------------------------
#endregion
}
Cheers.
- Roger Nedel