Click here to Skip to main content
12,634,651 members (25,520 online)
Click here to Skip to main content
Add your own
alternative version

Stats

65.5K views
2K downloads
100 bookmarked
Posted

OptionsForm and OptionsPanel, two classes to easily manage application options

, 25 Oct 2006 CPOL
Rate this:
Please Sign up or sign in to vote.
Two classes that let you synchronize GUI and Settings (or any other binding) with automatic saving/cancelling.

OptionsLib image example

OptionsLib image example 2

Introduction

.NET offers a powerful tool to manage program options, and that is the data-binding and settings classes, which can serialize and store any type of data. This is great, but it would be better if these tools worked as they should. In fact, when you bind data (and specifically settings in this case) with your control, there are always some problems on handling the binding. These problems often are caused by the designer, which doesn't allow you to access some option in binding (for example, the *direction* of the binding). The only solution is editing the code, for each control you bind a setting to...but that's annoying to do in each application that uses some type of settings. For this reason, I thought to do a resuable set of controls to make my life easier with settings (and bindings in general).

Using the code

As always, I wrote my code to be the as usable as I could. To be extremely clear, I'll show a step-by-step how-to-do:

  1. Include the source files in your project or add a reference to OptionsLib.dll.
  2. Right click on your project and select Add -> New Item; in the dialog that pops up, select Inherited Form, put a name. and select OK. In the next screen, select as the base form OptionsForm, and the form is added. Go to the default constructor of the form and set a : base (Settings) inheritance for the default constructor, where settings is an instance of a SettingBase derived class (for instance, Properties.Settings.Default, the default Settings object generated by Visual Studio). Then you could set some options.
  3. Right click again on your project and select Add -> New Item; in the dialog that pops up, select Inherited UserControl, put a name (for instance SomethingPanel) and select OK. In the next screen, select as the base class OptionsPanel, and the UserControl is added.
  4. Go to the just added OptionsPanel control (for instance SomethingPanel) properties, and scroll down to the Options Form category. The CategoryPath property indicates the TreeView path at which the OptionsPanel is placed. You can use this syntax "Options\Path{"Display Text of the TreeNode"}\SomethingPanel" without the external quotes; each subpath (Options,Path) defines a TreeNode parent name (which doesn't correspond to an OptionsPanel); instead the text between {" and "} is the label of the TreeNode, that is, the text which appears in the TreeView list (if you don't specify a label, the name will be used). The label for the SomethingPanel is contained in the DisplayName property of the panel itself, and if you leave it blank, the panel label will remain blank. In addition, you can specify the AccessibleDescription property and it will appear in the little category description box at the bottom-left of the OptionsForm.
  5. Now return back to the OptionsForm inherited form (added at point 2) the constructor, and inside of it call
    Panels.Add(new SomethingPanel());
    
  6. Now, you can add all controls you like to your SomethingPanel through the designer, setting application bindings (or data ones, even if I didn't try this last, it should work) to the properties of the controls. In addition, you can set the AccessibleDescription property of the controls added into the panel, and the AccessibleDescription text will appear on the description box in the OptionsForm when the user enters the control with the mouse. You don't have to mind for the bindings because the control will do, and it will automatically disable/enable the Apply button. If the settings change, it will automatically save/cancel the changed settings if the user clicks Apply or OK or Cancel.

OptionsForm Properties

[Category("Options Form")]
[Description("Images used in the category tree")]
public ImageList CategoryImages

[Category("Options Form")]
[Description("Indicates the width of the Category Tree Box")]
public int CategoryTreeWidth

[Category("Options Form")]
[Description("This is the Text for the category header box")]
public String CategoryHeaderText

[Category("Options Form")]
[Description("This is the Text for the OK button")]
public String OkButtonText

[Category("Options Form")]
[Description("This is the Text for the Apply button")]

[Category("Options Form")]
[Description("This is the Text for the Cancel button")]
public String CancelButtonText

[Category("Options Form")]
[Description("This is the Text for the 
    Application Must Restart label box")]
public String AppRestartText

[Category("Options Form Colors")]
[Description("Indicates the background color for each box")]
public Color BoxBackColor

[Category("Options Form Colors")]
[Description("Indicates the foreground color 
    for the options panel path box")]
public Color OptionsPathForeColor

[Category("Options Form Colors")]
[Description("Indicates the foreground color for 
       the Application Must Restart label box")]
public Color AppRestartForeColor

[Category("Options Form Descriptions")]
[Description("This is the default Text for the description 
       box when no description is available")]
public String OptionsNoDescription

[Category("Options Form Descriptions")]
[Description("This is the description for the 
       Category Header box when the mouse hover it")]
public String CategoryHeaderDescription

[Category("Options Form Descriptions")]
[Description("This is the description for the Options 
       Panel Path box when the mouse hover it")]
public String OptionsPanelPathDescription

[Category("Options Form Descriptions")]
[Description("This is the description for the Category 
              Tree box when the mouse hover it")]
public String CategoryTreeDescription

[Category("Options Form Descriptions")]
[Description("This is the description for the Category 
              Description box when the mouse hover it")]
public String CategoryDescrDescription

[Category("Options Form Descriptions")]
[Description("This is the description for the Option 
              Description box when the mouse hover it")]
public String OptionDescrDescription

[Category("Options Form Descriptions")]
[Description("This is the description for the Application 
              Must Restart box when the mouse hover it")]
public String ApplicationRestartDescription

[Category("Options Form Descriptions")]
[Description("This is the description for the 
              OK Form Button when the mouse hover it")]
public String OkButtonDescription

[Category("Options Form Descriptions")]
[Description("This is the description for the 
              Apply Form Button when the mouse hover it")]
public String ApplyButtonDescription

[Category("Options Form Descriptions")]
[Description("This is the description for the Cancel 
              Form Button when the mouse hover it")]
public String CancelButtonDescription

[Category("Options Form Flags")]
[Description("Indicates if the Application Must Restart box must appear")]
public bool ApplicationMustRestart

[Category("Options Form Flags")]
[Description("Indicates if the Category Header box must be visible")]
public bool ShowCategoryHeader

[Category("Options Form Flags")]
[Description("Indicates if the Category Description box must be visible")]
public bool ShowCategoryDescription

[Category("Options Form Flags")]
[Description("Indicates if the Options Description box must be visible")]
public bool ShowOptionsDescription

[Category("Options Form Flags")]
[Description("Indicates if first Panel must be selected 
              when selection is on a parent Category.")]
public bool SelectFirstPanel

[Category("Options Form Flags")]
[Description("Indicates if the Options Panel Path box must be visible")]
public bool ShowOptionsPanelPath

[Category("Options Form Flags")]
[Description("Indicates if the Splitter capacity 
              is enabled for the Options Form")]
public bool EnableFormSplitter

[Category("Options Form Flags")]
[Description("Indicates if the Options form will 
              automatically save the Application Settings")]
public bool AutomaticSaveSettings

[Category("Options Form Flags")]
[Description("Indicates if the Apply button must be 
              always enabled (and not only when an option change)")]
public bool ApplyAlwaysEnabled

[Category("Options Form Flags")]
[Description("Indicates if the Apply button must be always 
              enabled (and not only when an option change)")]
public bool SaveAndCloseOnReturn

OptionsForm Events

[Category("Action")]
[Description("This event occurs when the form options 
      must be saved (click on Apply or OK form buttons)")]
public event EventHandler OptionsSaving;

[Category("Action")]
[Description("This event occurs when the form options have been saved")]
public event EventHandler OptionsSaved;

[Category("Action")]
[Description("This event occurs when the form items 
        must be reset (click on Cancel form button)")]
public event EventHandler ResetForm;

These events are automatically handled in OptionsPanels so you don't need to add event handlers for those events, only override the respective methods.

Conclusion

So, that's all, I think. I hope you'll find it useful, bye!

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Share

About the Author

Elia Sarti
Synved Ltd.
Ireland Ireland

You may also be interested in...

Pro
Pro

Comments and Discussions

 
Questionwpf version? Pin
filmee2412-Nov-14 3:18
memberfilmee2412-Nov-14 3:18 
QuestionMultiple dialogs and resetting to default values Pin
TonBlokhuizen17-Sep-13 2:21
memberTonBlokhuizen17-Sep-13 2:21 
QuestionCould you tell me what license your code is released under? Pin
Noobsa4418-Aug-09 18:29
memberNoobsa4418-Aug-09 18:29 
QuestionDataGridView control & Binding issue Pin
mpgjunky29-Sep-08 6:28
membermpgjunky29-Sep-08 6:28 
QuestionExpanded Categories Pin
mpgjunky8-Sep-08 23:15
membermpgjunky8-Sep-08 23:15 
AnswerRe: Expanded Categories Pin
Gladstone9-Sep-08 3:58
memberGladstone9-Sep-08 3:58 
AnswerRe: Expanded Categories Pin
mpgjunky9-Sep-08 4:04
membermpgjunky9-Sep-08 4:04 
GeneralComboBox [modified] Pin
Khayralla9-Apr-08 6:20
memberKhayralla9-Apr-08 6:20 
GeneralRe: ComboBox Pin
Gladstone9-Apr-08 19:03
memberGladstone9-Apr-08 19:03 
QuestionRe: ComboBox Pin
Khayralla10-Apr-08 4:19
memberKhayralla10-Apr-08 4:19 
GeneralRe: ComboBox Pin
Khayralla16-Apr-08 4:14
memberKhayralla16-Apr-08 4:14 
GeneralGlobalization issue Pin
StefanH1-Apr-08 5:02
memberStefanH1-Apr-08 5:02 
QuestionDesigner error after switching localizable to true Pin
StefanH1-Apr-08 1:41
memberStefanH1-Apr-08 1:41 
AnswerRe: Designer error after switching localizable to true Pin
StefanH1-Apr-08 2:47
memberStefanH1-Apr-08 2:47 
QuestionMultiple settings files Pin
jauwaad17-Aug-07 16:56
memberjauwaad17-Aug-07 16:56 
AnswerRe: Multiple settings files Pin
Gladstone18-Aug-07 2:27
memberGladstone18-Aug-07 2:27 
GeneralRe: Multiple settings files Pin
jauwaad20-Aug-07 9:09
memberjauwaad20-Aug-07 9:09 
GeneralDesigner Error Pin
kin3tik17-May-07 7:49
memberkin3tik17-May-07 7:49 
GeneralRe: Designer Error Pin
Gladstone18-May-07 14:26
memberGladstone18-May-07 14:26 
GeneralOptionsForm_FormClosing always returns "Cancel" Pin
redjoy26-Feb-07 6:56
memberredjoy26-Feb-07 6:56 
QuestionWhat does method OptionsSaving supposed to do? [modified] Pin
redjoy19-Feb-07 5:30
memberredjoy19-Feb-07 5:30 
AnswerRe: What does method OptionsSaving supposed to do? Pin
Gladstone19-Feb-07 12:30
memberGladstone19-Feb-07 12:30 
QuestionTab order Pin
P.Burman6-Feb-07 12:59
memberP.Burman6-Feb-07 12:59 
AnswerRe: Tab order Pin
Gladstone6-Feb-07 20:55
memberGladstone6-Feb-07 20:55 
GeneralRe: Tab order Pin
P.Burman8-Feb-07 1:13
memberP.Burman8-Feb-07 1:13 
GeneralThis is great Pin
redjoy25-Oct-06 16:02
memberredjoy25-Oct-06 16:02 
GeneralRe: This is great Pin
Gladstone25-Oct-06 17:52
memberGladstone25-Oct-06 17:52 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Terms of Use | Mobile
Web01 | 2.8.161208.2 | Last Updated 25 Oct 2006
Article Copyright 2006 by Elia Sarti
Everything else Copyright © CodeProject, 1999-2016
Layout: fixed | fluid