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
term |
description |
CategoryAttribute |
Categories |
DescriptionAttribute |
Label |
Property |
The public read and writeable property to visualize. |
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.
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.
Control CreateGroup(
string group
);
Request for a new Option Group. E.g. you create a new TabPage
and return it.
void SetError(
IPropertyConverterControl control,
string message
);
Validation messages output, e.g. you can call ErrorProvider.SetError((Control) control, message)
.
IPropertyConverterControl
Interface
object Value {get; set;}
You can visualize any data type with 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:
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:
string PropertyNameValidating(PropertyType newValue)
if you return String.Empty
, the validation is ok. If you
return a validation error message, the validation is cancelled.
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. 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.