Rick
2009-09-06 03:14:15
A tutorial on My.Settings by r!ch!e
In this tutorial I will teach you how to use the My.Settings feature in Visual Basic.
My.Settings is an easy way to store User Settings without having to utilize other files, such as ini's or xml files.
Start by creating a new Windows Form Application Project.
Name it whatever you like, and chuck a button onto that form.

Now, put a Checkbox, Radio Button, Textbox, and a Combobox onto the form.

So you've got all those components on your form, but how do you save the last input values when the form closes ?
We use My.Settings of course ! :p
To use My.Settings you're going to want to go to Project -> Properties

Then click on the Settings Tab

Okay, so the first component we want to save on the form is the Checkbox. So we want to save a Boolean Value [True/False, Checked/Unchecked].
Change the values of the boxes to look like this:

We also have our other components, so to save time set up your screen as follows: [I'm assuming you know what values are associated with each Component]

Now for some code :)
Go back to your Form Design, and double click it to create a Form Load Sub.
In your Form Load sub add this code:
' Load Settings
Me.CheckBox1.Checked = My.Settings.Checkbox
Me.RadioButton1.Checked = My.Settings.Radiobutton
Me.TextBox1.Text = My.Settings.Textbox
Me.ComboBox1.Text = My.Settings.Combobox
This code sets all the stored values into your components when the form loads.
How about saving those values ?
Add this code:
Private Sub Form1_FormClosing(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.FormClosing
' Save Settings
My.Settings.Checkbox = Me.CheckBox1.Checked
My.Settings.Radiobutton = Me.RadioButton1.Checked
My.Settings.Textbox = Me.TextBox1.Text
My.Settings.Combobox = Me.ComboBox1.Text
End Sub
This code adds a Form Closing Event [When the form closes it performs this code] and stores the value of the components into the My.Settings.Setting
You can also add the save settings button code
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
' Save Settings
My.Settings.Checkbox = Me.CheckBox1.Checked
My.Settings.Radiobutton = Me.RadioButton1.Checked
My.Settings.Textbox = Me.TextBox1.Text
My.Settings.Combobox = Me.ComboBox1.Text
End Sub
Press F5 to Run/Debug your application. You can now mess around with the values, and check that it saves each one.
Final Product:

By following this tutorial you should have a basic idea of how to use My.Settings and be able to adapt it to your needs. I am going to attach the final product and sauce in a minute :)
Thanks for reading my first tutorial.
~ r!ch!e