Click here to Skip to main content
15,881,882 members
Articles / Desktop Programming / Windows Forms
Article

A Simple Configuration Form for Applications

Rate me:
Please Sign up or sign in to vote.
3.18/5 (7 votes)
13 Sep 20052 min read 36.8K   1.1K   39   2
A simple configuration form for applications.

Configuration Control Image

Introduction

The application I developed as part of a big project needed a lot of user parameters. Firstly, I added a couple of forms and every one of them held its parameter logic, but then I decided to merge these forms into one form like the Preferences of Eclipse or Options of the Visual Studio.

Unfortunately, I didn't find anything on the web, so I was compelled to write some basic control by myself.

Let's start

The concept is to add a TreeNode to the TreeView control and relate each tree node with some custom control. Every time the user selects the tree node the related control is activated.

Step 1

Create an instance of the ConfigurationForm class.

C#
ConfigurationForm m_configurationForm = new ConfigurationForm();

Step 2

Create your own control which inherits my ConfigurationBaseControl, override and implement two abstract methods (Apply and Restore).

C#
public class NewControl : ConfigurationBaseControl
{
    public NewControl(string configName): base(configName)
    {

    }

    //override the Restore method and add functionality
    public override void Restore()
    {

    }

    //override the Apply method and add functionality
    public override void Apply()
    {
    }
}

Step 3

Create ConfigurationTreeNode and provide the custom control as parameter to the constructor.

C#
ConfigurationTreeNode config = new ConfigurationTreeNode("NewControl", 
                       new NewControl("New Control Configuration"));

The first parameter in the constructor is the name of the TreeNode item in the TreeView form.

Step 4

Add the configuration tree node to the form so it will appear in the tree view control on the left.

C#
m_configurationForm.AddConfigItem(config);

Some explanations

Usually the business logic configuration classes in the application are static and singleton so each custom configuration control you implement will use some static configuration class you provide, that's why it is possible to implement a generic configuration form.

ConfigurationForm.AddConfigItem(TreeNode) method adds the configuration tree node to the root of the tree. If you want to add two or more levels to the configuration tree node, then add it to the root configuration tree node and then add it to the form as you would do with a regular TreeView.

You can add a basic TreeNode to the configuration form but it won't be related to any custom control. You can see an example of this in the demo project.

You can remove the configuration node using the method ConfigurationForm.RemoveConfigItem( name of the tree node item ).

I've implemented event handlers for four buttons (OK, Cancel, Restore, Apply).

C#
private void ClickHandler(object sender, System.EventArgs e)
{
    //ok button clicked
    if (sender == m_okBtn)
    {
        this.TreeWalker(m_optionsTV.Nodes,Action.Apply);
    }
    //cancel button is clicked
    else if (sender == m_cancelBtn)
    {
        this.Hide();
    }
    //restore button is clicked
    else if (sender == m_restoreBtn)
    {
        this.TreeWalker(m_optionsTV.Nodes,Action.Restore);
    }
    //apply button is clicked
    else if (sender == m_applyBtn)
    {
        this.TreeWalker(m_optionsTV.Nodes,Action.Apply);
    }
}

But it is possible to add other event handlers to the buttons. Right now the default behaviour of the OK and Apply buttons is the same. When they are clicked, the TreeWalker method iterates through all custom configuration controls and call their Apply method.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


Written By
Engineer
Germany Germany
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
GeneralGood idea ! Pin
Stoyan K Petrov14-Sep-05 1:20
Stoyan K Petrov14-Sep-05 1:20 
GeneralRe: Good idea ! Pin
Kisilevich Slava14-Sep-05 2:12
Kisilevich Slava14-Sep-05 2:12 

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.