Click here to Skip to main content
15,867,704 members
Articles / Programming Languages / C#
Article

Application Configuration Editor using the PropertyGrid

Rate me:
Please Sign up or sign in to vote.
4.80/5 (35 votes)
1 Mar 20045 min read 191.6K   5.9K   118   26
An application configuration editor using the PropertyGrid control.

Sample Image - configeditor.jpg

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.

C#
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

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

 
GeneralExcellent Example and References Pin
gino_d_animal17-May-07 9:27
gino_d_animal17-May-07 9:27 
GeneralMasking Pin
toshi_moshi1-Dec-06 1:37
toshi_moshi1-Dec-06 1:37 
GeneralStoopid Pin
vidapura20-Jan-06 6:35
vidapura20-Jan-06 6:35 
Questionstring[] ? Pin
Alex........29-Oct-04 9:44
Alex........29-Oct-04 9:44 
GeneralProblem reading config file Pin
Shmulik2821-May-04 14:42
Shmulik2821-May-04 14:42 
GeneralSelect Different Editor Pin
Kevin Hartman4-Mar-04 9:06
Kevin Hartman4-Mar-04 9:06 
GeneralRe: Select Different Editor Pin
thomlemon11-May-04 9:18
thomlemon11-May-04 9:18 
GeneralRe: Select Different Editor Pin
Anthony Bouch11-May-04 19:22
Anthony Bouch11-May-04 19:22 
GeneralRe: Select Different Editor Pin
andang6-Jun-06 20:39
andang6-Jun-06 20:39 
Generalsections problem Pin
JackDotNet29-Feb-04 2:59
JackDotNet29-Feb-04 2:59 
GeneralRe: sections problem Pin
Anthony Bouch29-Feb-04 3:19
Anthony Bouch29-Feb-04 3:19 
GeneralRe: sections problem Pin
Anthony Bouch29-Feb-04 3:45
Anthony Bouch29-Feb-04 3:45 
GeneralRe: sections problem Pin
JackDotNet29-Feb-04 11:29
JackDotNet29-Feb-04 11:29 
GeneralOops! Pin
netclectic23-Feb-04 2:25
netclectic23-Feb-04 2:25 
GeneralRe: Oops! Pin
Anthony Bouch23-Feb-04 4:14
Anthony Bouch23-Feb-04 4:14 
GeneralRe: Oops! Pin
JackDotNet27-Feb-04 5:30
JackDotNet27-Feb-04 5:30 

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.