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

An XML- and State Machine-based Design of a WinForms Control

Rate me:
Please Sign up or sign in to vote.
4.98/5 (35 votes)
1 Dec 20024 min read 86.1K   1.3K   49   5
Usage of state machine and comprehensive XML description of control helps its user to considerably simplify control handling code.

Sample Image - XMLStateMachineWFControl.gif

Introduction

The presented WinForms control has three distinctive features:

  • it may contain and operate any element that implements a certain uniform interface,
  • its main output is its current state, and
  • it is completely described and configured with XML.

The control is a UserControl-based container for sensitive elements (below referred to as simply elements). Filled with elements of various types, it can serve as a customizable toolbar, toolbox, or hot spot control for different applications. The control is called Open Group Control (OGC) since it contains and manages extendable (open) group of elements.  Advantage of such a control in comparison with, say, ordinary toolbar is that it does not require writing user's code to handle control's internal elements and their relationship. The OGC provides its user with reusable already compiled (and therefore already debugged - hopefully) modules containing some useful tools and mechanisms (like e.g., error handling).

Elements

The elements of the control should comply with certain rules: implement special interface IElement and be placed in a separate  assembly. If element be instantiated then the element's assembly is dynamically loaded by the control. It means that the elements may be of different types (non-WinForms-control-based type, or based on WinForms controls), and new elements’ types may be developed without change in the control itself. So, OGC allows its extension by designing new types of elements. Currently, two types of elements have been implemented with the following classes

OGCVisualElement - hot spot element, image acting as multi-state buttons, and

OGCComboBox - combo box.

Element classes are implemented in separate dynamically loaded assemblies.

States

The control contains a group (set) of elements. Each of them has several distinct states. Vector of elements' states forms the state of entire control (the control's state). This relationship is represented by the Control States Matrix. By activating an element (e.g., click hot spot element, select an item in combo box) user makes control to transit from one its state to another. Rules for such transitions should be defined for each element by the element's State Transition Matrix. XML configuration file contains Control States Matrix for a control and State Transition Matrices for each of its elements. The matrices structure is comprehensively explained in file Additional_Notes.html that can be loaded along with this article code sample.

Composition

Assembly OGCLib contains basic types and interfaces definitions:

OGControl – elements container that provides main OGC functionality for mouse operations, control’s state machine, docking and error handling mechanisms, initiates elements’ geometry modification.

Configurator – responsible for OGC configuration using XML file.

ErrorEventArgs – object of this class is used as a parameter in error processing delegate; contains error severity and message.

The assembly contains definition for the following interfaces:

IElement – base for the element’s types.

IFactory – base for the element instantiation Factory classes in element type assemblies.

Assemblies OGCVisualElementLib and OGCComboBoxLib contain appropriate IElement-based element classes OGCVisualElement and OGCComboBox. These assemblies are loaded dynamically by OGControl.AddElement() method using Reflection techniques.

Configuration

The entire control configuration data are placed in XML files. File OGCErrorMessages.xml contains all error message templates. Currently, the name of this file is hardcoded and may not be changed. Reading error message template from XML allows user to configure error message and receive it in her/his native language. All OGCs and their elements in the application share this information. The control reads elements related data from its configuration file using Configurator class. Based on these data control loads appropriate assembly and instantiates requested element with the assembly’s Factory : IFactory class. 

Usage

To create the OGC with minimum functionality a parent form should call OGC constructor passing name of an XML configuration file as a parameter, and add newly instantiated control to its controls list:

C#
Controls.Add(new OGControl("Configurator.xml"));

Delegates for processing of OGC’s StateChangedEvent and ErrorEvent static events

C#
delegate void StateChangedEventHandler(OGControl source, EventArgs ea);
delegate void ErrorEventHandler(OGControl source, ErrorEventArgs eea);

should be implemented.

To enhance functionality, some virtual methods, like OnChangeState() and ConstructTip() may be overridden.

Code Sample and Additional Notes

These are available for download. The sample may be built with __RunMe_First_To_Build.bat batch file. The batch files _Run_Example_1.bat and _Run_Example_2.bat allow to run two related examples. File Additional_Notes.html provides more comprehensive explanation of the matrices structure and the sample, while file Data_to_Examples.txt contains Control States Matrices for the examples.

Further Development

The current version of the control may be improved/extended in various ways, e.g. by incorporating design mode to the control (allow size modification, change in elements location with mouse and define their states filling up configuration XML file), adding more functionality employing caption right click floating menu (for instance, making the control invisible), etc. And, of course, additional types of IElement-based elements may be designed.

Conclusions

An approach to WinForms control design characterizing by usage of state machine, XML configuration and handling of uniform-interface-based internal elements is presented. The control with its elements types is largely self-contained. In many cases such structure can spare its user from developing and debugging of control related code.

I will be very much grateful for constructive discussion, suggestions and bugs reports.

Thanks

My deep thanks to .S.Rod. for his suggestions on article organization and title.

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
Software Developer (Senior)
Israel Israel


  • Nov 2010: Code Project Contests - Windows Azure Apps - Winner
  • Feb 2011: Code Project Contests - Windows Azure Apps - Grand Prize Winner



Comments and Discussions

 
GeneralOGC : Unfortunate acronym Pin
Denevers24-Sep-03 13:06
Denevers24-Sep-03 13:06 
GeneralRe: OGC : Unfortunate acronym Pin
Igor Ladnik24-Sep-03 20:26
professionalIgor Ladnik24-Sep-03 20:26 
Generalhelp me,vertically input text Pin
jirigala3-Dec-02 19:21
jirigala3-Dec-02 19:21 
GeneralExelent job !!! Pin
Gidonim3-Dec-02 5:53
Gidonim3-Dec-02 5:53 
GeneralRe: Exelent job !!! Pin
Igor Ladnik3-Dec-02 8:18
professionalIgor Ladnik3-Dec-02 8:18 

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.