views:

751

answers:

3

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

A: 

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.

John Fisher
Im not sure what you mean by hold the main form?
Crash893
+3  A: 

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.

Adam Robinson
Perfect answer, you beat me to it.
Chris Marasti-Georg
I think this is exactly what i want but im a little unclear -- I get an error on the 3rd line of code saying (in my case logon) newform_authenticatied); does not exist in current context -- if this were a textbox or a costume usercontrol i would understand what to do because i would just click on the even in desgine view and be done with it but because that form is not present in the parent form im not sure how to make a costume event can you please explain more (and again thanks)
Crash893
Crash, that line was just an example. Typically Visual Studio will automatically "paint" a new event handler for you if you just type the += then press tab. newForm_Authenticated is likely the name of the method that it will create.
Adam Robinson
It did (which is weird because that exactly what i had as the text but now it seems to like it)
Crash893
one more question, From the logon form how do i trigger the event?
Crash893
Crash, how much experience do you have in C#? Just curious so I know how deep to go. What that line that we were discussing before does is say that when the Authenticated event is raised that it should call your method, which is called newForm_Authenticated. It didn't like it before because you didn't have a function with that name. In order to raise your event from the form, you can just call it like this (no quotes) "Authenticated(this, EventArgs.Empty);". I would highly recommended looking up information on events in .NET and some of the patterns before you go further, though.
Adam Robinson
A: 

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
}
Ezekiel Rage