Click here to Skip to main content
Click here to Skip to main content
Add your own
alternative version

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.
xmlstatemachinewfcontrol_src.zip
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
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
using OGCLib;


namespace TestOGC
{
	// Summary description for TestOGCForm.
	public class TestOGCForm : System.Windows.Forms.Form
	{
		// Const
		private const string mapControlName = "Map";

		// ReadOnly
		private readonly Point beetleDepartPoint = new Point(0, 0x50);
		private readonly Point speed = new Point(2, 2);
		private readonly Size formInitSize = new Size(620, 550);

		static private string mapConfigFileName;

		// Enum
		enum MapControlVisibility
		{
			Hide = 0,
			Show = 1,
		}

		// Required designer variable.
		private System.ComponentModel.Container components = null;
		
		public TestOGCForm()
		{
			//
			// Required for Windows Form Designer support
			//
			InitializeComponent();

			Size = formInitSize;

			///////////////////////////////////////////////////////////////////////////////
			// Set Message Handlers for all OGControl derived controls
			OGControl.StateChangedEvent += 
				new OGControl.StateChangedEventHandler(OnOGControlStateChanged);
			OGControl.ErrorEvent += 
				new OGControl.ErrorEventHandler(OnOGControlError);		
			
			///////////////////////////////////////////////////////////////////////////////
			// OGControls
			Controls.Add(new OGControl(mapConfigFileName));
			
			OGControl ogcToggle = new OGControl("Config_Toggle.xml");
			Controls.Add(ogcToggle);

			// Show/Hide one map control acording to the state of Toggle control
			ShowHideOneMapControl((int)MapControlVisibility.Show == ogcToggle.CurrentState);
		}

		public virtual void OnOGControlStateChanged(OGControl source, EventArgs ea)
		{
			switch (source.Name)
			{
				case "Toggle":
					// Show/Hide the map control acording to new state of Toggle control
					ShowHideOneMapControl((int)MapControlVisibility.Show == source.CurrentState);
					break;

				case mapControlName:
					break;
			}

// Comment the following block to suppress the "Parent's StateChanged Event Handler" message box
// /*
			MessageBox.Show(source.Name + ":  \"" + source.HitElement.GetName() + 
				"\" is clicked!" + "\nIts state is \"" + 
				source.HitElement.GetStateName() + "\"\nCurrent Control State: " +
				source.CurrentState,
				"Parent's StateChanged Event Handler");
// */		
		}
				
		public virtual void OnOGControlError(OGControl source, ErrorEventArgs eea)
		{
			string severity = "";
			switch (eea.errorSeverity)
			{
				case ErrorSeverity.Error:
					severity = "Error";
					break;

				case ErrorSeverity.Warning:
					severity = "Warning";
					break;
			}

			MessageBox.Show(severity + " in '" + source.Name + "' control: " + eea.errorMessage,
				"Parent's Error Event Handler",
				MessageBoxButtons.OK, MessageBoxIcon.Error);
		}
	
		// Show/Hide one map control
		private void ShowHideOneMapControl(bool show)
		{
			for (int i=0; i<Controls.Count; i++)
				if (mapControlName == Controls[i].Name)
				{
					Controls[i].Visible = show;
					break;
				}
		}
	
	// Clean up any resources being used.
		protected override void Dispose( bool disposing )
		{
			if( disposing )
			{
				if (components != null) 
				{
					components.Dispose();
				}
			}
			base.Dispose( disposing );
		}

		#region Windows Form Designer generated code
		/// <summary>
		/// Required method for Designer support - do not modify
		/// the contents of this method with the code editor.
		/// </summary>
		private void InitializeComponent()
		{
			// 
			// TestOGCForm
			// 
			this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
			this.ClientSize = new System.Drawing.Size(292, 273);
			this.Name = "TestOGCForm";
			this.Text = "OGC Test";

		}
		#endregion

		// The main entry point for the application.
		[STAThread]
		static void Main(string[] args) 
		{
			if (args.Length > 0) 
				mapConfigFileName = args[0];
			else
			{
				MessageBox.Show("Config. XML file should be specified as a parameter.",
					"Error in TestOGC", MessageBoxButtons.OK, MessageBoxIcon.Error);
				return;
			}

			Application.Run(new TestOGCForm());
		}
	}
}

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

Share

About the Author

Igor Ladnik
Architect
Israel Israel


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

Follow on   LinkedIn

| Advertise | Privacy | Mobile
Web02 | 2.8.141015.1 | Last Updated 2 Dec 2002
Article Copyright 2002 by Igor Ladnik
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid