日期:2010-10-09  浏览次数:20448 次

在现实的软件中,经常可以看到一些向导(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(4