Showing and Hiding Forms

  • Thread starter Thread starter Eric W
  • Start date Start date
E

Eric W

CreateProductForm f = new CreateProductForm();

f.Parent = this;

f.Show();



I'm trying to show a child form on the smartphone. It works fine when i
just call f.Show() but as soon as I set f.Parrent, the form never displays.
Can anyone help me. I need to be able to close the parent form from the
child form.

Many thanks.
 
Ok, if I don't set the parent, I seem to get a null pointer exception.

I have to cast this.Parent to a form since this.Parent is a control. All
I'm really needing to do is close a parent and current form from the child.

How can I do this ?


//the close method from the child
private void menuItem1_Click(object sender, System.EventArgs e)

{

Form f = (Form) this.Parent;

f.Close();

}
 
Add a field to your second form and store there the launching form (by
either passing it to a new constructor or setting the field via a property).

At this point let me tell you that it is bad design to close a form from
another that was launched from the original one...it just smells bad.

Cheers
Daniel
 
Ok, so what is good design practice?

Can I close a form and open a new form at the same time ??
 
How can we propose a good design without knowing what we are designing for?
For example, if you are building a wizard type application, you could use
panels and show/hide them thus creating the illusion of the user moving
through forms. You can keep opening forms and then allowing the user to go
back through the path they went forward (just be mindful of the memory
consumption).

In case it is useful to you, if you need to know from FormA when FormB is
closed (assuming FormB was Shown by FormA), consider adding a event handler
to the Closing event of FormB prior to showing it. The alternative is to use
ShowDialog so the thread of execution returns to where you showed the form.

Cheers
Daniel
 
So just as a matter of interest, why won't the following work ??


Form f = (Form) this.Parent;
f.Close();
 
Becasue the parent probably owns your reference and whe you close and
dispose of it, your own reference becomes invalid. What happens then I
won't guess - that's why Daniel noted that it's just a bad idea.

-Chris
 
Back
Top