Click here to Skip to main content
15,884,473 members
Articles / Programming Languages / C#

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  
Usage of state machine and comprehensive XML description of control helps its user to considerably simplify control handling code.
using System;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
using System.Reflection;
using OGCLib;

//
// General Information about an assembly is controlled through the following 
// set of attributes. Change these attribute values to modify the information
// associated with an assembly.
//
[assembly: AssemblyTitle("OGCComboBoxLib")]
[assembly: AssemblyDescription("This assembly contains classes for the combo box element.")]
[assembly: AssemblyCompany("Igor Ladnik")]
[assembly: AssemblyProduct("Open Group Control")]

[assembly: AssemblyVersion("1.2.*")]

namespace OGCComboBoxLib
{
	///////////////////////////////////////////////////////////////////////////////////
	// Class CBComboBox
	///////////////////////////////////////////////////////////////////////////////////
	public class OGCComboBox : ComboBox, IElement
	{
		// Variables
		private int		  id;
		private OGControl ogControl = null;

		// Specify positions of element for hrizontal and vertical orientation of control
		private Point	  horizPosition;				
		private Point	  vertPosition;

		// Number of element's states
		private int		  statesMax = 0;

		// State Transition matrix
		private int[,]	  aiControlStateTransition = null;
		
		// false - if SetCurrentState(int currentState) method of element has not been called yet.
		// true  - otherwise
		private bool	  selectionChanged = false;
		private bool	  justStarted = true;

		// CONSTRUCTOR
		public OGCComboBox(OGControl ogControl)
		{
			this.ogControl = ogControl;
			Visible = false;
		}

		// METHODS

		// Virtual
		protected override void OnSelectionChangeCommitted(EventArgs ea)
		{
			selectionChanged = true;
		}

		protected override void OnSelectedValueChanged(EventArgs ea)
		{
			if (null != ogControl && selectionChanged)
			{
				ogControl.HitElement = this;
				ogControl.ChangeState(GetNewControlState());
			}

			selectionChanged = false;
		}

		// IElement implementation methods
		public void Initialize(string name, Point horizPosition, Point vertPosition, Size size)
		{
			if (null != ogControl)
			{
				Name = name;
				id = ogControl.VisualElementsNumber;
				this.horizPosition = horizPosition;
				this.vertPosition = vertPosition;
				
				this.horizPosition.Y += ogControl.CaptionHeight; 
				this.vertPosition.Y += ogControl.CaptionHeight; 			
				
				UpdateGeometry();

				Size = size;
				Visible = true;
			}
		}

		public void SetControlStateTransitionElement(int currCtrlState, int currState, 
			int nextCtrlState)
		{
			if (null == aiControlStateTransition)
			{
				aiControlStateTransition = new int[ogControl.ControlStatesMax, statesMax];
				for (int i=0; i<ogControl.ControlStatesMax; i++)
					for (int j=0; j<statesMax; j++)
						aiControlStateTransition[i, j] = -1;
			}

			aiControlStateTransition[currCtrlState, currState] = nextCtrlState;
		}

		public int AddState(string name, string pictureName, string ext)
		{
			Items.Add(name);
			return ++statesMax;
		}

		public int GetNewControlState()
		{
			if (null != ogControl && null != aiControlStateTransition)
				return aiControlStateTransition[ogControl.CurrentState, SelectedIndex];
	
			return -1;
		}

		public void SetCurrentState(int currentState)
		{
			if (-1 < currentState && Items.Count > currentState)
			{
				SelectedIndex = currentState;
				Update();
			}

			if (-1 == currentState && justStarted)
			{	
				SelectedIndex = 0;
				Update();
			}

			justStarted = false;
		}

		public bool HasImage()
		{
			return false;
		}

		public string GetName()
		{
			return Name;
		}

		public int GetId()
		{
			return id;
		}

		public string GetStateName()
		{
			return SelectedItem.ToString();
		}

		public int GetNextStatesNum()
		{
			return Items.Count;
		}

		public void UpdateGeometry()
		{
			Point position = OGCLib.Orientation.Horizontal == ogControl.GetOrientation()
				? horizPosition : vertPosition;

			Left = position.X;
			Top = position.Y;
		}

		public void Draw(Graphics grfx, bool show)
		{
			Visible = show;
		}

		public bool HitTest(MouseEventArgs mea)
		{
			return false;
		}
	}

	///////////////////////////////////////////////////////////////////////////////////
	// Class Factory
	///////////////////////////////////////////////////////////////////////////////////
	public class Factory : IFactory
	{
		public IElement CreateElement(OGControl ogControl)
		{
			OGCComboBox comboBox = new OGCComboBox(ogControl);
			ogControl.Controls.Add(comboBox);
			return comboBox;
		}	
	}
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

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.

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