Our starting application will be one of the easiest uses of settings that I can think of. We'll simply save a selected background color (BackColor) bound to a Form component. A new BackColor setting will be selected and "persisted" (saved so it will be used in future executions of the same program) in the Click event for a Button.
The settings file that is saved with the application is called app.config and it doesn't exist with a new application. So the first thing that needs to be done is to create the file. You can do this in several ways. We'll use the Property window to create our setting.
Step 1 - Create a new standard Windows application. Drag a Button and a ColorDialog component onto the form from the Toolbox. (These components are only necessary because we'll specify the value of the setting in code later on.) In the Properties window, scroll up and open the Application Settings Property Binding dialog. A binding isn't quite the same thing as a property. A binding associates an object, in this case the Form object, with some other data. The Form object here is bound to the BackColor property data.
--------
Click Here to display the illustration
Click the Back button on your browser to return
--------
Step 2 - Click the "ellipsis" button (with the three dots) to open the Application Settings for Form1 dialog window. Scroll up to find the BackColor property. Click the down arrow for the BackColor property value and then click the "New ..." link at the bottom.
--------
Click Here to display the illustration
Click the Back button on your browser to return
--------
Step 3 - In the New Application Setting dialog, give your setting a name (I used myBackColor) and then click OK. Notice that I didn't actually change the BackColor for the form. The purpose of this step is to simply create the app.config file. If you're watching carefully, you'll see it pop into existance in Solution Explorer. This is an XML file which now has a custom setting entry for BackColor. The Scope setting can be either User (read/write) or Application (read only). Leave the Scope setting set to User because we'll actually change the setting in code in the next step.
--------
Click Here to display the illustration
Click the Back button on your browser to return
--------
You can double-click on the app.config file in Solution Explorer to view the XML configuration information created. Only this section of the file actually creates the BackColor setting.
<userSettings>
<WindowsApplication1.My.MySettings>
<setting name="myBackColor" serializeAs="String">
<value>Control</value>
</setting>
</WindowsApplication1.My.MySettings>
</userSettings>
--------
Click Here to display the illustration
Click the Back button on your browser to return
--------
Step 4 - We could have changed the color setting directly in the designer dialog shown in the illustrations above. Instead, the setting will be made in code to show how that works. In the Click event for the Button component, add this code:
ColorDialog1.ShowDialog()
My.Settings.myBackColor = ColorDialog1.Color
That's all it takes! Visual Basic, in contrast to C# and other languages, saves the settings automatically when the form is closed.
To see Application Settings in action, start the application and click the button. Select any color from the color dialog and the background will change to that color. Then close the application. Restart the application and the backcolor will still be the same color that it was when you closed it! (Stopping the debug session won't save it however. You must close the form.)
The Properties designer dialog will let you add settings and change existing settings bound to controls, but you can't delete them there. (You can change a setting back to None but the setting in App.Config will still be there.) There are more ways to create settings. The next page tells you what they are!

