Settings in Visual Basic development are particular objects that provide a managed way of manipulating applications and user-level settings. For example, you could provide users with the ability to customize options in the user interface of your application. To save and read such customizations to and from disk, you can use .NET settings. My Project provides a tab named Settings that enables you to specify information at the application or user level (see Figure 3.2).
As you can see in Figure 3.2, you can specify an identifier for each setting, a type (which you’ll understand better after you read Chapter 4), the scope, and the value. For the scope, User
means that only the user who runs the application can use the setting. Application
means that the setting is available at the application level, independently from the user who logged in to Windows (and therefore is available to all users). Like resources, settings are also described in detail in Chapter 19. Settings are represented by a simple XML file, named Settings.settings. Listing 3.6 shows the content of Settings.settings after the addition of the sample setting.
Viewing the Settings.Settings File with the Xml Editor
To view the XML content for the Settings.settings file, right-click the filename in Solution Explorer, select Open With, and select Xml (Text) Editor in the Open With dialog box.
<?xml version='1.0' encoding='utf-8'?>
<SettingsFile xmlns="http://schemas.microsoft.com/VisualStudio/2004/01/settings"
CurrentProfile="(Default)" GeneratedClassNamespace="My" GeneratedClass-
Name="MySettings" UseMySettingsClassName="true">
<Profiles />
<Settings>
<Setting Name="StartState" Type="System.Boolean" Scope="User">
<Value Profile="(Default)">True</Value>
</Setting>
</Settings>
</SettingsFile>
In this XML markup, you can see the presence of a Settings node that stores as many Setting
elements and as many settings as you specify in My Project. In our example there is just one Setting
element that contains the name of the setting, the data type, the scope, and the default value (which means the value you specify in My Project).
The Settings.settings file also has Visual Basic support, which is represented by another file, named Settings.designer.vb. You do not need to examine all the content of this file as just a couple parts of the code are interesting. First, this file implements a property named Settings
that is accessible via the My
namespace, as detailed in Chapter 19. Listing 3.7 shows the definition of this property.
Friend ReadOnly Property Settings() As
Global.MyFirst2015Program.My.MySettings
Get
Return Global.MyFirst2015Program.My.MySettings.Default
End Get
End Property
The Settings
property represents the active instance of the Settings
object that you can use in your applications. How the active instance is defined is beyond the scope of this chapter, but now you know that the Settings tab in My Project also has a counterpart in two support files. Just for your convenience, the following line of code shows how you can access settings and how you set them before:
Dim currentValue As Boolean = My.Settings.StartState
The value stored in the StartState
setting will be assigned to a variable named currentValue
. Examining the My
namespace in Chapter 19 can clarify how you use settings and resources, as well as many other interesting features.
3.143.4.181