Solved

VB.NET Windows Form retaining values on forms

Posted on 2010-09-09
11
641 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
Comment Utility
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
Comment Utility
Would this ToggleForm Sub go on every form them?  Where would it get called from?

Thanks!
0
 
LVL 85

Expert Comment

by:Mike Tomlinson
Comment Utility
What version VB.net are you using?...
0
 
LVL 3

Expert Comment

by:ghayasurrehman
Comment Utility
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
Comment Utility
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
Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

 
LVL 85

Assisted Solution

by:Mike Tomlinson
Mike Tomlinson earned 125 total points
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
Oooops, @Idle_Mind, I posted without refreshing the browser this time. :=)
0
 

Author Comment

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

Expert Comment

by:Mike Tomlinson
Comment Utility
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

How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

Join & Write a Comment

The ECB site provides FX rates for major currencies since its inception in 1999 in the form of an XML feed. The files have the following format (reducted for brevity) (CODE) There are three files available HERE (http://www.ecb.europa.eu/stats/exch…
A theme is a collection of property settings that allow you to define the look of pages and controls, and then apply the look consistently across pages in an application. Themes can be made up of a set of elements: skins, style sheets, images, and o…
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…
This video explains how to create simple products associated to Magento configurable product and offers fast way of their generation with Store Manager for Magento tool.

728 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

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now