Click here to Skip to main content
Click here to Skip to main content

Application Configuration Editor using the PropertyGrid

, 1 Mar 2004
Rate this:
Please Sign up or sign in to vote.
An application configuration editor using the PropertyGrid control.
<!-- Article image -->

Sample Image - configeditor.jpg

<!-- Add the rest of your HTML here -->

Introduction

Since I discovered configuration sections and configuration section handlers, I've been using custom configuration sections to hold a variety of runtime required settings. What I've wanted for some time now was an easy way to edit these sections, either for simple application setting changes, or during deployment as a custom installation action. In this article I'll describe the approach and logic to the development of my own configuration editor. Download the source code and run the project to see the application in action.

Background

The first example of a custom configuration handler I came across was in the sample Duamish7 application that ships with the .NET Framework SDK. I confess my first reaction was 'oh my god'. It looked like a lot of code to implement the equivalent of a few registry settings. Looking back I can see why I was initially skeptical about their value. Even without using a custom configuration section handler the .NET Framework Class Library supplies a simple NameValue section reading class in System.Configuration.ConfigurationSettings.AppSettings - for quick and easy reading of any setting in the <appSettings> section. It wasn't until recently that I discovered the true value of a custom configuration handler. By handling the section yourself you are no longer limited to simple key value pair attributes. In fact you can build your own XML structure altogether and do what ever you want with this when the node for your section is passed to the handler. There are a lot of excellent resources out there that describe how to implement your own custom configuration section handler and so that's not the topic here, although I'll include several links to them below. What you will see in this application however is the value of handling a section yourself, in a simple way. I've extended the name value pair keys to include an additional XML attribute for the setting, the 'description' attribute which you can see in action in the screen shot above.

Enter the PropertyGrid control. It turns out that the PropertyGrid is perfectly suited for this application. I wanted a two column grid with the first column readonly, and the second column editable and the whole thing divided nicely into categorized sections. I've seen at least one other attempt at creating a Configuration file editor using the DataGrid and a DataSet however it just didn't seem as elegant a solution as using the PropertyGrid. If you're wondering where the PropertyGrid control comes from you'll be pleased to know you already have it, it's free to use as a .NET control, you just need to add it to the toolbar in Visual Studio.

Dynamic Properties. Having chosen the tools and controls I was faced with a problem - how to effectively de-serialize the configuration sections I was interested in, creating an object that could be used for the PropertyGrid's SelectedObject property. The PropertyGrid is an impressive control. It will automatically display all the properties of a class, along with property values which can be edited. How you create the object and what you do with it when you're finished changing values is the 'interesting' part. There is a LOT of customization options that will determine how values are displayed in the property grid. Again there are links below to the MSDN resources that provide excellent descriptions on how to use the PropertyGrid.

Full Credit. As often happens it was while trolling through the newsgroups in search of a solution to the problem of how I was going to create an object at runtime, I came across a posting that described the world of PropertyDescriptorCollection and PropertyDescriptor classes. Thank you Venu Madhav (venu_madhav_g@yahoo.com) for pointing me in the right direction. CustomClass in this application is adapted from Venu's CustomClass and there are notes in the application that describe the changes.

Using the code

To use the application - open the Visual Studio project and compile the application as a Windows Application - a .Exe file. Then change the project properties and Output Type to Class Library and compile again. This will create a .dll assembly. To use the Configuration Editor in your own Windows application - add a reference to the compiled .dll file and add the following two lines of codes to your application (under a menu called Settings for example). That's all there is to it. The application will attempt to find the .Config file if there is one present and automatically load it.

    private void mnuSettings_Click(object sender, System.EventArgs e)
    {
        DotBits.Configuration.ConfigEditor c = new 
					DotBits.Configuration.ConfigEditor();
        c.ShowDialog(this);
    }
    

If you want to include the .Exe in your Setup Project as a Custom Action during installation simply include the ConfigEditor.Exe in the Setup Project as a required file. Create a Custom Action that points to the .Exe (and remember to change the Custom Actions InstallerClass property to false).

Note: There is a sample App.Config file in the project. You can run the project as soon as you unzip it to see what the application looks like - however you might be tricked into thinking that your changes to the .Config file are not being saved. Remember that Visual Studio will copy the App.Config file into the compiled directory every time the project is run - and so any setting changes you make while running the project will be lost when you run the project again.

Links

Custom Configuration Sections and Handlers

Application Configuration Files Explained (Adventures in .NET)
Declaring Custom Configuration Sections (.NET Framework Developer's Guide)
Custom Element for NameValueSectionHandler and DictionarySectionHandler

The PropertyGrid Control

Getting the Most out of the .Net Framework PropertyGrid Control
Make your Components Really RAD with Visual Studio .Net Property Browser

History

  • First post 22 Feb 2004
  • Updated source code 2 Mar 2004<!------------------------------- That's it! --------------------------->

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

Share

About the Author

Anthony Bouch
Web Developer
Thailand Thailand
I've been working in IT for about 14 years. Started in general support, networking and later specialized in publishing and editorial system networks.
 
Made the switch to full-time software development only about 5 years ago - mainly C#, VB and SQLServer. C# and ASP.Net for two years now and I love it.

Comments and Discussions

 
BugError while reading XML comments PinmemberAndreas Houben29-Nov-13 1:46 
GeneralMy vote of 5 PinmemberRonnieDean7719-Jun-13 6:01 
QuestionWhich license do you want? Pinmemberr.j.pieper27-Sep-12 11:04 
GeneralVS2005 exe.config file Pinmemberm_karahan18-Feb-10 4:00 
GeneralNew Version Pinmembertobster15-Sep-08 9:18 
GeneralRe: New Version Pinmemberkiquenet AE25-Dec-11 23:22 
GeneralEureka Pinmemberizmoto12-Mar-08 5:25 
Just found waht i was looking to build.
Thanks man. Great Stuff. Smile | :)
 
void izmoto(char* szKwazi);

GeneralChild nodes Pinmemberstub31-Aug-07 9:50 
GeneralRe: Child nodes Pinmembersebma29-Jul-10 0:55 
GeneralExcellent Example and References Pinmembergino_d_animal17-May-07 9:27 
GeneralMasking Pinmembertoshi_moshi1-Dec-06 1:37 
GeneralStoopid Pinmembervidapura20-Jan-06 6:35 
Questionstring[] ? PinmemberAlex........29-Oct-04 9:44 
GeneralProblem reading config file PinmemberShmulik2821-May-04 14:42 
GeneralSelect Different Editor PinmemberKevin Hartman4-Mar-04 9:06 
GeneralRe: Select Different Editor Pinmemberthomlemon11-May-04 9:18 
GeneralRe: Select Different Editor PinmemberAnthony Bouch11-May-04 19:22 
GeneralRe: Select Different Editor Pinmemberandang6-Jun-06 20:39 
GeneralRe: Select Different Editor PinmemberDutchMafia28-Feb-08 12:42 
Generalsections problem PinmemberJackDotNet29-Feb-04 2:59 
GeneralRe: sections problem PinmemberAnthony Bouch29-Feb-04 3:19 
GeneralRe: sections problem PinmemberAnthony Bouch29-Feb-04 3:45 
GeneralRe: sections problem PinmemberJackDotNet29-Feb-04 11:29 
GeneralOops! Pinmembernetclectic23-Feb-04 2:25 
GeneralRe: Oops! PinmemberAnthony Bouch23-Feb-04 4:14 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    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 | Mobile
Web04 | 2.8.140827.1 | Last Updated 2 Mar 2004
Article Copyright 2004 by Anthony Bouch
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid