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

Application settings based on public class properties

Rate me:
Please Sign up or sign in to vote.
3.86/5 (11 votes)
11 Oct 2003CPOL3 min read 72.3K   2.5K   58   3
Application settings based on public class properties, dynamically filled into a UI

Sample Image - PropertyConverter.gif

Introduction

How do you manage application settings? How do you display them nicely to the user? If you would like to write reusable code to manage and visualize application settings basically you have to:

  • Reflect your settings objects dynamically
  • Implement controls to visualize different data types.
  • Dynamically create the controls and arrange them
  • Bind your dynamic controls to the settings object instance.
  • Handle validating and resulting validating errors

The code use the following UI Controls delivered in release mode assemblies

Key features

  • Works a bit like the well know PropertyGrid form Visual Studio.
  • You can implement the interfaces to use your current UI.
    The demo application provides a option dialog like the one from Visual Studio.
  • Every class type that provide public properties can be visualized
  • FlowManager arranges the controls
  • DataBinding between property and control is set automatically
  • You can validate the data and set validating error message.
  • Localization (Culture-specific resources) supported

PropertyConverter

PropertyConverter reflects the public read and writeable properties for the given settings object. Every setting item is based on

termdescription
CategoryAttributeCategories
DescriptionAttributeLabel
PropertyThe public read and writeable property to visualize.
C#
string PropertyNameValidating(PropertyType 
newValue)
Validating new value, return String.Empty for OK, otherwise a validating error message to cancel the validation process.

PropertyConverter can fill IPropertyConverterGui with IPropertyConverterControls based on your settings class. Each property can have its own validating method. You can send your validation messages to a error provider via IPropertyConverterGui. FlowManager takes care of the layout. The Categories and Descriptions are localized if there is an embedded resource file (resx) for the setting class.

IPropertyConverterGui Interface

We first start by implement the IPropertyConverterGui interface. You could use a TabControl on a form or the PropertyTree that i have used in my demo application.
C#
IPropertyConverterControl CreateControl(
PropertyInfo propertyInfo
); 

Request to create a IPropertyConverterControl (Control) for the given PropertyInfo type, e.g. for type string you would create a textbox control which implements the IPropertyConverterControl and return it. There are a few controls provided in Raccoom.Windows.Forms.PropertyConverterControls namespace.

C#
Control CreateGroup(
string group
);

Request for a new Option Group. E.g. you create a new

C#
TabPage 
and return it.

C#
void SetError(
IPropertyConverterControl control,
string message
);

Validation messages output, e.g. you can call ErrorProvider.SetError((Control) control, message).

IPropertyConverterControl Interface

C#
object Value {get; set;}

You can visualize any data type with

C#
PropertyConverter 
if you implement this interface to your controls. Because you're responsible to map the property types on your own, you can create any type of control you like to visualize your data types. The only thing you must do is implement this light weight interface. It is used for data binding.

Basically you implement this interface like this:

C#
/// IPropertyConverterControl implementation for TextBox.
public class TextBoxOption : TextBox, IPropertyConverterControl
{
    public TextBoxOption() {}

    #region IPropertyConverterControl Members

    public object Value
    {
        get
        {
            return base.Text;
        }
        set
        {
            if(value is string) base.Text = (string) value;
        }
    }

    #endregion
}

How to validate your data

For each public property you can write your own validate method. It must have the following signature:

C#
string PropertyNameValidating(PropertyType newValue)

if you return String.Empty, the validation is ok. If you return a validation error message, the validation is cancelled.

C#
public string UserNameValidating(string newValue)
{
    if(newValue=="OK") return String.Empty;
    return "Value is not OK, please change!";
}

How to localize your Categories and Descriptions

You can provide resource files (resx) for each culture. The resource key has to be the Category / Description name.

C#
PropertyConverter 
automatically loads the resource file and localize the strings.

Demo Application

The setting class in the demo application is deserialized at startup, you can change settings and if you're done you can accept (serialize) or reject your changes. If you enter some invalid data the error provider informs you about the problem.

Conclusions

  • FlowManager arrange controls only vertical and does not care about Minimum/Maximum size of clientrectangle.

Links

History

  • 16.08.2003 experimental release

Have fun with it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Software Developer (Senior)
Switzerland Switzerland
My interest is in the future because I am going to spend the rest of my life there. (Charles Kettering)

Biography

  • 1996 - 1998 PC Board PPL, HTML, DHTML, Javascript and ASP
  • 1999 - 2001 coding Centura against Sql Database (SqlBase,MSSQL,Oracle)
  • 2002 - 2004 C# Windows Forms
  • 2005 - 2006 C# ASP.NET, Windows Forms
  • 2006 - 2009 C#, WCF, WF, WPF
  • 2010 - 2012 C#, Dynamics CRM, Sharepoint, Silverlight
  • 2013 - 2013 C#, WCF DS (OData), WF, WPF
  • 2014 - 2016 C#, Azure PaaS, Identity, OWIN, OData, Web Api
  • 2017 - now C#, aspnet.core, IdentityServer4, TypeScript & Angular @ Azure IaaS or PaaS

Interests

  • family & friends
  • chilaxing ,)
  • coding

Comments and Discussions

 
GeneralNice... Pin
Gilalion2-Jul-04 2:42
Gilalion2-Jul-04 2:42 
GeneralAnimated gif = cool Pin
dog_spawn12-Oct-03 2:24
dog_spawn12-Oct-03 2:24 
GeneralRe: Animated gif = cool Pin
Chris Richner12-Oct-03 3:38
Chris Richner12-Oct-03 3:38 

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.