SRL Archive - Computer Help and Tutorials

[VB.NET] Using My.Settings


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

Simtoon

2009-09-06 03:28:09

Which version are you using?

Sandstorm

2009-09-06 03:35:27

Perfect! Now, can you create a setting from inside the code? Can you delete them? If so, I'm set :D.

~Sandstorm

Rick

2009-09-06 06:20:16

From: Simtoon
Which version are you using?


Visual Basic Express 2008 Express

From: Sandstorm
Perfect! Now, can you create a setting from inside the code? Can you delete them? If so, I'm set :D.

~Sandstorm


This will create a temporary setting during runtime. I'll look into it more tomorrow.

        Dim NewSet As System.Configuration.SettingsProperty
NewSet = New System.Configuration.SettingsProperty(TextBox1.Text)
My.Settings.Properties.Add(NewSet)
My.Settings.Save()

TRiLeZ

2009-09-06 06:30:17

If you want to delete a setting value…


if (Properties.Settings.Default.SavedPassword.Length >= 1) Properties.Settings.Default.SavedPassword = "";

Taken right from my SMTP Client. Its C# not VB though.
What it does is… if the setting contains a value then it will delete it.

Replace SavedPassword with whatever the setting is called.

I Rambozo I

2009-11-23 15:01:34

Lovely, thanks!