How would one switch a public bool to true from a child form in a mdi type program?
I have a child form called logon that if everything checks out i want to set a "authenticated" bool to true in the form1 (main) form
How would one switch a public bool to true from a child form in a mdi type program?
I have a child form called logon that if everything checks out i want to set a "authenticated" bool to true in the form1 (main) form
You could make a globally accessible variable that holds the main form, then use that variable within the child to call methods on the main form.
Or, you could cast the appropriate Parent or Owner property of the child window to the proper type of the main form, and work from there.
The proper, true OO way of doing things would be to expose an event on your child form that the parent can attach to. You're violating your separation of concerns if you have the child form make assumptions about its MdiParent
.
For example, a very simple method of doing what you describe would be to have this on your child form:
public event EventHandler Authenticated;
The when the parent opens it...
YourForm newForm = new YourForm();
newForm.Authenticated += new EventHandler(newForm_Authenticated);
newForm.MdiParent = this;
// and so on
You could also go slightly more sophisticated (and I do mean slightly) by adding an Authenticated
boolean property to your child form, and rename the event to AuthenticatedChanged
. You could then use the same event handler to inspect the value of the property to determine if the user has authenticated.
In either scenario, you simply raise your event from the child form when you want the parent to update.
Since I noticed you are using a "logon" form you could try the following: set the logon form's DialogResult property according to username/password testing success. I am using username/pass just as an example. On the logon form do something like:
if(isMatch(username, password)){
this.DialogResult=DialogResult.OK;
this.Close();
}
else MessageBox.Show("Logon error - try again!");
// or anything else you would like to do in case of an error
And then on the parent form:
LogonForm f = new LogonForm();
if(f.ShowDialog() == DialogResult.OK){
// continue
}
else {
// abort
}