Wizard in C#
As we know there is no wizard class in .net framework. I just write two classes for control wizard in C#.
The code list here.
1, BaseWizardForm: The wizard each page’s base form
The form will auto generate the wizard step button, you can set the button type in the derived form class.
BaseWizardFormusing System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Windows.Forms; namespace WindowsFormsApplication1 { public enum WizardButtons { None, OK, Cancel, OKCancel, NextCancel, BackNextCancel, BackFinishCancel, } public partial class BaseWizardForm : Form { /// <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.backCommonButton = new System.Windows.Forms.Button(); this.nextCommonButton = new System.Windows.Forms.Button(); this.cancelCommonButton = new System.Windows.Forms.Button(); this.SuspendLayout(); // // backCommonButton // this.backCommonButton.BackColor = System.Drawing.Color.Transparent; this.backCommonButton.DialogResult = System.Windows.Forms.DialogResult.Retry; this.backCommonButton.FlatStyle = System.Windows.Forms.FlatStyle.Flat; this.backCommonButton.Location = new System.Drawing.Point(194, 338); this.backCommonButton.Name = "backCommonButton"; this.backCommonButton.Size = new System.Drawing.Size(80, 26); this.backCommonButton.TabIndex = 3; this.backCommonButton.Text = "Back"; this.backCommonButton.UseVisualStyleBackColor = true; this.backCommonButton.Click += new System.EventHandler(this.backCommonButton_Click); // // nextCommonButton // this.nextCommonButton.BackColor = System.Drawing.Color.Transparent; this.nextCommonButton.DialogResult = System.Windows.Forms.DialogResult.OK; this.nextCommonButton.FlatStyle = System.Windows.Forms.FlatStyle.Flat; this.nextCommonButton.Location = new System.Drawing.Point(284, 338); this.nextCommonButton.Name = "nextCommonButton"; this.nextCommonButton.Size = new System.Drawing.Size(80, 26); this.nextCommonButton.TabIndex = 4; this.nextCommonButton.Text = "Next"; this.nextCommonButton.UseVisualStyleBackColor = true; this.nextCommonButton.Click += new System.EventHandler(this.nextCommonButton_Click); // // cancelCommonButton // this.cancelCommonButton.BackColor = System.Drawing.Color.Transparent; this.cancelCommonButton.DialogResult = System.Windows.Forms.DialogResult.Cancel; this.cancelCommonButton.FlatStyle = System.Windows.Forms.FlatStyle.Flat; this.cancelCommonButton.Location = new System.Drawing.Point(374, 338); this.cancelCommonButton.Name = "cancelCommonButton"; this.cancelCommonButton.Size = new System.Drawing.Size(80, 26); this.cancelCommonButton.TabIndex = 5; this.cancelCommonButton.Text = "Cancel"; this.cancelCommonButton.UseVisualStyleBackColor = true; this.cancelCommonButton.Click += new System.EventHandler(this.cancelCommonButton_Click); // // BaseWizardForm // this.AcceptButton = this.nextCommonButton; this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.None; this.CancelButton = this.cancelCommonButton; this.ClientSize = new System.Drawing.Size(514, 427); this.Controls.Add(this.cancelCommonButton); this.Controls.Add(this.nextCommonButton); this.Controls.Add(this.backCommonButton); this.MaximizeBox = false; this.MinimizeBox = false; this.Name = "BaseWizardForm"; this.ShowIcon = false; this.ShowInTaskbar = false; this.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen; this.Text = "BaseWizardForm"; this.ResumeLayout(false); } #endregion protected Button backCommonButton; protected Button nextCommonButton; protected Button cancelCommonButton; protected WizardButtons formButtons = WizardButtons.OKCancel; public WizardButtons WizardButton { get { return this.formButtons; } set { this.formButtons = value; this.ChangeFormButtons(value); } } public BaseWizardForm() { InitializeComponent(); } protected override void OnCreateControl() { base.OnCreateControl(); this.AutoScaleMode = AutoScaleMode.None; } private void ChangeFormButtons(WizardButtons buttons) { this.RePositionButtons(); switch (buttons) { case WizardButtons.None: { this.backCommonButton.Visible = false; this.nextCommonButton.Visible = false; this.cancelCommonButton.Visible = false; } break; case WizardButtons.OK: { this.nextCommonButton.Text = "OK"; this.nextCommonButton.Location = this.cancelCommonButton.Location; this.backCommonButton.Visible = false; this.cancelCommonButton.Visible = false; this.nextCommonButton.Visible = true; } break; case WizardButtons.Cancel: { this.cancelCommonButton.Text = "Cancel"; this.backCommonButton.Visible = false; this.nextCommonButton.Visible = false; this.cancelCommonButton.Visible = true; } break; case WizardButtons.OKCancel: { this.nextCommonButton.Text = "OK"; this.cancelCommonButton.Text = "Cancel"; this.backCommonButton.Visible = false; this.cancelCommonButton.Visible = true; this.nextCommonButton.Visible = true; } break; case WizardButtons.NextCancel: { this.nextCommonButton.Text = "Next"; this.cancelCommonButton.Text = "Cancel"; this.backCommonButton.Visible = false; this.cancelCommonButton.Visible = true; this.nextCommonButton.Visible = true; } break; case WizardButtons.BackNextCancel: { this.backCommonButton.Text = "Back"; this.nextCommonButton.Text = "Next"; this.cancelCommonButton.Text = "Cancel"; this.backCommonButton.Visible = true; this.cancelCommonButton.Visible = true; this.nextCommonButton.Visible = true; } break; case WizardButtons.BackFinishCancel: { this.backCommonButton.Text = "Back"; this.nextCommonButton.Text = "Finish"; this.cancelCommonButton.Text = "Cancel"; this.backCommonButton.Visible = true; this.cancelCommonButton.Visible = true; this.nextCommonButton.Visible = true; } break; } } private void RePositionButtons() { this.cancelCommonButton.Location = new Point(this.ClientRectangle.Width - this.cancelCommonButton.Width - 10, this.ClientRectangle.Height - this.cancelCommonButton.Height - 10); this.nextCommonButton.Location = new Point(this.cancelCommonButton.Left - this.nextCommonButton.Width - 15, this.ClientRectangle.Height - this.nextCommonButton.Height - 10); this.backCommonButton.Location = new Point(this.nextCommonButton.Left - this.backCommonButton.Width - 15, this.ClientRectangle.Height - this.backCommonButton.Height - 10); this.cancelCommonButton.Visible = true; this.backCommonButton.Visible = true; this.nextCommonButton.Visible = true; } protected override void OnSizeChanged(EventArgs e) { base.OnSizeChanged(e); if (this.cancelCommonButton == null) return; this.ChangeFormButtons(this.formButtons); } private void backCommonButton_Click(object sender, EventArgs e) { if (!this.OnBack()) this.DialogResult = DialogResult.None; } private void nextCommonButton_Click(object sender, EventArgs e) { if (this.WizardButton == WizardButtons.BackFinishCancel) { if (!this.OnFinish()) this.DialogResult = DialogResult.None; } else { if (!this.OnNext()) this.DialogResult = DialogResult.None; } } private void cancelCommonButton_Click(object sender, EventArgs e) { if (!this.OnCancel()) this.DialogResult = DialogResult.None; } protected virtual bool OnBack() { return true; } protected virtual bool OnNext() { return true; } protected virtual bool OnFinish() { return true; } protected virtual bool OnCancel() { return true; } } }
2, WizardController used to contol which wizard form to display
You can create new wizard control class from this class and control your wizard step yourself by override NextStep and PreStep.
WizardControlusing System; using System.Collections.Generic; using System.ComponentModel; using System.Diagnostics; using System.Linq; using System.Text; using System.Windows.Forms; using System.Reflection; namespace WindowsFormsApplication1 { public class WizardController : Component { private List<BaseWizardForm> formList = new List<BaseWizardForm>(); protected int currentStep = 0; private Timer delayShowDialogTimer; protected IWin32Window parentWindow = null; #region Constructor and Destructor public WizardController() : base() { } #endregion #region IDisposable Members protected override void Dispose(bool disposing) { if (disposing) { foreach (BaseWizardForm form in formList) { if (!form.IsDisposed) form.Dispose(); } } base.Dispose(disposing); } #endregion #region Methods public void AddForm(BaseWizardForm form) { this.formList.Add(form); } public void AddForm(Type formType) { Assembly assembly = Assembly.GetCallingAssembly(); if (assembly == null) return; BaseWizardForm form = assembly.CreateInstance(formType.FullName) as BaseWizardForm; this.AddForm(form); } public void RemoveForm(BaseWizardForm form) { this.formList.Remove(form); } public DialogResult ShowDialog(IWin32Window parent) { this.parentWindow = parent; return this.ShowDialog(); } protected virtual DialogResult ShowDialog() { DialogResult ret = this.formList[this.currentStep].ShowDialog(this.parentWindow); if (ret == DialogResult.OK) { if (this.NextStep()) return this.ShowDialog(); } else if (ret == DialogResult.Retry) { if (this.PreStep()) return this.ShowDialog(); } else { return DialogResult.Cancel; } return ret; } protected virtual bool NextStep() { if (this.currentStep == this.formList.Count - 1) return false; this.currentStep++; return true; } protected virtual bool PreStep() { if (this.currentStep == 0) return false; this.currentStep--; return true; } #endregion } }
3 How to use.
A, Create new forms derived from BaseWizardForm and set the forms' WizardButton Property.
B, Create a new class derived from WizardController and add the forms to the controller.
public TestWizardController()
{
this.AddForm(typeof(Form1));
this.AddForm(typeof(Form2));
}
C, Show the wizard
TestWizardController test = new TestWizardController()
test.ShowDialog(null);