How to save contents of multiple forms to a single text file


I have about 6 forms in my VB windows application project. Each form takes user input in different ways (textbox, checkbox,Combobox).  I wanted to store information of all the forms into a single textfile and load forms with user information back from the text file to the corresponding forms.

I could save the contents of textboxes and load them back into the form using Streamwriter and Streamreader to write and read line by line. I want to save the checkboxes value that were checked before to the same text file which I have used for storing textbox values so that when user opens that form again he will see what he had done before. All the textboxes were in one form and all checkboxes are in other form.

Is there any way that I can store the user given data into a single text file and load the data back into corresponding controls i.e., textboxes, checkboxes and the combobox.

thank you in advance

Who is Participating?

Improve company productivity with a Business Account.Sign Up

Daniellus83Connect With a Mentor Commented:
I will give you an (working!) example with the following parts:

3 forms;
  - Form_Main
  - Form1
  - Form2

- Form_Main, with 4 buttons;

  - one button to SAVE all setting,
  - one button to LOAD all settings,

  - one button to OPEN Form1 with 3 items;
     - 2 checkboxes
     - 1 button to SAVE the settings of this form1 to Form_Main

  - one button to OPEN Form2 with 3 items;
     - 2 textboxes
     - 1 button to SAVE the settings of this form2 to Form_Main

The extra code needed in Form1.vb:
    Public myCaller As Form_Main

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        myCaller.Checked1 = CheckBox1.Checked
        myCaller.Checked2 = CheckBox2.Checked
    End Sub

The extra code needed in Form2.vb:
    Public myCaller As Form_Main

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        myCaller.Checked1 = CheckBox1.Checked
        myCaller.Checked2 = CheckBox2.Checked
    End Sub

The code needed in Form_Main

    'Making a clone of the designed forms to be able to use/access them
    'Form1 with checkbox1 and checkbox2 && Form2 with textbox1 and textbox2
    Public Form1Clone As Form1
    Public Form2Clone As Form2

    'Now we define a plavce to store the setting. This is needed because after close a form (e.g. form1) the setting are destroyed.
    'Make sure these variables are NOT set private, else they can't be reached!!
    Public Checked1 As Boolean
    Public Checked2 As Boolean
    Public Text1 As String
    Public Text2 As String

    'Buttons to make Form1&Form2 visible AND load the settings
    Private Sub Show_Form1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Show_Form1.Click
        Form1Clone = New Form1
        Form1Clone.myCaller = Me
        Form1Clone.CheckBox1.Checked = Checked1
        Form1Clone.CheckBox2.Checked = Checked2
    End Sub
    Private Sub Show_Form2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Show_Form2.Click
        Form2Clone = New Form2
        Form2Clone.myCaller = Me
        Form2Clone.TextBox1.Text = Text1
        Form2Clone.TextBox2.Text = Text2
    End Sub

    Private Sub Button_Save_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button_Save.Click

        Dim nr As Integer = FreeFile()
        Dim FileName As String

        If SaveParameterFile.ShowDialog() = DialogResult.OK Then

            'Opening file
            FileName = SaveParameterFile.FileName & ".cnf"
            FileOpen(nr, FileName, OpenMode.Output)

            'Write all settings to file
            WriteLine(nr, Checked1) 'Checkbox1
            WriteLine(nr, Checked2) 'Checkbox2
            WriteLine(nr, Text1) 'Textbox1
            WriteLine(nr, Text2) 'Textbox2

            'Close file
        End If
    End Sub

    Private Sub Button_Load_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button_Load.Click

        Dim nr As Integer = FreeFile()
        Dim value As Boolean
        Dim FileName, text As String

        If LoadParameterFile.ShowDialog() = DialogResult.OK Then

            'Opening file
            FileName = LoadParameterFile.FileName()
            FileOpen(nr, FileName, OpenMode.Input)  'Open file for output.

            'Read piece-by-piece all data from file:

            Input(nr, value)
            Checked1 = value

            Input(nr, value)
            Checked2 = value

            Input(nr, text)
            Text1 = text

            Input(nr, text)
            Text2 = text

            'Close file

        End If

    End Sub

This should do the trick !!

You can simply expand this solution to your needs (6 forms... much items on each form)

Succes on your project... Daniël

>>> You might wanna download my home-made solution (as descibed above):
Assuming you have a function that writes to your file - might declare something like this:

Public Function WriteValues(controlName As String, controlValue As String)
On your form:

dim strTempName as string
dim strTempVal as string

Dim ctl As Control

For Each ctl In Me.Controls
 Select Case ctl.GetType.ToString
   Case "CheckBox"
      strTempName = Ctype(Ctl, CheckBox).Name
      strTempVal = Ctype(Ctl, checkbox).Checked.ToString
      WriteValue(strTempName, strTempVal)      
   Case "TextBox"
      strTempName = Ctype(Ctl, TextBox).Name
      strTempVal = Ctype(Ctl, checkbox).Text
      WriteValue(strTempName, strTempVal)  

This would write all control names and their values to the file.

When loading, you could reverse the logic.

Does that give you some ideas?
Hi there..
yes you can...
use module.
name and instantiate all the forms in there.
make all the controls you want to get/set as "freinds"
use text reader/writer.
save all the control contents in a specific pattern and order. e.g:
    Form Name : Control Name : Content

Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

I've done this same thing using the Serialzie functions that come with .NET
Yeah just do as said as above and use a module, but personally speaking from my experience doing this same task at work. If you are going to do this for multiple users, I personally would make a database (ie Access) and store everything to a corresponding column with each new user being a row. Then using some info that is unique per user(P-KEY) to  find the users info and reload the module.

Public Logon as string
Public BDAY as string

Read: form load maybe -main form

   Dim oldbc As OleDb.OleDbConnection
        Dim stroldb As String
        Dim cmsoldb As OleDb.OleDbCommand
        Dim oldbread As OleDb.OleDbDataReader
        oldbc = New OleDb.OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;User ID=Admin; Data Source= URDATABASE.mdb")
        stroldb = "Select Logon From USERTABLE Where (ur thing to look for)"
        cmsoldb = New OleDb.OleDbCommand(stroldb, oldbc)
        oldbread = cmsoldb.ExecuteReader()
        While (oldbread.Read)
            Logon = oldbread("LOGON") ‘
            BDAY =  oldbread("B-Day”)
        End While

Then In ur form where its need.. During load do

This all so makes it portable if u were to put the database on a sever and distribute ur app...

Good Luck,

Textbox1.text = Logn
Combobox1.text = BDAY ‘for example

Save- Just do insert into data base -
i agree with the comment above to look into storing your data using the serialization functions.  you could store your data in a class object and then write that object to a file using serialization.

I just typed an extra page with explaination to support my last example... but as alway I pressed the BACKSPACE and my Internet Explorer did go to a previous page.... Lost all my text I typed in the textbox below this page ;-((((( aaarrrgggghhhh

You'd best download the source-code I provided with the link! You could see a working example.

If you'd wanna read some more explaination on this, I would be glad to type it again for you. But I programmed the example as easy as possible for you. Just post a msg if you need some more info.
BeginnerVB_NetAuthor Commented:
Hi all

Thank you for your replies..
To jrandallsexton,DotNetLover_Baan,thenrich ,xorcrack
I have to try out your ideas.. give me some more time...As I am a beginner I need to understand those concepts and try out...

To Jojo1771
In my VB Project there are multiple users ... each user will go through all the six forms more than once.... Thus I want to store the contents of six forms into a single text file.....each user can have more than one text file....I will store the text files into database...under his name.

To Daniellus83
Thank you very much for sending me the demo. I got what you are trying to do. but in my project I dont want  a main form. I wanted to store data from the forms to a single text file with some strings added to seperate data of different forms. for example the contents of text file will be some thing like this
Form 1 Start
the data in form one will be stored here
Form 1 End
Form 2 Start
the data in form 2 will be stored here
Form 2 End
So when you open that file, the software should read the data between the strings Form 1 Start and Form 1 End and load it into form 1 at appropriate controls

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.