Click here to Skip to main content
6,305,776 members and growing! (17,602 online)
Email Password   helpLost your password?
Languages » C# » Windows Forms     Intermediate

A Simple Configuration Form for Applications

By Kisilevich Slava

A simple configuration form for applications.
C#, Windows, .NET 1.1, WinForms, VS.NET2003, Dev
Posted:13 Sep 2005
Views:17,651
Bookmarked:31 times
Announcements
Loading...
 
Search    
Advanced Search
printPrint   Broken Article?Report       add Share
  Discuss Discuss   Recommend Article Email
6 votes for this article.
Popularity: 2.33 Rating: 3.00 out of 5
3 votes, 50.0%
1

2
2 votes, 33.3%
3

4
1 vote, 16.7%
5

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

About the Author

Kisilevich Slava


Member

Occupation: Engineer
Location: Germany Germany

Other popular C# articles:

Article Top
You must Sign In to use this message board.
FAQ FAQ 
 
Noise Tolerance  Layout  Per page   
 Msgs 1 to 2 of 2 (Total in Forum: 2) (Refresh)FirstPrevNext
GeneralGood idea ! Pinmemberpetrovstoyan2:20 14 Sep '05  
GeneralRe: Good idea ! PinmemberKisilevich Slava3:12 14 Sep '05  

General General    News News    Question Question    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

PermaLink | Privacy | Terms of Use
Last Updated: 13 Sep 2005
Editor: Smitha Vijayan
Copyright 2005 by Kisilevich Slava
Everything else Copyright © CodeProject, 1999-2009
Web13 | Advertise on the Code Project