?
Solved

VB.NET Windows Form retaining values on forms

Posted on 2010-09-09
11
Medium Priority
?
656 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 3
  • 3
  • +1
11 Comments
 
LVL 3

Accepted Solution

by:
ghayasurrehman earned 1000 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 86

Expert Comment

by:Mike Tomlinson
ID: 33639775
What version VB.net are you using?...
0
Command Line Tips and Tricks

The command line is a powerful tool at the disposal of every Linux user. Although Linux distros come with beautiful user interfaces, it's worthwhile to learn the command line because it allows you to do a number of things that you otherwise cannot do from the GUI.  

 
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 86

Assisted Solution

by:Mike Tomlinson
Mike Tomlinson earned 500 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 1000 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 500 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 86

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

NFR key for Veeam Agent for Linux

Veeam is happy to provide a free NFR license for one year.  It allows for the non‑production use and valid for five workstations and two servers. Veeam Agent for Linux is a simple backup tool for your Linux installations, both on‑premises and in the public cloud.

Question has a verified solution.

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

Today I had a very interesting conundrum that had to get solved quickly. Needless to say, it wasn't resolved quickly because when we needed it we were very rushed, but as soon as the conference call was over and I took a step back I saw the correct …
Whether you've completed a degree in computer sciences or you're a self-taught programmer, writing your first lines of code in the real world is always a challenge. Here are some of the most common pitfalls for new programmers.
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…
In this brief tutorial Pawel from AdRem Software explains how you can quickly find out which services are running on your network, or what are the IP addresses of servers responsible for each service. Software used is freeware NetCrunch Tools (https…

752 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