Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles
(untagged)

A Simple Configuration Form for Applications

0.00/5 (No votes)
13 Sep 2005 1  
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.

ConfigurationForm m_configurationForm = new ConfigurationForm();

Step 2

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

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.

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.

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).

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