Navigation:  I Want To... > Miscellaneous >

Save a user setting

Previous pageReturn to chapter overviewNext page

Use Case

You want to save a user specific setting in Tools > Options and restore it the next time the application starts up.

Relevant Architecture

None

Relevant Types

ConfigurationPageProviderExtensionPoint
IConfigurationPageProvider
ConfigurationApplicationComponent
StandardSettingsProvider
System.Configuration.SettingsProviderAttribute
System.Configuration.ApplicationSettingsBase

Sample Code

using System.Collections.Generic;

using System.Configuration;

using System.Diagnostics;

using ClearCanvas.Common;

using ClearCanvas.Common.Configuration;

using ClearCanvas.Desktop;

using ClearCanvas.Desktop.Configuration;

 

namespace MyPlugin.TextEditor

{

   [SettingsGroupDescription("Stores settings for the text editor.")]

   [SettingsProvider(typeof(StandardSettingsProvider))]

   internal sealed partial class TextEditorSettings

   {

      private static TextEditorSettings defaultInstance =

         ((TextEditorSettings)(global::System.Configuration.ApplicationSettingsBase.Synchronized(new TextEditorSettings())));

 

      public static TextEditorSettings Default {

         get

         {

            return defaultInstance;

         }

      }

 

      [global::System.Configuration.DefaultSettingValueAttribute("False")]

      public bool WordWrap

      {

         get

         {

            return ((bool)(this["WordWrap"]));

         }

         set

         { 

            this["WordWrap"] = value;

         }

      }

      public TextEditorSettings()

      {

         ApplicationSettingsRegistry.Instance.RegisterInstance(this);

      }

   }

 

   [ExtensionPoint]

   public class TextEditorConfigComponentViewExtensionPoint : ExtensionPoint<IApplicationComponentView> { }

 

   [AssociateView(typeof(TextEditorConfigComponentViewExtensionPoint))]

   public class TextEditorConfigComponent : ConfigurationApplicationComponent

   {

      private bool _wordWrap;

 

      public bool WordWrap

      {

         get { return _wordWrap; }

         set {

            if (_wordWrap != value)

            {

               _wordWrap = value;

 

               base.NotifyPropertyChanged("WordWrap");

               base.Modified = true;

            }

         }

      }

 

      public override void Start()

      {

         _wordWrap = TextEditorSettings.Default.WordWrap;

 

         base.Start();

      }

 

      public override void Save()

      {

         TextEditorSettings.Default.WordWrap = _wordWrap;

         TextEditorSettings.Default.Save();

      }

   }

 

   [ExtensionOf(typeof(ConfigurationPageProviderExtensionPoint))]

   public class ConfigPageProvider : IConfigurationPageProvider

   {

      public IEnumerable<IConfigurationPage> GetPages()

      {

         List<IConfigurationPage> list = new List<IConfigurationPage>();

         list.Add(new ConfigurationPage<TextEditorConfigComponent>("TextEditor"));

         return list.AsReadOnly();

      }

   }

}

Remarks

The sample code above is a compilation of snippets in the section Designing for Configurability.

To add a custom page to the Tools > Options dialog and save its settings, you must do four things:

1.Derive a class from the .NET class ApplicationSettingsBase in the manner shown above.  Be sure to include the static methods as well as the call to ApplicationSettingsRegistry.Instance.RegisterInstance() in the constructor.  Alternatively, use the ClearCanvas Settings Item Template to generate all this code for you; the template should be available to you through Visual Studio once you've installed the CC SDK.  Using the template also allows you to use the designer to specify the settings you want.
2.Create the pages you want by creating the appropriate ApplicationComponents.  These components should provide public properties that allow the view to change the settings, and the properties should internally set the Modified property so that the configuration dialog knows something has changed.
3.Implement the IConfigurationPageProvider interface, returning the pages you want added via the GetPages() method.  Make the class an extension of the ConfigurationPageProviderExtensionPoint.
4.Make the view and controls for the ApplicationComponents you created in step 2.  If you implemented properties that call the NotifyPropertyChanged method, you can use Data Binding to bind basic Windows controls to the exposed properties.

Examples in Code Base

ClearCanvas.ImageViewer.Configuration.MonitorConfigurationSettings (related classes in same namespace)
ClearCanvas.ImageViewer.Explorer.Dicom.DicomExplorerConfigurationSettings (related classes in same namespace)