Switch Between Forms Without Data Loss

  • Thread starter Thread starter Dennis
  • Start date Start date
D

Dennis

Good Day

I am working on a Windows program that uses three seperate forms.
[ frmMain, frmTwo, frmThree ]

frmMain receives input from the user and preforms the necessary calculations
It also stores the user's data and calculated values in a set of variables
within a
global class. That much seems to work fine.

However, when the user switches between frmMain and frmTwo [ or frmThree ]
and then goes back to frmMain, the textboxes are all empty. How can I make
the
switch without losing the textbox data ?

Here is the type of code within frmMain that actually switches to frmTwo:

private void mnuGoTofrmTwo_Click(object sender, System.EventArgs e)
{

frmTwo copyfrmTwo = new frmTwo();

copyfrmTwo.Show();

Hide(); // Hide frmMain

}

Here is the type of code within frmTwo that actually switches to frmMain:

private void mnuGoTofrmMin_Click(object sender, System.EventArgs e)
{

frmMain copyfrmMain = new frmMain();

copyfrmMain.Show();

Hide(); // Hide frmTwo

}
 
The easiest solution is to not create new forms each time. You need to create
a singleton form so that you always have access. As long as you Hide/Show the
form and don't Close it out, it will stick around and you won't lose your data.

Otherwise, when you create the new form, you are going to have to reload all of
the data from that global object, and that means more code for you. Of course,
singleton forms means more code as well. End of the day, it is up to you.
 
Thank you for the reply:
I will be trying it on a simple windows program and will let you know if I
was successful

Dennis

Justin Rogers said:
The easiest solution is to not create new forms each time. You need to create
a singleton form so that you always have access. As long as you Hide/Show the
form and don't Close it out, it will stick around and you won't lose your data.

Otherwise, when you create the new form, you are going to have to reload all of
the data from that global object, and that means more code for you. Of course,
singleton forms means more code as well. End of the day, it is up to you.


--
Justin Rogers
DigiTec Web Consultants, LLC.
Blog: http://weblogs.asp.net/justin_rogers

Dennis said:
Good Day

I am working on a Windows program that uses three seperate forms.
[ frmMain, frmTwo, frmThree ]

frmMain receives input from the user and preforms the necessary calculations
It also stores the user's data and calculated values in a set of variables
within a
global class. That much seems to work fine.

However, when the user switches between frmMain and frmTwo [ or frmThree ]
and then goes back to frmMain, the textboxes are all empty. How can I make
the
switch without losing the textbox data ?

Here is the type of code within frmMain that actually switches to frmTwo:

private void mnuGoTofrmTwo_Click(object sender, System.EventArgs e)
{

frmTwo copyfrmTwo = new frmTwo();

copyfrmTwo.Show();

Hide(); // Hide frmMain

}

Here is the type of code within frmTwo that actually switches to frmMain:

private void mnuGoTofrmMin_Click(object sender, System.EventArgs e)
{

frmMain copyfrmMain = new frmMain();

copyfrmMain.Show();

Hide(); // Hide frmTwo

}
 
Back
Top