Compartir a través de


How to: Create Property Grids for User Settings in Visual Basic

You can create a property grid for user settings by populating a PropertyGrid control with the user setting properties of the My.Settings object.

Note

In order for this example to work, your application must have its user settings configured. For more information, see Managing Application Settings.

The My.Settings object exposes each setting as a property. The property name is the same as the setting name, and the property type is the same as the setting type. The setting's Scope determines if the property is read-only; the property for an Application-scope setting is read-only, while the property for a User-scope setting is read-write. For more information, see My.Settings Object.

Note

You cannot change or save the values of application-scope settings at run time. Application-scope settings can be changed only when creating the application (through the Project Designer) or by editing the application's configuration file. For more information, see Managing Application Settings.

This example uses a PropertyGrid control to access the user-setting properties of the My.Settings object. By default, the PropertyGrid shows all the properties of the My.Settings object. However, the user-setting properties have the UserScopedSettingAttribute attribute. This example sets the BrowsableAttributes property of the PropertyGrid to UserScopedSettingAttribute to display only the user-setting properties.

To add a user setting property grid

  1. Add the PropertyGrid control from the Toolbox to the design surface for your application, assumed here to be Form1.

    The default name of the property-grid control is PropertyGrid1.

  2. Double-click the design surface for Form1 to open the code for the form-load event handler.

  3. Set the My.Settings object as the selected object for the property grid.

    PropertyGrid1.SelectedObject = My.Settings
    
  4. Configure the property grid to show only the user settings.

    ' Attribute for the user-scope settings. 
    Dim userAttr As New System.Configuration.UserScopedSettingAttribute
    Dim attrs As New System.ComponentModel.AttributeCollection(userAttr)
    PropertyGrid1.BrowsableAttributes = attrs
    

    Note

    To show only the application-scope settings, use the ApplicationScopedSettingAttribute attribute instead of UserScopedSettingAttribute.

Robust Programming

The application saves the user settings when the application shuts down. To save the settings immediately, call the My.Settings.Save method. For more information, see How to: Persist User Settings in Visual Basic.

See Also

Tasks

How to: Read Application Settings in Visual Basic

How to: Change User Settings in Visual Basic

How to: Persist User Settings in Visual Basic

Reference

My.Settings Object

Concepts

Managing Application Settings