Click here to Skip to main content
15,897,146 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
It takes two forms to be able to enter all the info on a particular transaction. I want to be able to flip back and forth between these two forms retaining what was entered on each until the 'Save' button is clicked. I think I should be able to use Form2.Show, Me.Hide and then Form1.Show, Me.Hide.

The first time I try to go back to Form1 from a ‘Back’ button on Form2 the information previously entered on Form1 has disappeared. Why? However, if I fill it in again, go to Form2, and then click the ‘Back’ button again the info is retained.

Obviously the code that is being executed when I click the ‘Back’ button on Form2 the second time is different than the code that is executed the first time I click that same button. How can this be?

For days I’ve been trying to understand this. I’ve commented out nearly every line of code, stepped thru code, googled till I’m blue in the face (there has been a lot written about this), and I still can’t figure out why this behavior occurs.

Can anyone explain this phenomenon? I can devise some workaround by storing data in globals and repopulating controls but this is a lot of work that should be unnecessary.
Posted
Updated 9-May-14 2:35am
v2
Comments
[no name] 9-May-14 9:47am    
You should probably post some code that demonstrates this phenomenon.

1 solution

Don't do it like that - it means that the two forms have to know about each other.

I'd do it like this:

In Form2, create an event which says "HideMe".
In Form1 have a class level variable called form2. Start with this null.
When you press the button, check form2: if it is null then create an new instance of Form2 and assign it to the form2 variable. Add a handler for the "FormClosing" and "HideMe" events of the Form2 instance.
Whether it was or not, call Hide on form1 and Show on form2.

When the form2 FormClosing event gets fired, close your form1
When the "HideMe" event is fired, call form2.Hide, and form1 Show.
 
Share this answer
 

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