12,624,121 members (33,571 online)

75.8K views
46 bookmarked
Posted

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

, 1 Dec 2002
Usage of state machine and comprehensive XML description of control helps its user to considerably simplify control handling code.
 res Mixed.gif Show.gif Hide.gif EstoniaFlag.gif Weather.gif LatviaFlag.gif LithuaniaFlag.gif hand.cur Lithuania0.gif Estonia0.gif Baltia.jpg Latvia1.gif Lithuania1.gif Estonia1.gif Latvia0.gif NoFlag.gif Raining.gif Sunny.gif ``` The Code Project

This file is intended to serve as an appendix to an article "An XML- and State Machine-based Design of a WinForms Control".

State Machine Related Matrices

The control contains set of elements. Each of them has several distinct states. Vector of elements' states forms the state of entire control. This relationship is represented by the Control States Matrix.

Control States Matrix definition

Control's State Element
0 1 ... n ... N-1
0 Element's State |
1 |
... V
k ---------- --------- ------> m n
...
K-1

To describe the matrix, let�s assume that the control has N elements numbered from 0 to N-1. n-th element has Mn states (from 0 to Mn-1). Let�s define state k of the entire control as a vector of mn states (n-th� element in its m-th state). Assuming that control has K distinct states we get Control States Matrix with K rows (control�s states) and N columns (elements). Value mn of matrix [k, n] cell shows that if the control is in the k-th state then its n-th element is in its m-th state. Value of [k, n] cell equaled to -1 means that any state of n-th element is �acceptable� for the control's k-th state. Value of all cells of the matrix is set by default to -1. Values of specific cells (deferred from -1) are given in XML configuration file.

Rules for transition from one control's state to another are defined by the State Transition Matrices. Such a matrix should be assigned to each element. The matrices are in configuration file. They have also K rows corresponding to the control's states. But their columns represent possible variants to change element's state.

State Transition Matrix definition

Control's State Variants to Change Elenment's State
0 1 ... z ... Z-1
0 Control's State |
1 |
... V
k   --------- --------- ------> k new
...
K-1

For example, for OGCVisualElement there is only one possible variant to change its state � being clicked. So, for this type of element State Transition Matrix is reduced to vector. The k-th coordinate of this vector equaled to knew means that if this element is clicked when control is in the k-th state then the control will transit to its knew-th state.

On the other hand, OGCComboBox element with Z strings in the list has Z possible variants to change its state (select any of the strings). So, OGCComboBox element is characterized with [K * Z] State Transition Matrix. [k, z]-th cell of this matrix equaled to knew means that if the z-th string is selected in combo box when the control is in k-th state then the control will transit to the knew-th state.

knew equaled to -1 indicates that no change in the control's state will be made in response to this action (i.e., knew = k, but it is easier to assign a uniform value -1 by default). Value of all cells of the matrix (vector) is set by default to -1. Values of specific cells (deferred from -1) are given in XML configuration file.

Error Handling

Error handling mechanism is chiefly located in OGControl class. The ErrorEvent is generated when error occurs. An instance of ErrorEventArgs class containing error�s severity and message is sent as a parameter to the delegate implementation. Error message templates are read from OGCErrorMessages.xml file by Configurator and added to appropriate list in OGControl. Message templates are listed in OGCErrorMessages.xml file.

Docking

The docking position of the control is changed by dragging the control with its caption. Position of an element within control may be specified (in XML configuration file) differently for cases of horizontal (docked to top, bottom or fill) and vertical (docked to left or right) orientation of the control. Images also rotate on + or � 90 degrees when orientation changes if Rotate value of the OGControl tag in XML configuration file is set to �Yes�.

Sample Description

The sample presents a WinForm containing two open group controls, namely, the Toggle and the Map. The Toggle control with just one hot spot element has two states. Depending on a current Toggle's state parent forms shows or hides the Map control. The Toggle control is configured with the Config_Toggle.xml file. For this control the Rotate property is initially set to "Yes". Thus, orientation of its hot spot element changes when control's orientation changes due to docking. The Map control depicts map of three Baltic states and related combo box(es). In Example 1 each state may be selected either by click on its territory or by appropriate selection in country combo box. In Examlpe 2 not only the state but also weather may be selected. Files Config_Map.xml and Config_Map_WithWeather.xml configure the Map control for the two examples. Appropriate Control States and State Transition matrices and vectors may be learned from these files (for both examples Control States matrices in conventional form are also presented in the Matrices_to_Examples.txt file). Cells with default -1 values are omitted in the configuration files and also in the Matrices_to_Examples.txt file.

In Example 1 the Control States Matrix has no default value (except for the 0-th element column, but this element has only one state anyway). It means that each control's state strictly defines state of each element. In Example2 control deals with two separate tasks (selection of a state and selection of weather). This leads to presence of default -1 values in Control States Matrix, and, therefore, to uncertainity: e.g., for one selected state all kinds of weather may be selected. So, it is not possible to save (say, for the next application run) exact state of all elements using the Control States Matrix alone. If such a necessity exists then special measures should be taken (like saving elements' states explicitly - this is not done in sample presented).

In Example 2 strict control's states definition leads to considerable increase in their number (multiplication of possibilities numbers for all tasks). This consideration should be taken into account while design and use control.

Pictures Sources.  Map picture was taken from here. Weather pictures were copied from the CNN weather site and undergone some modifications.

```

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

A list of licenses authors might use can be found here

## Share

 Architect Israel

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

## You may also be interested in...

 Pro Pro