RSS Feed
Download our iPhone app
Browse DevX
Sign up for e-mail newsletters from DevX


Personalization with Application Settings in Visual Basic : Page 2

Application Settings is a new feature in Windows Forms 2.0 that allows you to maintain both user- and application-specific data (known as settings). Find out how to use it to improve your application's usability with personalization--this time in Visual Basic.

Binding Property Grids to User Settings
Instead of writing code to programmatically access the values of application settings, you can directly bind properties of a Windows Forms control to the various application settings you have defined in your application. As an example, let's now bind the FormLocation setting to the Location property of Form1.

In the Properties window for Form1, expand the (ApplicationSettings) item and in the (PropertyBinding) item, click the “…” button (see Figure 2).

Figure 2. Binding: The FormLocation setting to the Location property.
Figure 3. Enter a New Name: Creating a new application setting in the properties window.

Locate the Location property and in the drop down list next to it, select FormLocation. Click OK.

You can also create new settings in this step. For example, select the BackColor property and in the drop down list next to it, you will realize that there are no settings that match the data type (System.Drawing.Color) of the BackColor property. You can create a new setting at this stage. Simply click on the (New…) link and enter a name for the new setting (see Figure 3).

Using this method of creating a new setting, you do not have to specifically indicate the data type of the setting—it is chosen for you automatically.

Back at the Properties window, you should now see the properties binding of Form1 (Figure 4).

Figure 4. The Properties Binding: The application settings binding of the various properties.
Figure 5. Adding a Button: Add it to control to Form1.

In Form1, add a new Button control and label it as shown in Figure 5.

In the Click event handler for the Button control, code it as follows to allow users to select a color to use as the background color for the form:

    Private Sub btnChangeBGColor_Click( _
       ByVal sender As System.Object, _
       ByVal e As System.EventArgs) _
       Handles btnChangeBGColor.Click

        Dim cd As New ColorDialog
        Dim result As DialogResult = cd.ShowDialog
        If (result = DialogResult.OK) Then
            '---set background color to the color selected---
            Me.BackColor = cd.Color
        End If
    End Sub
That's it! Press F5 to debug the application. Move the form around the screen and then choose a color for the form. Shut down the application by clicking on the "x" button on the form. When you press F5 to debug the application again, notice that the form is displayed at its last position with the background color set to the color you have chosen previously. Notice that you haven't written any code to save the settings values?

Remember that the application settings are only saved when the application shuts down. To ensure that application settings are saved as and when you want them to, you can do it programmatically:


Changing the User Settings Values Visually During Runtime
Most of the time, you will programmatically manipulate the settings through your code. However, there are times where it is useful to allow your users to modify the values for the settings themselves. For example, you might have a Preferences page that allows users to change the various properties of the application. In this case, you can build your own user interface to allow users to customize the application and then you will manually translate these values into the various application settings.

A better approach is to expose the settings to the user graphically so that users can directly modify their values without you needing to write the extra code. Here is where the PropertyGrid control comes in handy. The PropertyGrid control provides a user interface for browsing and modifying the properties of an object. In this case, we can use it to display the values of our application settings.

In the Toolbox, drag and drop the PropertyGrid control onto Form1.

Double-click on Form1 to switch to its code-behind. In the Form1_Load event, set the SelectedObject property of the PropertyGrid control to My.Settings, like this:

    Private Sub Form1_Load( _
       ByVal sender As System.Object, _
       ByVal e As System.EventArgs) _
       Handles MyBase.Load
        PropertyGrid1.SelectedObject = My.Settings
    End Sub
Press F5 to debug the application. You will now see the settings shown in the PropertyGrid control (see Figure 6). Best of all, you can directly change their values and the changes will be immediately effected.

Figure 6. The Setting Shown: View the values of the application settings through the PropertyGrid control..

By default, the My.Settings object will return all the application settings that you have defined. If you want to display only user-scoped settings, you can do so via the following code snippet:

    Private Sub Form1_Load( _
       ByVal sender As System.Object, _
       ByVal e As System.EventArgs) _
       Handles MyBase.Load

        PropertyGrid1.SelectedObject = My.Settings

        '---display only user-scoped settings---
        Dim userAttr As New _
        Dim attrs As New _
        PropertyGrid1.BrowsableAttributes = attrs
    End Sub

Like-wise, for application-scoped settings:

        '---display only application-scoped settings---
        Dim appAttr As New _
        Dim attrs As New _

Close Icon
Thanks for your registration, follow us on our social networks to keep up-to-date