Hi Jacques,
Thanks for your feedback.
In my code, the form1 was just hide not destroyed.
I think you should create an overloaded constructor of Form2, which take
form1's reference as parameter.
Then in class form2 you have got a reference of form1.
In form2, you can do whatever program logic you like, if you want to reshow
the form1, you can show it out while no need to create a new instance.
So I think in one form, you should store an instance of another form. Then
you can manipulate another form freely.
Do like this:
Class Form1:
private System.Windows.Forms.Form f;
private void button1_Click(object sender, System.EventArgs e)
{
if(this.f==null)
{
Form2 f2=new Form2(this);
this.f =f2;
}
this.Hide();
f.Show();
}
Class Form2:
private System.Windows.Forms.Form f;
public Form2(Form1 f1)
{
this.f=f1;
InitializeComponent();
}
private void button1_Click(object sender, System.EventArgs e)
{
this.f.Show();
this.Hide();
}
Then, you can swith between these 2 forms. But you must design your
application model well, for example, when closing the one of the forms, you
should either show or close another form, or the application will not
end(Because another "hide" form is still exist).
For your stack overrun problem:
First, in .Net all the class that inherited from System.Object will be
created on heap, and the class's instance is reference to this class object.
Second, you should not hide one form and then pay no attention to it. Then
it will hide and not die, tie up the system resource. You should manage
these forms well. This is why I suggest each form class manage an instance
of other forms.
Hope this helps,
Best regards,
Jeffrey Tan
Microsoft Online Partner Support
Get Secure! -
www.microsoft.com/security
This posting is provided "as is" with no warranties and confers no rights.