Persisting the values of Controls on your Form

This is a common situation and a frequently asked question on various technical forums and websites.

  • I want the controls to retain values after the form/application is closed and opened again.
  • I want to persist the values in textbox after my application is restarted.
  • I want the form to open exactly how I left it the last time.
  • I want the form to remember its state and persist it. etc.etc.
  • Today we will see how to resolve this problem in our VB.NET applications.

The Problem

Persist the values of controls on my form / Persist the TextBox values on my form.

The Solution

If you want the controls to persist the values after application is closed and started again, you will need to save the values to disk and load it again when the application starts the next time.

In VB.NET, the easiest way is to use the My.Settings class, since it is already built to cater to such kind of situations.

In this demo, I will show how to persist the TextBox values on the form. But this method is not limited to TextBox or their Text property. You can save anything you want to, using similar way.

1. Open project properties, Settings tab.

2. Add a setting

Name: TextBoxValues
Type: System.Collections.Specialized.StringCollection
Scope: User

3. Add the following code to your form:

Dim textBoxes() As TextBox

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    '' add all textbox names here whose value you want to persist.
    textBoxes = New TextBox() {TextBox1, TextBox2, TextBox3, TextBox4}

    With My.Settings
        If .TextBoxValues Is Nothing Then .TextBoxValues = New System.Collections.Specialized.StringCollection
        For i = 0 To textBoxes.Length - 1
            If .TextBoxValues.Count <= i Then .TextBoxValues.Add("")
            textBoxes(i).Text = .TextBoxValues(i)
        Next
    End With
End Sub

Private Sub Form1_FormClosing(ByVal sender As Object, ByVal e As System.Windows.Forms.FormClosingEventArgs) Handles Me.FormClosing
    For i = 0 To textBoxes.Length - 1
        My.Settings.TextBoxValues(i) = textBoxes(i).Text
    Next
    My.Settings.Save()
End Sub

4. Run the application. Type something in your textboxes. Then close the application and restart. Observe that now the textboxes retain their values between application restarts.

How this Works

We first create a Setting named TextBoxValues which will hold the values of all our textboxes (or the control values we want to persist). It is of type StringCollection so that we don’t need to create a separate entry for each control/textbox whose value we want to persist.

In the Form_Load we initialize the TextBoxValues collection is automatically initialized with the values from the Settings file. We just fill our controls/textboxes with values from here. The additional code you see is for the first application run when there is no entry in the Settings file.

When closing the form, we write back the values from controls/textboxes to the TextBoxValues collection. This is so that we can load it from there the next time our application starts.

Advertisements

4 Responses to “Persisting the values of Controls on your Form”

  1. jayson Says:

    how do you save the values in textboxes if you have datagrid full of user ids that have a separate update screen

  2. Rodrigo Says:

    For me…only works if i declare “dim i as integer”

  3. ravinder Says:

    what would be the c# code for this.
    With My.Settings
    If .TextBoxValues Is Nothing Then .TextBoxValues = New System.Collections.Specialized.StringCollection
    For i = 0 To textBoxes.Length – 1
    If .TextBoxValues.Count <= i Then .TextBoxValues.Add("")
    textBoxes(i).Text = .TextBoxValues(i)
    Next
    End With

    thanks in advance

  4. ravinder Says:

    With My.Settings
    If .TextBoxValues Is Nothing Then .TextBoxValues = New System.Collections.Specialized.StringCollection
    For i = 0 To textBoxes.Length – 1
    If .TextBoxValues.Count <= i Then .TextBoxValues.Add("")
    textBoxes(i).Text = .TextBoxValues(i)
    Next
    End With


Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: