Solved

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

Posted on 2004-08-04
8
260 Views
Last Modified: 2010-04-23
Hi

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
ricky


0
Comment
Question by:BeginnerVB_Net
8 Comments
 
LVL 4

Expert Comment

by:jrandallsexton
ID: 11720237
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)  
Next

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?
0
 
LVL 18

Expert Comment

by:DotNetLover_Baan
ID: 11720273
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

-Baan
0
 
LVL 5

Expert Comment

by:thenrich
ID: 11720522
I've done this same thing using the Serialzie functions that come with .NET
0
 
LVL 5

Expert Comment

by:Jojo1771
ID: 11722803
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.
EXAMPLE:

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)
        oldbc.Open()
        oldbread = cmsoldb.ExecuteReader()
        While (oldbread.Read)
            Logon = oldbread("LOGON") ‘
            BDAY =  oldbread("B-Day”)
        End While
        oldbc.Close()

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,
Jojo

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


Save- Just do insert into data base -
0
Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

 
LVL 1

Expert Comment

by:xorcrack
ID: 11722884
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.
0
 
LVL 4

Accepted Solution

by:
Daniellus83 earned 500 total points
ID: 11724847
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
        Form1Clone.Show()
    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
        Form2Clone.Show()
    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
            FileClose(nr)
        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:

            'Form1Clone:Checkbox1
            Input(nr, value)
            Debug.WriteLine(value)
            Checked1 = value

            'Form1Clone:Checkbox2
            Input(nr, value)
            Debug.WriteLine(value)
            Checked2 = value

            'Form2Clone:Textbox1
            Input(nr, text)
            Debug.WriteLine(text)
            Text1 = text

            'Form2Clone:Textbox2
            Input(nr, text)
            Debug.WriteLine(text)
            Text2 = text

            'Close file
            FileClose(nr)

        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): http://www.danieltrommel.com/download/expl01.zip
0
 
LVL 4

Expert Comment

by:Daniellus83
ID: 11725634

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.
0
 

Author Comment

by:BeginnerVB_Net
ID: 11727596
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
etc.,
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



0

Featured Post

What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

Join & Write a Comment

Well, all of us have seen the multiple EXCEL.EXE's in task manager that won't die even if you call the .close, .dispose methods. Try this method to kill any excels in memory. You can copy the kill function to create a check function and replace the …
Microsoft Reports are based on a report definition, which is an XML file that describes data and layout for the report, with a different extension. You can create a client-side report definition language (*.rdlc) file with Visual Studio, and build g…
It is a freely distributed piece of software for such tasks as photo retouching, image composition and image authoring. It works on many operating systems, in many languages.
Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.

758 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

19 Experts available now in Live!

Get 1:1 Help Now