You can create a property grid for user settings by populating a PropertyGrid control with the user setting properties of the
In order for this example to work, your application must have its user settings configured. For more information, see Managing Application Settings (.NET).
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.
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 (.NET).
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
Add the PropertyGrid control from the Toolbox to the design surface for your application, assumed here to be
The default name of the property-grid control is
Double-click the design surface for
Form1to open the code for the form-load event handler.
My.Settingsobject as the selected object for the property grid.
PropertyGrid1.SelectedObject = My.Settings
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
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.