Wednesday, November 4, 2009

My Settings (persisting data)

My Settings is a way to store data and settings used by your program for use on elsewhere (or at a later time. Say a user wants currency to show Euros instead of dollars, you can save this preference in My.Settings. Here's how:


Start by building a form with 2 checkboxes, a textbox, and a button.




Next go to Project->'yourAppName' Properties




Choose Settings on the left and create some properties to store your data:



Note that the default is String, but this can be changed to almost any data type. For collections of data you want to store (like history) choose StringCollection.





Once you choose Boolean as your type like the example above, False is the default value, you can change this and use it how you like.

One unintuitive (to me at least) items was the string collection. For this to be setup for easy use, you need to click the elipses (...) to the right and enter in a sample piece of data. If you want there to be standard entries, this is where you enter them:




Now you'll notice that some XML is stored in the value column, this is how My.Settings stores your data. Use the elipses to open the screen back up and delete what you just entered, we want it blank to start so you can see the results.


Double-click the button to get to the click event and enter the following:


My.Settings.CB1Value = Me.CheckBox1.Checked

My.Settings.CB2Value = Me.CheckBox2.Checked

My.Settings.TB1Value = Me.TextBox1.Text

My.Settings.Save()
 
This changes the values in the My.Settings to our checkbox and textbox values and then saves them, the values will stay until you close the program but will then clear back to the default unless you save them.

If you run the program, type some data in the textbox and then check one or both of the checkboxes and click your button. If you now go back to the Setting tab in your properties screen, you will see that nothing looks changed. This is because the data is saved in a fairly safe location that the average Joe wouldn't know how to get to. This is pretty secure, but I wouldn't go putting my SSN and credit card numbers in there without more security.

To view the data saved in the My.Settings, we need to bind that data to something, so add a listbox and another button to your form. On the click event of the new button add the following:


Me.ListBox1.DataSource = My.Settings.SettingsColl

Before we run this, we want to concatenate all of our options into one string that we can save to be used later. You can think of this in terms of a survey, we want to record all of the responses in one line per person. We will do that in a seperate routine that we will call from the first button click:


Sub storestring()

Dim myString As String

myString = Me.CheckBox1.Checked

myString &= "~" & Me.CheckBox2.Checked

myString &= "~" & Me.TextBox1.Text

My.Settings.SettingsColl.Add(myString)

My.Settings.Save()

End Sub

And the code to add to the first button click:

storestring()
Now run the project again, this time put something different in the textbox and select a different combination of checkboxes. Now click the second button.




As you can see, the data we put in is stored in a string collection in the listbox, each item seperated by a ~.


Doubleclicking on the listbox should bring up the event for SelectedIndexChanged. In this event place the following code:


Dim currentString As String
currentString = Me.ListBox1.SelectedItem.ToString()
Me.CheckBox1.Checked = Split(currentString, "~")(0)
Me.CheckBox2.Checked = Split(currentString, "~")(1)
Me.TextBox1.Text = Split(currentString, "~")(2)


What this does is parses the string, and sets the values of our controls to the saved values each time a user makes a selection in the listbox.


Thats it, there are endless possibilities with My.Settings and I'm sure you will find many uses for them. Feel free to post any questions or unique usages.


Here is the entire code:



Public Class Form1
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
My.Settings.CB1Value = Me.CheckBox1.Checked
My.Settings.CB2Value = Me.CheckBox2.Checked
My.Settings.TB1Value = Me.TextBox1.Text
My.Settings.Save()
storestring()
End Sub
Sub storestring()
Dim myString As String
myString = Me.CheckBox1.Checked
myString &= "~" & Me.CheckBox2.Checked
myString &= "~" & Me.TextBox1.Text
My.Settings.SettingsColl.Add(myString)
My.Settings.Save()
End Sub
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
Me.ListBox1.DataSource = My.Settings.SettingsColl
End Sub
Private Sub ListBox1_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ListBox1.SelectedIndexChanged
Dim currentString As String
currentString = Me.ListBox1.SelectedItem.ToString()
Me.CheckBox1.Checked = Split(currentString, "~")(0)
Me.CheckBox2.Checked = Split(currentString, "~")(1)
Me.TextBox1.Text = Split(currentString, "~")(2)
End Sub
End Class