Saturday, June 23, 2012

Creating Wizard using C# Form (Tab Control)

In one of the previous posting there is a Wizard implementation which used console application and multiple forms. But sometimes requirements are so that implementation needed to some sort of Wizard type but not with all pomp and show, even time constraint makes it hard to achieve an evolving application.

Creating Wizard using tab control is the other to do that, it is simple way to do that. It is a single form implementation with Wizard flavor. This implementation has following basic tweaks to achieve, rest of the implementations and functionality are user specific:

  1. Inherit TabControl and hide tabs by supressing TCM_ADJUSTRECT
  2. Then replace the instance of TabControl Class object initialisation with your class (in my case I names WizardTabControl). Do this replacement once you have completed the screen design and feature is complete.
  3. Set the dock property to Fill for TabPage

Following changes in Form.Designer.cs is the only thing needed. Add Following code at the end to inherit the TabControl class.
namespace System.Windows.Forms
    public class WizardTabControl : TabControl
        protected override void WndProc(ref Message m)
            if (m.Msg == 0x1328 && !DesignMode) // Hide tabs by trapping the TCM_ADJUSTRECT message
                m.Result = IntPtr.Zero;
                base.WndProc(ref m);

Changes in Form class, replace the TabControl reference with WizardTabControl
private System.Windows.Forms.WizardTabControl tabControl1;
private void InitializeComponent()
    this.tabControl1 = new System.Windows.Forms.WizardTabControl();