|
|||||||||||||||||||||
|
|||||||||||||||||||||
|
Announcements
Chapters
Services
Feature Zones
|
IntroductionI wanted to create an easy way to setup a Settings (options) dialog box similar to the Options box of Visual Studio when you go to Tools > Options. There were a few requirements I wanted to include like, reusability, handling many data types, categories, sorting and spaces in the names. For complete explanation of the code described in this article, please download the source code and read the comments. Each function/subroutine is fully commented, as well as has the new XML comments (new for VB) for each one (no XML comments are on control handlers). 12/12/2005 - I'd like to thank Peter Spiegler for sending me the code to support system-defined enumerations. The settingsThe great part about this dialog box is that it reads all the settings from the There are a few rules that are imposed on your settings in order for them to show up in the dialog box:
The underscore is required so that it can separate the actual name of your setting from the category you wish to put it in. See the screenshot below for some examples:
You'll notice several settings in the screenshot have double underscores in their names. This was to allow spaces in either the category name or in the setting name itself. You'll also notice that all the names in the screenshot have a number after them. This is for the settings sort index within its own category. The setting needs to have a scope of user, not application. When a setting has a scope of application - it is readonly at runtime, and since we want to change these settings at runtime it must have a scope of user. There are several data types that are supported by the IDE's Settings Designer, but it is a little more difficult to support data types such as a TimeSpan, or a GUID. While a
Using this formUsing the form is like using any other form. Declare an instance of it and use the Dim f As New frmOptions
f.Style = frmOptions.OptionsStyle.FireFox2
f.ShowDialog(Me)
The styles (12/12/2005)There are four different styles that you can use. Dim f As New frmOptions
'f.OptionStyle = frmOptions.OptionsStyle.FireFox1
f.Style = frmOptions.OptionsStyle.FireFox2
'f.OptionStyle = frmOptions.OptionsStyle.TabPages
'f.OptionStyle = frmOptions.OptionsStyle.TreeView
f.ImageAdd("Database", My.Resources.database)
f.ImageAdd("Misc", My.Resources.ClockFace)
f.ImageAdd("Fonts", My.Resources.fonts)
f.ImageAdd("Colors", My.Resources.circles)
f.ImageAdd("COM", My.Resources.rotary_phone)
f.ShowDialog(Me)
The formThe form is relatively simple. I have a Loading the settingsIn order to be able to change the settings, but not apply them (hence the Apply and OK buttons) we need to have a storage spot for all our setting information. So, I created a class with a collection to store that information for us - In the 'Load the SaveOnExit value
chkSaveOnExit.Checked = My.Application.SaveMySettingsOnExit
'Setup the Setting property object for grabing all the settings
Dim sp As System.Configuration.SettingsProperty = Nothing
'Cycle through each setting and add it if appropriate.
For Each sp In My.Settings.Properties
'If the name doesn't have an underscore - we can't
'assign a category so we can't change it.
'If the setting is ApplicationScoped - we aren't
'able to hange it at runtime.
'Check also if there is support on this form for
'the System.Type the setting is.
If sp.Name.IndexOf("_") <> -1 AndAlso _
IsUserScope(sp) AndAlso IsAllowedType(sp) Then
'Passed the tests create a new SettingInfo object
Dim newSetting As New SettingInfo
'Load the settings data into the object
newSetting.LoadData(sp.Name)
'Add the object to the collection
Settings.Add(newSetting)
End If
Next
'Sort the settings by category - makes the
'TreeView look nice
quickSort(Settings)
'Load the settings into the TreeView
LoadTreeView()
To load the categories into the Displaying the edit controlsAfter a category is selected from the 'Get all the settings that match this category
Dim sets() As SettingInfo = _
Settings.GetByCategory(tvCategories.SelectedNode.FullPath)
From here, I cycle through all the settings returned to me, and determine the appropriate control to use for editing, adding handlers to each control's Applying the settingsApplying the settings is very easy - just loop through each setting in the collection and update the value stored in the 'Cycle through each setting that we could edit and
'update the real setting contained in the My namespace
For Each si As SettingInfo In Settings
My.Settings.Item(si.TrueName) = si.Value
Next
'Update the SaveOnExit value
My.Application.SaveMySettingsOnExit = chkSaveOnExit.Checked
Hitting either the OK or Apply button will trigger the code given above. Apply will not close the dialog box. OK sets the DialogResult to OK and is the Accept button of the form. Cancel is the Cancel button of the form and will set DialogResult to CANCEL. During runtime, if you've unchecked the save settings on the exit checkbox, the settings will apply correctly when you change them, but after you restart the application they will again be at their defaults. Leaving it checked will ensure that the settings you've changed are carried on to the next run of the program. ConclusionI really hate writing conclusions. I'll let you, the readers, come to your own conclusion about this code. I hope you find this helpful and easy to use. Feel free to send any comments/suggestions/bug reports to codeproject@stdominion.net or just leave a message below. | ||||||||||||||||||||