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]
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
'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
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 <= 27DataSaveString = 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 WhileMe.TempSaveField.Text = DataSaveStringDim TextEncoding As Encoding = Encoding.ASCII File.WriteAllText(
"TestFile.txt", Me.TempSaveField.Text, TextEncoding)End Sub