DevCity.NET - http://devcity.net
Creating, Using and Saving My.Settings Default Values in Visual Basic 2005
http://devcity.net/Articles/232/1/article.aspx
Dave Barr
Not much to tell, so for now view some of my custom logo designs, and a photograph of mine that made it to the local newspaper. 
by Dave Barr
Published on 5/29/2006
 

This is a short and very basic tutorial about creating, using and saving My.Settings default values in Visual Basic 2005. By completing this tutorial you will learn the basic steps involved in creating Settings that can be saved and re-loaded the next time your application is run...


Getting Started

This is a short and very basic tutorial about creating, using and saving My.Settings default values in Visual Basic 2005. By completing this tutorial you will learn the basic steps involved in creating Settings that can be saved and re-loaded the next time your application is run.

Open Visual Studio 2005, create a new project and name it: VbCity Sample Project

 

Press the "Project" menu item, then press the "VbCity Sample Project Properties..." sub-menu. The VbCity Sample Project Properties Window will open. Press the "Settings" Tab located near the center left of the window.

 

The Settings Window is where you add variables/constants that can be used anywhere throughout the entire project. If you take a peek at the options listed in the 'Scope' ComboBox, you will see a choice of 'User' or 'Application'. Application Scope is used as a constant, which means the value you add in design-view cannot be changed during runtime.

A good example of Application Scope is a database connection string. Normally, a connection string is set by the programmer and isn't changed by the user.

User Scope is used as a variable, which means you can change its value during runtime. Another great advantage of a User Scope variable is that you can save the value to reuse next time the application is run. All of the examples in this tutorial will use the User Scope.

 

Adding User Scope Settings

 

Fill in the following properties for the first Setting:

Name: DefaultFont

Type: System.Drawing.Font

Scope: User

In the Value column press the ellipse (...) button. Since you have chosen 'font' as the setting type, the Font Dialog opens. Use the following font settings:

Font: Microsoft Sans Serif

Font Style: Italic

Size: 12

Script: Western

 

Create three more Settings. For the first, use the following property settings:

Name: DefaultFontColor

Type: System.DrawingColor

Scope: User

For the Value you can type, or choose the color itself. Use the color 'Navy'.

 

For the second setting, use:

Name: DefaultFormColor

Type: System.DrawingColor

Scope: User

Value: Lavender (Found under the 'Web' color tab)

 

For the third setting, use:

Name: DefaultName

Type: String

Scope: User

Value: 3-bSoftware.com

 

 

Your Settings should look like the image above.

 

Add controls to the form

That's all the settings we need for this tutorial so save your changes. Close the Settings Window and set focus back to the Form1 Designer Window.

Add a TextBox to the form and stretch it almost the entire width of the form. Now select the form itself and double-click to open the code window for the Form1_Load event. Type the following code inside the Form1_Load event:

 

Me.BackColor = My.Settings.DefaultFormColor

Me.TextBox1.Font = My.Settings.DefaultFont

Me.TextBox1.ForeColor = My.Settings.DefaultFontColor

Me.TextBox1.Text = My.Settings.DefaultName

 

Press F5 to see your progress in action. You should have a form that looks similar to the following image:

 

Saving Setting value changes at runtime

Add a Form1_FormClosing event. Type this code:

My.Settings.DefaultName = Me.TextBox1.Text

Now press F5 to run. This time, change the text in the textbox from 3-bSoftware.com to your name. Close the form. Close the project. Close Visual Studio. If you like, you can restart your computer too.

Next time you run the "VbCity Sample Project" you will see the name you just typed in the TextBox.

You can use this same process to save any of the 'User Scope' variable changes. Try practising by saving different values for the DefaultColors, or DefaultFont. Good luck!

Thank you for reading my article, Dave.