在现实的软件中,经常可以看到一些向导(Wizard)的存在,如何给自己的应用程序实现一个向导呢?
下面给出一个使用面向对象的思想设计出来的应用程序向导框架,虽然很简单,但希望能给人帮助。
其中有三个比较关键的类,一个是向导窗体要收集的信息封装成的类Information,一个是所有向导窗体都要继承的窗体基类frmBase,还有一个就是最关键的类,向导控制类WizardController。
有了基类frmBase,设计一个子类窗体非常简单,只需从frmBase类中派生一个新窗体,设计完用户界面之后重写其UpdateInfo()方法即可。
所有代码(VS2003版)如下,通俗易懂,不再做说明:
Information类:
using System;
namespace Wizard
{
/// <summary>
/// Information 的摘要说明。
/// </summary>
public class Information
{
public Information()
{
//
// TODO: 在此处添加构造函数逻辑
//
}
//姓名
public string Name = "";
//性别
public bool IsMale = true;
//学历
public string EduBackground = "";
//编程语言
public string ProgrameLanguage = "";
}
}
frmBase类:
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
namespace Wizard
{
/// <summary>
/// frmBase 的摘要说明。
/// </summary>
public class frmBase : System.Windows.Forms.Form
{
private System.Windows.Forms.Panel panel1;
private System.Windows.Forms.Button btnGoPrev;
private System.Windows.Forms.Button btnGoNext;
private System.Windows.Forms.Button btnOver;
private System.Windows.Forms.Button btnCancel;
private System.Windows.Forms.Button btnHelp;
/// <summary>
/// 必需的设计器变量。
/// </summary>
private System.ComponentModel.Container components = null;
public frmBase()
{
//
// Windows 窗体设计器支持所必需的
//
InitializeComponent();
//
// TODO: 在 InitializeComponent 调用后添加任何构造函数代码
//
}
/// <summary>
/// 清理所有正在使用的资源。
/// </summary>
protected override void Dispose( bool disposing )
{
if( disposing )
{
if(components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
}
#region Windows 窗体设计器生成的代码
/// <summary>
/// 设计器支持所需的方法 - 不要使用代码编辑器修改
/// 此方法的内容。
/// </summary>
private void InitializeComponent()
{
this.panel1 = new System.Windows.Forms.Panel();
this.btnGoPrev = new System.Windows.Forms.Button();
this.btnGoNext = new System.Windows.Forms.Button();
this.btnOver = new System.Windows.Forms.Button();
this.btnCancel = new System.Windows.Forms.Button();
this.btnHelp = new System.Windows.Forms.Button();
this.panel1.SuspendLayout();
this.SuspendLayout();
//
// panel1
//
this.panel1.Anchor = ((System.Windows.Forms.AnchorStyles)(((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Left)
| System.Windows.Forms.AnchorStyles.Right)));
this.panel1.Controls.Add(this.btnHelp);
this.panel1.Controls.Add(this.btnCancel);
this.panel1.Controls.Add(this.btnOver);
this.panel1.Controls.Add(this.btnGoNext);
this.panel1.Controls.Add(this.btnGoPrev);
this.panel1.Location = new System.Drawing.Point(0, 202);
this.panel1.Name = "panel1";
this.panel1.Size = new System.Drawing.Size(450, 40);
this.panel1.TabIndex = 0;
//
// btnGoPrev
//
this.btnGoPrev.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Left)));
this.btnGoPrev.Location = new System.Drawing.Point(25, 8);
this.btnGoPrev.Name = "btnGoPrev";
this.btnGoPrev.Size = new System.Drawing.Size(56, 23);
this.btnGoPrev.TabIndex = 1;
this.btnGoPrev.Text = "上一步";
this.btnGoPrev.Click += new System.EventHandler(this.btnGoPrev_Click);
//
// btnGoNext
//
this.btnGoNext.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Left)));
this.btnGoNext.Location = new System.Drawing.Point(105, 8);
this.btnGoNext.Name = "btnGoNext";
this.btnGoNext.Size = new System.Drawing.Size(56, 23);
this.btnGoNext.TabIndex = 2;
this.btnGoNext.Text = "下一步";
this.btnGoNext.Click += new System.EventHandler(this.btnGoNext_Click);
//
// btnOver
//
this.btnOver.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Left)));
this.btnOver.Location = new System.Drawing.Point(193, 8);
this.btnOver.Name = "btnOver";
this.btnOver.Size = new System.Drawing.Size(56, 23);
this.btnOver.TabIndex = 3;
this.btnOver.Text = "完成";
this.btnOver.Click += new System.EventHandler(this.btnOver_Click);
//
// btnCancel
//
this.btnCancel.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Left)));
this.btnCancel.Location = new System.Drawing.Point(281, 8);
this.btnCancel.Name = "btnCancel";
this.btnCancel.Size = new System.Drawing.Size(56, 23);
this.btnCancel.TabIndex = 4;
this.btnCancel.Text = "取消";
this.btnCancel.Click += new System.EventHandler(this.btnCancel_Click);
//
// btnHelp
//
this.btnHelp.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Left)));
this.btnHelp.Location = new System.Drawing.Point(369, 8);
this.btnHelp.Name = "btnHelp";
this.btnHelp.Size = new System.Drawing.Size(56, 23);
this.btnHelp.TabIndex = 5;
this.btnHelp.Text = "帮助";
//
// frmBase
//
this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
this.ClientSize = new System.Drawing.Size(450, 239);
this.Controls.Add(this.panel1);
this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.FixedSingle;
this.Name = "frmBase";
this.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen;
this.panel1.ResumeLayout(false);
this.ResumeLayout(false);
}
#endregion
public WizardController controller = null;
public void DisableButton()
{
if(this.controller == null)
return;
if(this.controller.IsFirstForm)
{
this.btnGoPrev.Enabled = false;
}
else
{
this.btnGoPrev.Enabled = true;
}
if(this.controller.IsL