Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

VB.NET Windows Form retaining values on forms

Posted on 2010-09-09
11
Medium Priority
?
662 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
Veeam Disaster Recovery in Microsoft Azure

Veeam PN for Microsoft Azure is a FREE solution designed to simplify and automate the setup of a DR site in Microsoft Azure using lightweight software-defined networking. It reduces the complexity of VPN deployments and is designed for businesses of ALL sizes.

 
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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
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…
Please read the paragraph below before following the instructions in the video — there are important caveats in the paragraph that I did not mention in the video. If your PaperPort 12 or PaperPort 14 is failing to start, or crashing, or hanging, …

610 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