Handling Settings Events
Most of the time, you manipulate settings either by assigning values to them or retrieving values from them, and you need not worry about how they work. However, there are four events related to the settings that you can service if you want to look out for certain events. These four events are:
- SettingChanging: Raised before a setting's value is changed.
- PropertyChanged: Raised after a setting's value is changed.
- SettingsLoaded: Raised after the setting values are loaded.
- SettingsSaving: Raised before the setting values are saved.
To service these four events, click on the View Code button in the Settings tab. The Settings.vb
file will now appear.
Listing 1 shows the signature of the four event handlers.
|Figure 7. The Result: Tracing the events fired by accessing the application settings.|
To see how the events are fired, add some code to each event handler so that you can trace their flow. Also, check that the user does not set the background color to Black. If s/he does, the BackColor application setting will not be saved (Listing 2)
Press F5 to debug the application. Move the form around and change the background color. You will observe something like that shown in Figure 7 (you can view this through the View | Output window).
Implementing Application Settings for Custom Controls
So far, these discussions have centered on the assumption that you are using settings in standalone applications. What about custom controls? How do you use settings in a custom control?
This section will demonstrate how you can enable your custom controls to make use of settings by following a few simple steps. For simplicity, add a User Control (Add | New Item…) to the project and use the default name of UserControl1.vb.
Populate the user control with the controls shown in (Figure 8).
|Figure 8. The Controls: Populating the user control with these controls.|
This user control allows users to select a range of dates. The dates selected will then be persisted to some application settings.
Switch to UserControl1's code-behind and import the following namespace:
Declare a class named CtrlAppSettings
(which inherits from the ApplicationSettingsBase class
Public NotInheritable Class CtrlAppSettings
Public Property StartDate() As Date
Set(ByVal value As Date)
Me("StartDate") = value
Public Property EndDate() As Date
Set(ByVal value As Date)
Me("EndDate") = value
class is a base class for creating wrapper classes to implement the application settings feature in Window Forms applications. Within the CtrlAppSettings
class, there are two properties: StartDate
. These two properties act as application settings to preserve the range of dates selected in the MonthCalendar
control. These two settings are user-scoped, as evident in the <UserScopedSetting()>
To make use of the application settings defined in the CtrlAppSettings class, declare an instance of the CtrlAppSettings class within the UserControl1 class (Listing 3).
This basically sets the MonthCalendar control to display the dates saved in the application settings.
|Figure 9. Finito!: Locate the user control in Toolbox.|
When the dates in the control is changed, you have to persist the new dates into the application settings. This is achieved in the DateChanged event of the user control (Listing 4).
That's it! Build the project and you should now observe that the UserControl1 is in the Toolbox (see Figure 9).
Drag and drop a copy of the UserControl1 control onto the form. Press F5 to debug the application. Select a range of dates in the user control, shut down the application, and press F5 to debug the application again. You will notice that the dates selected are preserved automatically; proving that the application settings feature is working.