Click here to Skip to main content
15,893,594 members
Articles / Desktop Programming / Windows Forms

A Plug-in Wizard Framework

Rate me:
Please Sign up or sign in to vote.
4.82/5 (31 votes)
21 May 2008BSD7 min read 91.4K   1.2K   127  
A wizard framework that supports plug-ins for the wizard pages.
using System;
using System.Collections.Generic;
using System.Windows.Forms;

namespace Clifton.Wizard.Interfaces
{
	/// <summary>
	/// This abstract class defines common fields, properties, and certain
	/// default behavior that each plugin can leverage.
	/// </summary>
	public abstract class WizardBase : IPlugin
	{
		/// <summary>
		/// The event that the plugin can use to notify the wizard of a state change.
		/// </summary>
		public event EventHandler UpdateStateEvent;
		
		/// <summary>
		/// The control list is preserved so that the control's state is maintained
		/// as the user navigates backwards and forwards through the wizard.
		/// </summary>
		protected List<Control> ctrlList;
		protected Form form;

		/// <summary>
		/// True if the plugin's data is validated and the user can proceed with
		/// the next wizard page.  True is the default.
		/// </summary>
		public virtual bool IsValid
		{
			get { return true; }
		}

		/// <summary>
		/// True if the plugin is going to provide help.  The default is false.
		/// </summary>
		public virtual bool HasHelp
		{
			get { return false; }
		}

		/// <summary>
		/// Constructor.
		/// </summary>
		public WizardBase()
		{
			ctrlList = new List<Control>();
		}

		/// <summary>
		/// The plugin can override this method if it needs to do
		/// something before the wizard proceeds to the next page.
		/// </summary>
		public virtual void OnNext()
		{
			// Do nothing.
		}

		/// <summary>
		/// The plugin can override this method if it wants to display 
		/// some help.
		/// </summary>
		public virtual void OnHelp()
		{
			// Do nothing.
		}

		/// <summary>
		/// Returns the controls from the form that the plugin assigned
		/// in the class.  The plugin can override this method to return
		/// a custom control list.
		/// </summary>
		/// <returns></returns>
		public virtual List<Control> GetControls()
		{
			// If this is the first time we're calling this method,
			// load the controls from the plugin form.
			if (ctrlList.Count == 0)
			{
				// Once loaded, we reuse the same control instances
				// which as the advantage of preserving state if the
				// user goes back to a previous page (and forward again.)
				GetFormControls();
			}

			// Otherwise, return the control list that we acquired from
			// the form. 
			return ctrlList;
		}

		/// <summary>
		/// Iterates through the form's top level controls to construct
		/// a list of form controls.
		/// </summary>
		protected virtual void GetFormControls()
		{
			foreach (Control c in form.Controls)
			{
				ctrlList.Add(c);
			}
		}

		/// <summary>
		/// The plugin can call this method to raise the UpdateStateEvent,
		/// which informs the wizard that the button states need to be updated.
		/// </summary>
		protected void RaiseUpdateState()
		{
			if (UpdateStateEvent != null)
			{
				UpdateStateEvent(this, EventArgs.Empty);
			}
		}
	}
}

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, along with any associated source code and files, is licensed under The BSD License


Written By
Architect Interacx
United States United States
Blog: https://marcclifton.wordpress.com/
Home Page: http://www.marcclifton.com
Research: http://www.higherorderprogramming.com/
GitHub: https://github.com/cliftonm

All my life I have been passionate about architecture / software design, as this is the cornerstone to a maintainable and extensible application. As such, I have enjoyed exploring some crazy ideas and discovering that they are not so crazy after all. I also love writing about my ideas and seeing the community response. As a consultant, I've enjoyed working in a wide range of industries such as aerospace, boatyard management, remote sensing, emergency services / data management, and casino operations. I've done a variety of pro-bono work non-profit organizations related to nature conservancy, drug recovery and women's health.

Comments and Discussions