VB.NET Windows Form retaining values on forms

VB.NET newbie here...
I have a vb.net windows application set up. It has multiple forms, about 6 in all. There's a main menu form, and then 5 other forms to collect data. The user will collect data on each form and bounce back and forth between forms. How do I get the forms to retain the data that has been entered into the text boxes and/or other controls on the forms? For example, they might fill out a few textboxes on a form, but then need to go to a different form and fill something out... and then back to the previous form. Right now, when they come back to the form, all the data previously entered in to the text boxes is gone. I am not Closing the form, just hiding it.  

Any help would be appreciated. Thanks in advance.
This is what I have on Form1, my main menu page:

Public Class frmForm1
Dim frmVS As New frmForm2


Private Sub btnForm2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnForm2.Click

frmVS.Show()
Me.Hide()

End Sub


and then on Form2, I have a main menu button (to take me back to the main menu when they're done filling out data on the form... but maybe not completely done, so they don't want to save data yet)

Private Sub btnMainMenu_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnMainMenu.Click
Dim frm As New frmForm1
frm.Show()
Me.Hide()

End Sub

End Class

so if I fill out data on Form2, and then leave it there and click the MainMenu button, I go to the main menu... but then I need to click back to Form2 and still have the data previously entered there still there in the text boxes...

Open in new window

geoffswebAsked:
Who is Participating?
 
ghayasurrehmanCommented:
1) when navigating between forms, do not close the form, instead of closing the form use hide() function
2) do not create a new instance of form every time, create global variables for the form and if the form is already instantiated then do not instantiate it again, see the code

Public Class frmForm1
Dim frmVS As New frmForm2
Dim frm As New frmForm1

Private Sub btnForm2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnForm2.Click

frmVS.Show()
Me.Hide()

End Sub



Private Sub btnMainMenu_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnMainMenu.Click

frm.Show()
Me.Hide()

End Sub

End Class


'OR USE THE CODE BELOW

Public Class frmForm1

Dim frm1 As New form1
Dim frm2 As New form2
Dim frm3 As New form3
Dim frm4 As New form4
Dim frm5 As New form5
Dim frm6 As New form6


Private Sub ToggleForm(ByVal formId As int)

switch(formId)
case 1: frm1.Show(); frm2.Hide();frm3.Hide();frm4.Hide();frm5.Hide();frm6.Hide(); break;

case 2: frm1.Hide(); frm2.Show();frm3.Hide();frm4.Hide();frm5.Hide();frm6.Hide(); break;
case 3: 'and so on
 break;
case 4:'and so on
 break;
case 5:'and so on
 break;
case 6:'and so on
 break;
end switch

End Sub

End Class

Open in new window

0
 
geoffswebAuthor Commented:
Would this ToggleForm Sub go on every form them?  Where would it get called from?

Thanks!
0
 
Mike TomlinsonMiddle School Assistant TeacherCommented:
What version VB.net are you using?...
0
Get expert help—faster!

Need expert help—fast? Use the Help Bell for personalized assistance getting answers to your important questions.

 
ghayasurrehmanCommented:
you sholuld have one main form which will be a MDI container and all other forms will be its child
0
 
geoffswebAuthor Commented:
I'm using Visual Studio 2008.

ghayasurrehman:  oops, I didn't set it up like that... every form is like the other form.... no parent/child relationship.  this is my first go around with a Windows Application.... I'm use to Web Applications.
0
 
Mike TomlinsonMiddle School Assistant TeacherCommented:
With VB.Net 2005 (and above) you can use what is known as the "default instance" of the forms.  This is done by using JUST THE NAME of the form without the "New" keyword.

For example, you can display Form2 from anywhere with:

    Form2.Show()

Similarly, you can hide it with:

    Form2.Hide()

Again, this will work from anywhere and there is only ever ONE default instance.

So just get rid of all lines where you create an instance with "New".  You do not need "global" declarations to keep references to the forms either...

Form1 could show Form2 like this:

    Form2.Show()
    Me.Hide()

Form2 can show Form1 like this:

    Form1.Show()
    Me.Hide()

Etc....
0
 
ghayasurrehmanCommented:
then use this

Public Class frmForm1
Dim frmVS As New frmForm2
Dim frm As New frmForm1

Private Sub btnForm2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnForm2.Click

frmVS.Show()
Me.Hide()

End Sub



Private Sub btnMainMenu_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnMainMenu.Click

frm.Show()
Me.Hide()

End Sub

End Class
0
 
ZhaolaiCommented:
Don't think you need to create new instance of form in order to call it. You can just call the form itself. So in Form1, do this:

Private Sub btnForm2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnForm2.Click
    Me.Hide()
    frmForm2.ShowDialog()
End Sub

In Form2, do this:

Private Sub btnMainMenu_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnMainMenu.Click
    Me.Hide()
    frmForm1.ShowDialog()
End Sub

0
 
ZhaolaiCommented:
Oooops, @Idle_Mind, I posted without refreshing the browser this time. :=)
0
 
geoffswebAuthor Commented:
OOOHHHHH, thanks, I'll try all this.... maybe I was looking at a VS 2003 example??!!!
0
 
Mike TomlinsonMiddle School Assistant TeacherCommented:
Perhaps!  There is obviously more than one way to do it...I think the default instance approach is the easiest though.

Default instances were around in the old classic VB6 days but were left out of VB.Net 2002/2003.  There were brought back in VB.Net 2005 and I don't think are going anywhere!

C# programmers have to manually implement the singleton pattern on their forms to get a similar mechanism...
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.