An XML- and State Machine-based Design of a W...

来源:百度文库 编辑:神马文学网 时间:2024/06/03 05:23:54

3,672,780 members and growing!   7,578 now online.txjchen |My Settings |My Bookmarks |My Articles |Sign out
HomeMFC/C++C#ASP.NET.NETVB.NETAll Topics  Help!ArticlesMessage BoardsStoreFrontLounge
All Topics,C#,.NET >>C# Controls >>General
An XML- and State Machine-based Design of a WinForms Control
ByIgor Ladnik.
Usage of state machine and comprehensive XML description of control helps its user to considerably simplify control handling code.
C#
Windows, .NET (.NET 1.0)
Win32, VS
Dev
Posted: 2 Dec 2002
Views: 44,234
Announcements
$10,000 worth of prizes to be wonMonthly Competition

Search   Articles Authors   Advanced Search
Sitemap |Add to IE Search
PrintBroken Article?BookmarkDiscussSend to a friend
26 votes for this article.
Popularity: 6.19. Rating: 4.38 out of 5.
You are signed up for one or morenewsletters but unfortunately we are unable to send you emails. Please clickhere to have an email sent that will allow us to confirm your email address.
Download source files - 124 Kb

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:
Controls.Add(new OGControl("Configurator.xml"));
Delegates for processing of OGC’s StateChangedEvent and ErrorEvent static events
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.
Igor Ladnik

Clickhere to view Igor Ladnik‘s online profile.
Other popular C# Controls articles:
Themed Windows XP style Explorer Bar A fully customizable Windows XP style Explorer Bar that supports Windows XP themes and animated expand/collapse with transparency.
XPTable - .NET ListView meets Java‘s JTable A fully customisable ListView style control based on Java‘s JTable.
SourceGrid - Open Source C# Grid Control SourceGrid is a free open source grid control. Supports virtual grid, custom cells and editors, advanced formatting options and many others features
TaskbarNotifier, a skinnable MSN Messenger-like popup in C# and now in VB.NET too The TaskbarNotifier class allows to display an MSN Messenger-like animated popup with a skinned background
[Top] Rate this Article for us!     PoorExcellent


FAQ  Message score threshold 1.0 2.0 3.0 4.0 5.0    Search comments
View Normal (slow) Preview (slow) Message View Topic View Thread View Expanded (Supporters only)    Per page 10 25 50
New Message Msgs 1 to 5 of 5 (Total: 5) (Refresh) First Prev Next
Subject  Author  Date

 OGC : Unfortunate acronym  Denevers  19:06 24 Sep ‘03
  I took me a while to understand that this article had nothing to do with OGC, as in "Open GIS Consortium". OGC actually has a set of protocol to get maps over the internet (as Web Services) and the first figure *is* a map. i was reading the article and kept asking to myself "where does he talk about the map ?" until I read back what OGC meant.
Excellent work nonetheless
[Reply |Email |View Thread |Get Link] Rate this message:12345 (out of 5)
Report asSpam orAbuse

 Re: OGC : Unfortunate acronym  Igor Ladnik  2:26 25 Sep ‘03
  Thank you for your positive response and information. Name collision is unfortunate indeed.
[Reply |Email |View Thread |Get Link] Rate this message:12345 (out of 5)
Report asSpam orAbuse


 help me,vertically input text  jirigala  1:21 4 Dec ‘02
  help me,vertically input text
I want a control ,that can vertically input text.
use it input mongol,how can help me,my name is jirigala,
i am from China, my home page
www.mongolmail.com
jirigala@mengguren.com
[Reply |Email |View Thread |Get Link] Score: 1.0 (1 vote). Rate this message:12345 (out of 5)
Report asSpam orAbuse


 Exelent job !!!  Gidonim  11:53 3 Dec ‘02
 It is remarkable and very useful gui-element.
Molotsky M.
[Reply |Email |View Thread |Get Link] Rate this message:12345 (out of 5)
Report asSpam orAbuse

 Re: Exelent job !!!  Igor Ladnik  14:18 3 Dec ‘02
  Thanks
[Reply |Email |View Thread |Get Link] Rate this message:12345 (out of 5)
Report asSpam orAbuse

Last Visit: 11:43 Tuesday 2nd January, 2007 First Prev Next
General comment   News / Info   Question   Answer   Joke / Game   Admin message
Updated: 2 Dec 2002 Article content copyright Igor Ladnik, 2002
everything else Copyright ©CodeProject, 1999-2006.
Web19 |Advertise on The Code Project |Privacy

The Ultimate Toolbox •ASP Alliance •Developer Fusion •Developersdex •DevGuru •Programmers Heaven •Planet Source Code •Tek-Tips Forums •
Help!
Articles
Message Boards
StoreFront
Lounge
What is ‘The Code Project‘?
General FAQ
Post a Question
Site Directory
About Us
Latest
Most Popular
Search
Site Directory
Submit an Article
Update an Article
Article Competition
Windows Vista
Visual C++
ATL / WTL / STL
COM
C++/CLI
C#
ASP.NET
VB.NET
Web Development
.NET Framework
Mobile Development
SQL / ADO / ADO.NET
XML / XSL
OS / SysAdmin
Work Issues
Article Requests
Collaboration
General Discussions
Hardware
Algorithms / Math
Design and Architecture
Subtle Bugs
Suggestions
The Soapbox