Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Simple way to save and load data Visual Basic

Tags:

vb.net

save

load

I was wondering what is the easiest way to save and load data through different forms in vb. I just want to save 3 textbox.text that a user saves and be able to load it on a different form.

like image 278
Jonathan Avatar asked May 23 '12 01:05

Jonathan


People also ask

How do you save in Visual Basic?

Select File > Save frmCounter As .... The Save Form As dialog box appears. The Save in: textbox shows the name of the folder where the form will be saved. You should create a new folder for this project.

How data is stored in Visual Basic?

You can use serialization to persist an object's data between instances, which enables you to store values and retrieve them the next time that the object is instantiated. In Visual Basic, to store simple data, such as a name or number, you can use the My. Settings object.

How do you create a Save button in Visual Basic?

Drag and drop a button from the VB toolbox to the form in which you want to display the auto-save button. VS automatically draws the button, but use your mouse to set up the button size.


1 Answers

The simplest option would be to save them to a simple delimited text file. For instance, this would save the values in a pipe-delimited file:

File.WriteAllText("C:\Data.txt", String.Join("|", new String() {TextBox1.Text, TextBox2.Text, TextBox3.Text}))

And this would read it in:

Dim values() as String = File.ReadAllText("C:\Data.txt").Split("|"c)
TextBox1.Text = values(0)
TextBox2.Text = values(1)
TextBox3.Text = values(2)

However, it's not smart to save to a file in the root directory. The safest thing would be to store it in a file in Isolated Storage. Also, it would be even better to store it in XML. This could be easily done with serialization.

like image 190
Steven Doggart Avatar answered Oct 17 '22 10:10

Steven Doggart