Click here to Skip to main content
15,885,216 members
Articles / Programming Languages / C#

Wizard Control based on TabControl

Rate me:
Please Sign up or sign in to vote.
3.43/5 (6 votes)
3 May 2010CPOL4 min read 41.2K   2.6K   24  
Wizard Control based on TabControl. Takes care of next and back movements.
using System;
using System.Windows.Forms;

namespace EZWizard
{
    public partial class FormWizard : Form
    {
        #region Form Wizard Generation And Functionality Code
        #region Windows Generated Code


        /// <summary>
        /// Required designer variable.
        /// </summary>
        private System.ComponentModel.IContainer components = null;

        /// <summary>
        /// Clean up any resources being used.
        /// </summary>
        /// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
        protected override void Dispose(bool disposing)
        {
            if (disposing && (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()
        {
            System.ComponentModel.ComponentResourceManager resources = new System.ComponentModel.ComponentResourceManager(typeof(FormWizard));
            this.tableLayoutPanel1 = new System.Windows.Forms.TableLayoutPanel();
            this.panel2 = new System.Windows.Forms.Panel();
            this.m_buttonBack = new Button();
            this.m_buttonFinish = new Button();
            this.m_buttonNext = new Button();
            this.m_buttonCancel = new Button();
            this.m_panelContainer = new System.Windows.Forms.Panel();
            this.tabControl = new TabControl();
            this.tabPage1 = new System.Windows.Forms.TabPage();
            this.tableLayoutPanel2 = new System.Windows.Forms.TableLayoutPanel();
            this.panel3 = new System.Windows.Forms.Panel();
            this.panel1 = new System.Windows.Forms.Panel();
            this.m_labelSubtitle = new Label();
            this.label1 = new Label();
            this.tableLayoutPanel1.SuspendLayout();
            this.panel2.SuspendLayout();
            this.m_panelContainer.SuspendLayout();
            this.tabControl.SuspendLayout();
            this.tabPage1.SuspendLayout();
            this.tableLayoutPanel2.SuspendLayout();
            this.panel1.SuspendLayout();
            this.SuspendLayout();
            // 
            // tableLayoutPanel1
            // 
            this.tableLayoutPanel1.BackColor = System.Drawing.Color.Transparent;
            resources.ApplyResources(this.tableLayoutPanel1, "tableLayoutPanel1");
            this.tableLayoutPanel1.Controls.Add(this.panel2, 0, 2);
            this.tableLayoutPanel1.Controls.Add(this.m_panelContainer, 0, 1);
            this.tableLayoutPanel1.Controls.Add(this.panel1, 0, 0);
            this.tableLayoutPanel1.Name = "tableLayoutPanel1";
            // 
            // panel2
            // 
            this.panel2.Controls.Add(this.m_buttonBack);
            this.panel2.Controls.Add(this.m_buttonFinish);
            this.panel2.Controls.Add(this.m_buttonNext);
            this.panel2.Controls.Add(this.m_buttonCancel);
            resources.ApplyResources(this.panel2, "panel2");
            this.panel2.Name = "panel2";
            // 
            // m_buttonBack
            // 
            resources.ApplyResources(this.m_buttonBack, "m_buttonBack");
            this.m_buttonBack.BackColor = System.Drawing.Color.LightSteelBlue;
            this.m_buttonBack.Name = "m_buttonBack";
            this.m_buttonBack.UseVisualStyleBackColor = false;
            this.m_buttonBack.Click += new System.EventHandler(this.m_buttonBack_Click);
            // 
            // m_buttonFinish
            // 
            resources.ApplyResources(this.m_buttonFinish, "m_buttonFinish");
            this.m_buttonFinish.BackColor = System.Drawing.Color.LightSteelBlue;
            this.m_buttonFinish.Name = "m_buttonFinish";
            this.m_buttonFinish.UseVisualStyleBackColor = false;
            // 
            // m_buttonNext
            // 
            resources.ApplyResources(this.m_buttonNext, "m_buttonNext");
            this.m_buttonNext.BackColor = System.Drawing.Color.LightSteelBlue;
            this.m_buttonNext.Name = "m_buttonNext";
            this.m_buttonNext.UseVisualStyleBackColor = false;
            this.m_buttonNext.Click += new System.EventHandler(this.m_buttonNext_Click);
            // 
            // m_buttonCancel
            // 
            resources.ApplyResources(this.m_buttonCancel, "m_buttonCancel");
            this.m_buttonCancel.BackColor = System.Drawing.Color.LightSteelBlue;
            this.m_buttonCancel.DialogResult = System.Windows.Forms.DialogResult.Cancel;
            this.m_buttonCancel.Name = "m_buttonCancel";
            this.m_buttonCancel.UseVisualStyleBackColor = false;
            // 
            // m_panelContainer
            // 
            this.m_panelContainer.Controls.Add(this.tabControl);
            resources.ApplyResources(this.m_panelContainer, "m_panelContainer");
            this.m_panelContainer.Name = "m_panelContainer";
            // 
            // tabControl
            // 
            this.tabControl.Controls.Add(this.tabPage1);
            resources.ApplyResources(this.tabControl, "tabControl");
            this.tabControl.HotTrack = true;
            this.tabControl.Name = "tabControl";
            this.tabControl.SelectedIndex = 0;
            // 
            // tabPage1
            // 
            this.tabPage1.Controls.Add(this.tableLayoutPanel2);
            resources.ApplyResources(this.tabPage1, "tabPage1");
            this.tabPage1.Name = "tabPage1";
            this.tabPage1.UseVisualStyleBackColor = true;
            // 
            // tableLayoutPanel2
            // 
            resources.ApplyResources(this.tableLayoutPanel2, "tableLayoutPanel2");
            this.tableLayoutPanel2.Controls.Add(this.panel3, 0, 0);
            this.tableLayoutPanel2.Name = "tableLayoutPanel2";
            // 
            // panel3
            // 
            resources.ApplyResources(this.panel3, "panel3");
            this.panel3.Name = "panel3";
            // 
            // panel1
            // 
            this.panel1.Controls.Add(this.m_labelSubtitle);
            this.panel1.Controls.Add(this.label1);
            resources.ApplyResources(this.panel1, "panel1");
            this.panel1.Name = "panel1";
            // 
            // m_labelSubtitle
            // 
            resources.ApplyResources(this.m_labelSubtitle, "m_labelSubtitle");
            this.m_labelSubtitle.Name = "m_labelSubtitle";
            // 
            // label1
            // 
            resources.ApplyResources(this.label1, "label1");
            this.label1.Name = "label1";
            // 
            // FormWizard
            // 
            this.AcceptButton = this.m_buttonFinish;
            resources.ApplyResources(this, "$this");
            this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
            this.CancelButton = this.m_buttonCancel;
            this.Controls.Add(this.tableLayoutPanel1);
            this.Name = "FormWizard";
            this.tableLayoutPanel1.ResumeLayout(false);
            this.panel2.ResumeLayout(false);
            this.m_panelContainer.ResumeLayout(false);
            this.tabControl.ResumeLayout(false);
            this.tabPage1.ResumeLayout(false);
            this.tableLayoutPanel2.ResumeLayout(false);
            this.panel1.ResumeLayout(false);
            this.panel1.PerformLayout();
            this.ResumeLayout(false);

        }

        #endregion

        public System.Windows.Forms.TableLayoutPanel tableLayoutPanel1;
        public TabControl tabControl;
        public System.Windows.Forms.Panel panel2;
        public Button m_buttonCancel;
        public Button m_buttonBack;
        public Button m_buttonNext;
        public Button m_buttonFinish;
        public System.Windows.Forms.Panel m_panelContainer;
        public System.Windows.Forms.TabPage tabPage1;
        private System.Windows.Forms.Panel panel1;
        private Label m_labelSubtitle;
        private Label label1;
        private System.Windows.Forms.TableLayoutPanel tableLayoutPanel2;
        private System.Windows.Forms.Panel panel3;
        #endregion



        #region Form Wizard Functionality

        int currentPanelIndex;

        private void SetCurrentTab(int currentIndex)
        {
            if (currentPanelIndex > -1) //remove all old controls back to tabcontrol
            {
                int count = m_panelContainer.Controls.Count;

                for (int i = count - 1; i >= 0; i--)
                {
                    Control control = m_panelContainer.Controls[i];
                    m_panelContainer.Controls.Remove(control);
                    tabControl.TabPages[currentPanelIndex].Controls.Add(control);
                }
            }

            {
                int count = tabControl.TabPages[currentIndex].Controls.Count;
                m_labelSubtitle.Text = (currentIndex + 1) + " : " + tabControl.TabPages[currentIndex].Text;
                for (int i = count - 1; i >= 0; i--)
                {
                    Control control = tabControl.TabPages[currentIndex].Controls[i];
                    tabControl.TabPages[currentIndex].Controls.Remove(control);
                    m_panelContainer.Controls.Add(control);
                }

                currentPanelIndex = currentIndex;
            }
        }

        private void m_buttonNext_Click(object sender, EventArgs e)
        {
            if (NextButtonClicked())
            {
                int currentIndex = currentPanelIndex;
                currentIndex++;
                if (!(currentIndex >= tabControl.TabCount))
                {
                    SetCurrentTab(currentIndex);
                    m_buttonBack.Enabled = true;

                    if (currentIndex == tabControl.TabCount - 1)
                    {
                        m_buttonNext.Enabled = false;
                        m_buttonFinish.Enabled = true;
                    }
                }
            }
        }

        private void m_buttonBack_Click(object sender, EventArgs e)
        {
            if (BackButtonClicked())
            {
                int currentIndex = currentPanelIndex;
                currentIndex--;
                if (currentIndex >= 0)
                {
                    SetCurrentTab(currentIndex);

                    m_buttonNext.Enabled = true;
                    m_buttonFinish.Enabled = false;

                    if (currentIndex == 0)
                        m_buttonBack.Enabled = false;
                }
            }
        }
        #endregion}
        #endregion
        public FormWizard()
        {
            InitializeComponent();
            m_panelContainer.Controls.Remove(tabControl);
            currentPanelIndex = -1;
            SetCurrentTab(0);

            LoadData();
        }

        private void LoadData()
        {

        }

        bool NextButtonClicked()
        {
            switch (currentPanelIndex)
            {
                case 0:// New Order Name Provided and Buyer Selected
                    {

                    }
                    break;
                case 1:
                    { }
                    break;
                default:
                    { }
                    break;
            }

            return true;
        }

        bool BackButtonClicked()
        {
            switch (currentPanelIndex)
            {
                case 0:
                    { }
                    break;
                case 1:
                    { }
                    break;
                default:
                    { }
                    break;
            }
            return true;
        }
    }
}



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 Code Project Open License (CPOL)


Written By
India India
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions