Click here to Skip to main content
15,891,529 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I am using visual studio 2010 with .NET 4.0.

In my project I have three forms, two of them have text boxes which will be updated to provide the user with information. When I use the hide() function or set the visibility to false and then bring the form back all the text boxes and labels go back to their default values.

Is there any way to hide forms, but make them retain their current state without having to transfer the text from the text boxes and labels into variables?
Posted
Comments
karylle 15-Nov-11 20:02pm    
How did you "unhide" Form1(form originally hidden) in Form2?

Not true. Calling Hide cannot do anything to the status except visibility. Call Show; and everything will be the same, even position/size of the form. You certainly do something else which causes this confusion.

Look, huge amount of problems like that happens when you experiment with your "real" development code. In all such cases do not hesitate to add a minimalistic prototype not related to your main code to perform a pure test.

—SA
 
Share this answer
 
Comments
BillWoodruff 16-Nov-11 1:10am    
+5 absolutely right. I bet the user is closing the Form and re-creating it.
Sergey Alexandrovich Kryukov 16-Nov-11 1:21am    
Could be. Thank you, Bill.
--SA
you must be recreating the form while showing it back.
you must retain the reference of a form in parent form so while showing it back you use the same object with show property.
i.e.

C#
From2 frm;

OpenForm2()
{
 if (frm == null)
   frm = new From2();
 frm.show();

}
 
Share this answer
 
Comments
Sergey Alexandrovich Kryukov 16-Nov-11 1:38am    
Basically correct, but it's not nice of you to provide code which cannot even compile. It can confuse OP and get a negative effect. Is is so difficult to validate your code using compiler? I had to vote 3.
--SA
At the moment I am using this code in a button click.

C#
Test openTest = new Test;
openTest.show();
This.hide();


Why do I need to check if it is equal to null?
 
Share this answer
 
Comments
Zubair Alie 17-Nov-11 0:16am    
the way you are creating, you are creating local veriable, of whom reference you lost right after executing these lines of code.
you need to declare the "Test openTest;" at top of the cs file (global veriable).
and for the second time, while showing back the hidden form. call show() with the same object.
"openTest.Show();"

for more clear example. view the solution i provided above.
madmike159 22-Nov-11 10:44am    
I tried putting the Test openTest = new Test(); at the top of the code just after the form is initialized. Now it is giving me "an unhandled exception of type 'system.stackoverflowexception'"

I just want to be able to show and hide forms with their values kept the same...
Zubair Alie 23-Nov-11 0:19am    
make sure that your TestForm Constructor is not having any code which is making nested calls...because
Test openTest = new Test()
this line can't provide stackoverflow exception unless your TestForm's constructor is not having any problem inside.

and post the portion of Form1 you are writing for showing and hiding. secondly the constructor of TestForm. i will see where the issue is.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900