Jonathan
Jonathan

Reputation: 255

Simple way to save and load data Visual Basic

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.

Upvotes: 11

Views: 71914

Answers (3)

Doug Null
Doug Null

Reputation: 8327

Complete description of solution, from Microsoft.

One great thing about Microsoft has been fantastic documentation for at least the last 25 years. MSDN quality is on par even with Stackoverflow (software mecca)

Upvotes: 0

Mark Hall
Mark Hall

Reputation: 54532

If it is a User setting you can use the built in My.Settings Object to Save and Load.

From above Link:

The My.Settings object provides access to the application's settings and allows you to dynamically store and retrieve property settings and other information for your application.

You can create the Setting in your Project Property's Settings Section:

Which you can access like this.

dim MyTemp as String = My.Settings.MySetting

and Save it like this

My.Settings.MySetting = "StringValue"
My.Settings.Save()

This will be persisted in your Config file like this:

<userSettings>
    <WindowsApplication11.My.MySettings>
        <setting name="MySetting" serializeAs="String">
            <value>TempValue</value>
        </setting>
    </WindowsApplication11.My.MySettings>
</userSettings>

Upvotes: 21

Steven Doggart
Steven Doggart

Reputation: 43743

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.

Upvotes: 4

Related Questions