Updating a variable in a Parent Form from a dialog box.

  • Thread starter Thread starter Jesse
  • Start date Start date
J

Jesse

Hi All,

Just wanted to know how to update a public variable in a parent form from a
dialog box. It appears that i get a null reference error when assigning the
variable as follows:

((FormMain)this.ParentForm).loggedIn = true;

Can anybody shed some light?

Thanks,
Jesse
 
I'm not sure, but i think that if is not a modal form, it has (as default)
no parent.

But you can always set it when you istantiate it

Form myChildForm=new Form();
myChildForm.Parent=this;
 
Hi,

I would do another thing, I will pass a reference to the parent to the
constructor of the dialog:

Form childform = new ChildForm( this );
childform.ShowDialog();

Hope this help,
 
ParentForm is really a property to get the form that your control is sited
on, (which is very handy for MDI forms). Try using "Parent" instead:

In your calling form (FormMain):

ChildForm cf = new ChildForm();
cf.Parent = this;
cf.Show();


Then in your child form:

((FormMain)this.Parent).loggedIn = true;
 
Back
Top