January 26, 2007

Simple Wizard Form

The SimpleWizardForm.designer.cs contenst are not here. Put 3 buttons on the Wizard form, 'butCancel', 'butPrevious' and 'butNext'. Use a 2 pixel high label control to get a bevel.

public partial class SimpleWizardForm : Form
{
    protected readonly string NEXT_BUT = @"&Next >";
    protected readonly string FINISH_BUT = @"Fi&nish";

    private Control[] panels;
    private readonly Size MaxPanelSize = new Size(590, 462);
    private readonly Point PanelOffset = new Point(0, 56);

    private int m_pos = 0;

    public SimpleWizardForm(Image img)
    {
        InitializeComponent();
        InitWizardImage(img);
    }

    private void InitWizardImage(Image img)
    {
        
    }

    // Takes an array or list or whatever of that are to 
    // be shown in the Wizard form. They are shown in the 
    // order that they are defined in the list.
    protected void Initialise(IList argPanels)
    {
        panels = new Control[argPanels.Count];
        int ix = 0;
        foreach (Control ctrl in argPanels)
        {
            panels[ix++] = ctrl;// Add the control to the wizard array
            System.Diagnostics.Debug.Assert(
                (ctrl.Size.Width < MaxPanelSize.Width) && 
                (ctrl.Size.Height < MaxPanelSize.Height),
                "Passed a control to the WizardForm which is larger in size (= " +
                ctrl.Size.ToString() + ") than the area available (=" +
                MaxPanelSize.ToString() + ") for showing panels!");                    
            this.Controls.Add(ctrl);
            ctrl.Location = new Point(
                PanelOffset.X + (MaxPanelSize.Width-ctrl.Width) / 2, 
                PanelOffset.Y + (MaxPanelSize.Height-ctrl.Height) / 2);
            ctrl.Visible = false; // All panels are invisible
        }

        m_pos = 0; 
        // Make first panel visible
        panels[m_pos].Visible = true;
        UpdatePrevNextButts();
    }

    private void butNext_Click(object sender, EventArgs e)
    {
        NextPanel();
        UpdatePrevNextButts();
    }

    private void butPrev_Click(object sender, EventArgs e)
    {
        PreviousPanel();
        UpdatePrevNextButts();
    }

    private void NextPanel()
    {
        // If this is the last panel
        if (m_pos == (panels.Length - 1))
        {
            // Close the dialog with a successful outcome
            this.DialogResult = DialogResult.OK;
            Close();
        }
        else
        {
            // Show the next panel
            panels[m_pos].Visible = false;
            m_pos = (m_pos + 1) % panels.Length;
            panels[m_pos].Visible = true;
        }
    }

    private void PreviousPanel()
    {
        panels[m_pos].Visible = false;
        m_pos = (m_pos + 1 + panels.Length) % panels.Length;
        panels[m_pos].Visible = true;
    }

    private void UpdatePrevNextButts()
    {
        if (IsLastPanel())
        {
            butNext.Text = FINISH_BUT; 
        }
        else
        {
            butNext.Text = NEXT_BUT; 
        }
        butPrev.Enabled = (!IsFirstPanel());
    }

    private bool IsLastPanel()
    {
        return m_pos == (panels.Length - 1);
    }

    private bool IsFirstPanel()
    {
        return (m_pos == 0);
    }

    private void butCancel_Click(object sender, EventArgs e)
    {
        this.DialogResult = DialogResult.Cancel;
        Close();
    }
}
And the corresponding WizardForm.Designer.cs
partial class WizardForm
{
    /// <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()
    {
        this.bevelControl1 = new System.Windows.Forms.Label();
        this.butPrev = new System.Windows.Forms.Button();
        this.butNext = new System.Windows.Forms.Button();
        this.butCancel = new System.Windows.Forms.Button();
        
        this.SuspendLayout();
        // 
        // bevelControl1
        // 
        this.bevelControl1.Anchor = 

((System.Windows.Forms.AnchorStyles)(((System.Windows.Forms.AnchorStyles.Bottom 

| System.Windows.Forms.AnchorStyles.Left)
                    | System.Windows.Forms.AnchorStyles.Right)));
        this.bevelControl1.Location = new System.Drawing.Point(-26, 524);
        this.bevelControl1.Name = "bevelControl1";
        this.bevelControl1.Size = new System.Drawing.Size(645, 2);
        this.bevelControl1.TabIndex = 59;
        // 
        // butPrev
        // 
        this.butPrev.Anchor = 

((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | 

System.Windows.Forms.AnchorStyles.Left)));
        this.butPrev.Location = new System.Drawing.Point(299, 534);
        this.butPrev.Name = "butPrev";
        this.butPrev.Size = new System.Drawing.Size(75, 23);
        this.butPrev.TabIndex = 57;
        this.butPrev.Text = "< &Previous";
        this.butPrev.UseVisualStyleBackColor = true;
        this.butPrev.Click += new System.EventHandler(this.butPrev_Click);
        // 
        // butNext
        // 
        this.butNext.Anchor = 

((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | 

System.Windows.Forms.AnchorStyles.Left)));
        this.butNext.Location = new System.Drawing.Point(390, 534);
        this.butNext.Name = "butNext";
        this.butNext.Size = new System.Drawing.Size(75, 23);
        this.butNext.TabIndex = 56;
        this.butNext.Text = "&Next >";
        this.butNext.UseVisualStyleBackColor = true;
        this.butNext.Click += new System.EventHandler(this.butNext_Click);
        // 
        // butCancel
        // 
        this.butCancel.Anchor = 

((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | 

System.Windows.Forms.AnchorStyles.Left)));
        this.butCancel.DialogResult = System.Windows.Forms.DialogResult.Cancel;
        this.butCancel.Location = new System.Drawing.Point(127, 534);
        this.butCancel.Name = "butCancel";
        this.butCancel.Size = new System.Drawing.Size(75, 23);
        this.butCancel.TabIndex = 58;
        this.butCancel.Text = "&Cancel";
        this.butCancel.UseVisualStyleBackColor = true;
        this.butCancel.Click += new System.EventHandler(this.butCancel_Click);
        // 
        // WizardForm
        // 
        this.AcceptButton = this.butNext;
        this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
        this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
        this.CancelButton = this.butCancel;
        this.ClientSize = new System.Drawing.Size(593, 565);
        this.Controls.Add(this.panelBrandHeader);
        this.Controls.Add(this.bevelControl1);
        this.Controls.Add(this.butPrev);
        this.Controls.Add(this.butNext);
        this.Controls.Add(this.butCancel);
        this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.FixedDialog;
        this.MaximizeBox = false;
        this.MinimizeBox = false;
        this.Name = "XXXWizardForm";
        this.StartPosition = 

System.Windows.Forms.FormStartPosition.CenterParent;
        this.Text = "XXX Wizard Form";
        this.ResumeLayout(false);
        this.PerformLayout();

    }

    #endregion

    
    private System.Windows.Forms.Label bevelControl1;
    private System.Windows.Forms.Button butPrev;
    private System.Windows.Forms.Button butNext;
    private System.Windows.Forms.Button butCancel;
}

No comments: