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)
{
}
public override void Restore()
{
}
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)
{
if (sender == m_okBtn)
{
this.TreeWalker(m_optionsTV.Nodes,Action.Apply);
}
else if (sender == m_cancelBtn)
{
this.Hide();
}
else if (sender == m_restoreBtn)
{
this.TreeWalker(m_optionsTV.Nodes,Action.Restore);
}
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.