Solved

VB.NET Windows Form retaining values on forms

Posted on 2010-09-09
11
644 Views
Last Modified: 2012-05-10
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

0
Comment
Question by:geoffsweb
  • 3
  • 3
  • 3
  • +1
11 Comments
 
LVL 3

Accepted Solution

by:
ghayasurrehman earned 250 total points
ID: 33639513
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
 

Author Comment

by:geoffsweb
ID: 33639688
Would this ToggleForm Sub go on every form them?  Where would it get called from?

Thanks!
0
 
LVL 85

Expert Comment

by:Mike Tomlinson
ID: 33639775
What version VB.net are you using?...
0
3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

 
LVL 3

Expert Comment

by:ghayasurrehman
ID: 33639792
you sholuld have one main form which will be a MDI container and all other forms will be its child
0
 

Author Comment

by:geoffsweb
ID: 33640019
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
 
LVL 85

Assisted Solution

by:Mike Tomlinson
Mike Tomlinson earned 125 total points
ID: 33640064
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
 
LVL 3

Assisted Solution

by:ghayasurrehman
ghayasurrehman earned 250 total points
ID: 33640068
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
 
LVL 17

Assisted Solution

by:Zhaolai
Zhaolai earned 125 total points
ID: 33640070
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
 
LVL 17

Expert Comment

by:Zhaolai
ID: 33640096
Oooops, @Idle_Mind, I posted without refreshing the browser this time. :=)
0
 

Author Comment

by:geoffsweb
ID: 33640155
OOOHHHHH, thanks, I'll try all this.... maybe I was looking at a VS 2003 example??!!!
0
 
LVL 85

Expert Comment

by:Mike Tomlinson
ID: 33640211
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

Featured Post

Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
VB net, code to modify xlsx file, and errors when it is ran on NON-developer PC... 2 23
VB.NET 1 21
write xml in vb.net 2 24
vb.net winforms sizing/resolution? 4 30
After several hours of googling I could not gather any information on this topic. There are several ways of controlling the USB port connected to any storage device. The best example of that is by changing the registry value of "HKEY_LOCAL_MACHINE\S…
A long time ago (May 2011), I have written an article showing you how to create a DLL using Visual Studio 2005 to be hosted in SQL Server 2005. That was valid at that time and it is still valid if you are still using these versions. You can still re…
This Micro Tutorial demonstrates using Microsoft Excel pivot tables, how to reverse engineer competitors' marketing strategies through backlinks.
This is Part 3 in a 3-part series on Experts Exchange to discuss error handling in VBA code written for Excel. Part 1 of this series discussed basic error handling code using VBA. http://www.experts-exchange.com/videos/1478/Excel-Error-Handlin…

785 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question