Save and Load

I am a novice, I have an application with multiple labels and textboxes, which are filled by the user, I want to be able to save the data in the text boxes, and open it again later with the same data.

Please any help would be great

thanks

poon23

[270 byte] By [poon23] at [2007-12-24]
# 1

there are a few ways of doing this.

  • you can create a simple textfile, and add entries per line, then reload this textfile next time and read them in again, populating it into the appropriate controls

  • you can store it in a database and then retrieve them later once again (expensive for this small task)

  • you can write to an xml file, perhaps serialize it, and load it again next time

    really depends how this application is going to be used and then with this, the appropriate solution should be chosen :-)

  • ahmedilyas at 2007-8-31 > top of Msdn Tech,Visual Basic,Visual Basic General...
    # 2

    ahmedilyas,

    thanks for the response, I am sure that the first option would work just fine for the simpleness of the application.

    So how is this done?

    Thanks

    poon23

    poon23 at 2007-8-31 > top of Msdn Tech,Visual Basic,Visual Basic General...
    # 3

    ok, taking in the first response....you need to open a file and write to it. you need to import the System.IO namespace, so declare it at the top of your class file (Code)

    imports System.IO

    Now, when the user presses, say, "Save" button, a click event will happen, in this event you would want to save the data to the file, so double click the button in designer view (where you create your form in the IDE) to create the event handler.

    Next up, create the file:

    Dim theStreamWriter as new StreamWriter(Application.StartupPath & "\myOutput.txt")

    you are now creating a writer to write to the file specified, in the current directory of the application (where the application is running from: Application.StartupPath)

    So now, you need to get your inputs and write to file. Next line of code would be something like:

    theStreamWriter.WriteLine(Me.theTextBox.Text)

    this will write, to the file, the text that is in the textbox on your form. The same thing happens when taking inputs from say, a combobox, or a listbox, or a label. you just need to find a way of getting its textual selected item/mode/property. So say if I had 3 textboxes, my lines of code:

    theStreamWriter.WriteLine(Me.theTextBox1.Text)

    theStreamWriter.WriteLine(Me.theTextBox2.Text)

    theStreamWriter.WriteLine(Me.theTextBox3.Text)

    would write to the file, the text in each of the textboxes specified. With me so far?

    finally we need to close the file:

    theStreamWriter.Close()

    now to open the file:

    If File.Exists(Application.StartupPath & "\myOutput.txt") then 'Check if the file exists before doing things

    Dim theStreamReader as new StreamReader(Application.StartupPath & "\myOutput.txt")

    'since we stored say, 3 lines in the file (for example) then we can read the 3 lines individually:

    Me.theTextBox1.Text = theStreamReader.ReadLine()

    Me.theTextBox2.Text = theStreamReader.ReadLine()

    Me.theTextBox3.Text = theStreamReader.ReadLine()

    theStreamReader.Close()

    end if

    This is an example, so be sure to modify to your needs. Be sure to implement error checking also. There are a few flaws already with this code but keeping it simple for now for you to understand/follow

    ahmedilyas at 2007-8-31 > top of Msdn Tech,Visual Basic,Visual Basic General...
    # 4

    ahmedilyas,

    Thanks works great appreciate the help

    poon23

    poon23 at 2007-8-31 > top of Msdn Tech,Visual Basic,Visual Basic General...
    # 5
    'Write text
    Private Sub Command1_Click()
    Open "C:\Documents and Settings\kada\My Documents\Textsave\txt3.txt" For Output As #3
    Open "C:\Documents and Settings\kada\My Documents\Textsave\txt2.txt" For Output As #2
    Open "C:\Documents and Settings\kada\My Documents\Textsave\txt1.txt" For Output As #1
    Write #1, Text1.Text
    Close #1
    Write #2, Text2.Text
    Close #2
    Write #3, Text3.Text
    Close #3
    End Sub
    'Read text
    Private Sub Command2_Click()
    Dim aString As String
    Open "C:\Documents and Settings\kada\My Documents\Textsave\txt2.txt" For Input As #2
    Open "C:\Documents and Settings\kada\My Documents\Textsave\txt3.txt" For Input As #3
    Open "C:\Documents and Settings\kada\My Documents\Textsave\txt1.txt" For Input As #1
    Input #1, aString
    Close #1
    Text1.Text = aString
    Input #2, aString
    Close #2
    Text2.Text = aString
    Input #3, aString
    Close #3
    Text3.Text = aString
    End Sub
    georgeskada at 2007-8-31 > top of Msdn Tech,Visual Basic,Visual Basic General...
    # 6
    thats not the correct way of doing things now in .NET - you don't need to do the whole Open/Close, for Input AS etc.... - the proper way is using the code I had supplied, using the StreamWriter/FileStream classes
    ahmedilyas at 2007-8-31 > top of Msdn Tech,Visual Basic,Visual Basic General...
    # 7
    VB.NET does not support those command any more...what you are referring to is the VB6 days, this is all changed in .NET now
    ahmedilyas at 2007-8-31 > top of Msdn Tech,Visual Basic,Visual Basic General...
    # 8
    can you send me a small toturial about vb.net plz a small toturial who can learn you your first programme
    georgeskada at 2007-8-31 > top of Msdn Tech,Visual Basic,Visual Basic General...
    # 9
    you have to revert to reading examples from here, or visiting MSDN, reading other sample code and doing alot of internet searching and reading the documentation :-)
    ahmedilyas at 2007-8-31 > top of Msdn Tech,Visual Basic,Visual Basic General...
    # 10
    plz can you give me a small exemple of programmes in vb.net and just a small programme
    georgeskada at 2007-8-31 > top of Msdn Tech,Visual Basic,Visual Basic General...
    # 11

    Hi,

    I am using similar code to save data from a datagrid to a text file. I manage to save the information but when I look at the text file, the chr(9), chr(10) and chr(13) shown as a "square" and all the data are in one line. Is there a way to save without the "square" on multiple line and with the "TAB" space?

    This is the code to save my data. The "DataCache" is a datagrid. The output file must be a ".txt" file, I am creating an interface to read data from an hardware, so I need to respect the original structure of the file. I am using Visual Basic 2005 Express.

    Dim DataRow As Integer = 0

    Dim DataSaveString As String = ""

    While DataRow <= 27

    DataSaveString = DataSaveString + Me.DataCache.Item(0, DataRow).Value + ChrW(10)

    DataSaveString = DataSaveString + Me.DataCache.Item(1, DataRow).Value + ChrW(10) + ChrW(13)

    DataRow = DataRow + 1

    End While

    Me.TempSaveField.Text = DataSaveString

    Dim TextEncoding As Encoding = Encoding.ASCII

    File.WriteAllText("TestFile.txt", Me.TempSaveField.Text, TextEncoding)

    End Sub

    Draco3d at 2007-8-31 > top of Msdn Tech,Visual Basic,Visual Basic General...