在现实的软件中,经常可以看到一些向导(Wizard)的存在,如何给自己的应用程序实现一个向导呢?
下面给出一个使用面向对象的思想设计出来的应用程序向导框架,虽然很简单,但希望能给人帮助。
其中有三个比较关键的类,一个是向导窗体要收集的信息封装成的类Information,一个是所有向导窗体都要继承的窗体基类frmBase,还有一个就是最关键的类,向导控制类WizardController。
有了基类frmBase,设计一个子类窗体非常简单,只需从frmBase类中派生一个新窗体,设计完用户界面之后重写其UpdateInfo()方法即可。
所有代码(VS2003版)如下,通俗易懂,不再做说明:
Information类:
1
using System;
2
3
namespace Wizard
4

{
5
/**//// <summary>
6
/// Information 的摘要说明。
7
/// </summary>
8
public class Information
9
{
10
public Information()
11
{
12
//
13
// TODO: 在此处添加构造函数逻辑
14
//
15
}
16
17
//姓名
18
public string Name = "";
19
//性别
20
public bool IsMale = true;
21
//学历
22
public string EduBackground = "";
23
//编程语言
24
public string ProgrameLanguage = "";
25
}
26
}
frmBase类:
1
using System;
2
using System.Drawing;
3
using System.Collections;
4
using System.ComponentModel;
5
using System.Windows.Forms;
6
7
namespace Wizard
8

{
9
/**//// <summary>
10
/// frmBase 的摘要说明。
11
/// </summary>
12
public class frmBase : System.Windows.Forms.Form
13
{
14
private System.Windows.Forms.Panel panel1;
15
private System.Windows.Forms.Button btnGoPrev;
16
private System.Windows.Forms.Button btnGoNext;
17
private System.Windows.Forms.Button btnOver;
18
private System.Windows.Forms.Button btnCancel;
19
private System.Windows.Forms.Button btnHelp;
20
/**//// <summary>
21
/// 必需的设计器变量。
22
/// </summary>
23
private System.ComponentModel.Container components = null;
24
25
public frmBase()
26
{
27
//
28
// Windows 窗体设计器支持所必需的
29
//
30
InitializeComponent();
31
32
//
33
// TODO: 在 InitializeComponent 调用后添加任何构造函数代码
34
//
35
}
36
37
/**//// <summary>
38
/// 清理所有正在使用的资源。
39
/// </summary>
40
protected override void Dispose( bool disposing )
41
{
42
if( disposing )
43
{
44
if(components != null)
45
{
46
components.Dispose();
47
}
48
}
49
base.Dispose( disposing );
50
}
51
52
Windows 窗体设计器生成的代码#region Windows 窗体设计器生成的代码
53
/**//// <summary>
54
/// 设计器支持所需的方法 - 不要使用代码编辑器修改
55
/// 此方法的内容。
56
/// </summary>
57
private void InitializeComponent()
58
{
59
this.panel1 = new System.Windows.Forms.Panel();
60
this.btnGoPrev = new System.Windows.Forms.Button();
61
this.btnGoNext = new System.Windows.Forms.Button();
62
this.btnOver = new System.Windows.Forms.Button();
63
this.btnCancel = new System.Windows.Forms.Button();
64
this.btnHelp = new System.Windows.Forms.Button();
65
this.panel1.SuspendLayout();
66
this.SuspendLayout();
67
//
68
// panel1
69
//
70
this.panel1.Anchor = ((System.Windows.Forms.AnchorStyles)(((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Left)
71
| System.Windows.Forms.AnchorStyles.Right)));
72
this.panel1.Controls.Add(this.btnHelp);
73
this.panel1.Controls.Add(this.btnCancel);
74
this.panel1.Controls.Add(this.btnOver);
75
this.panel1.Controls.Add(this.btnGoNext);
76
this.panel1.Controls.Add(this.btnGoPrev);
77
this.panel1.Location = new System.Drawing.Point(0, 202);
78
this.panel1.Name = "panel1";
79
this.panel1.Size = new System.Drawing.Size(450, 40);
80
this.panel1.TabIndex = 0;
81
//
82
// btnGoPrev
83
//
84
this.btnGoPrev.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Left)));
85
this.btnGoPrev.Location = new System.Drawing.Point(25, 8);
86
this.btnGoPrev.Name = "btnGoPrev";
87
this.btnGoPrev.Size = new System.Drawing.Size(56, 23);
88
this.btnGoPrev.TabIndex = 1;
89
this.btnGoPrev.Text = "上一步";
90
this.btnGoPrev.Click += new System.EventHandler(this.btnGoPrev_Click);
91
//
92
// btnGoNext
93
//
94
this.btnGoNext.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Left)));
95
this.btnGoNext.Location = new System.Drawing.Point(105, 8);
96
this.btnGoNext.Name = "btnGoNext";
97
this.btnGoNext.Size = new System.Drawing.Size(56, 23);
98
this.btnGoNext.TabIndex = 2;
99
this.btnGoNext.Text = "下一步";
100
this.btnGoNext.Click += new System.EventHandler(this.btnGoNext_Click);
101
//
102
// btnOver
103
//
104
this.btnOver.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Left)));
105
this.btnOver.Location = new System.Drawing.Point(193, 8);
106
this.btnOver.Name = "btnOver";
107
this.btnOver.Size = new System.Drawing.Size(56, 23);
108
this.btnOver.TabIndex = 3;
109
this.btnOver.Text = "完成";
110
this.btnOver.Click += new System.EventHandler(this.btnOver_Click);
111
//
112
// btnCancel
113
//
114
this.btnCancel.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Left)));
115
this.btnCancel.Location = new System.Drawing.Point(281, 8);
116
this.btnCancel.Name = "btnCancel";
117
this.btnCancel.Size = new System.Drawing.Size(56, 23);
118
this.btnCancel.TabIndex = 4;
119
this.btnCancel.Text = "取消";
120
this.btnCancel.Click += new System.EventHandler(this.btnCancel_Click);
121
//
122
// btnHelp
123
//
124
this.btnHelp.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Left)));
125
this.btnHelp.Location = new System.Drawing.Point(369, 8);
126
this.btnHelp.Name = "btnHelp";
127
this.btnHelp.Size = new System.Drawing.Size(56, 23);
128
this.btnHelp.TabIndex = 5;
129
this.btnHelp.Text = "帮助";
130
//
131
// frmBase
132
//
133
this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
134
this.ClientSize = new System.Drawing.Size(450, 239);
135
this.Controls.Add(this.panel1);
136
this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.FixedSingle;
137
this.Name = "frmBase";
138
this.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen;
139
this.panel1.ResumeLayout(false);
140
this.ResumeLayout(false);
141
142
}
143
#endregion
144
145
public WizardController controller = null;
146
147
public void DisableButton()
148
{
149
if(this.controller == null)
150
return;
151
if(this.controller.IsFirstForm)
152
{
153
this.btnGoPrev.Enabled = false;
154
}
155
else
156
{
157
this.btnGoPrev.Enabled = true;
158
}
159
if(this.controller.IsLastForm)
160
{
161
this.btnGoNext.Enabled = false;
162
}
163
else
164
{
165
this.btnGoNext.Enabled = true;
166
}
167
}
168
protected virtual void UpdateInfo()
169
{
170
171
}
172
protected virtual void GoNext()
173
{
174
UpdateInfo();
175
controller.GoNext();
176
}
177
protected virtual void GoPrev()
178
{
179
UpdateInfo();
180
controller.GoPrev();
181
}
182
protected virtual void Finish()
183
{
184
UpdateInfo();
185
controller.FinishWizard();
186
this.Visible = false;
187
}
188
protected virtual void Cancel()
189
{
190
this.controller.info = null;
191
this.Close();
192
}
193
194
private void btnGoPrev_Click(object sender, System.EventArgs e)
195
{
196
GoPrev();
197
}
198
199
private void btnGoNext_Click(object sender, System.EventArgs e)
200
{
201
GoNext();
202
}
203
204
private void btnOver_Click(object sender, System.EventArgs e)
205
{
206
Finish();
207
}
208
209
private void btnCancel_Click(object sender, System.EventArgs e)
210
{
211
Cancel();
212
}
213
}
214
}
向导控制器WizardController类:
1
using System;
2
using System.Collections;
3
4
namespace Wizard
5

{
6
/**//// <summary>
7
/// WizardController 的摘要说明。
8
/// </summary>
9
public class WizardController
10
{
11
public WizardController()
12
{
13
//
14
// TODO: 在此处添加构造函数逻辑
15
//
16
WizardForms.Add(new frmStep1());
17
WizardForms.Add(new frmStep2());
18
foreach(frmBase frm in WizardForms)
19
{
20
frm.controller = this;
21
frm.DisableButton();
22
}
23
}
24
private ArrayList WizardForms = new ArrayList();
25
public Information info = new Information();
26
private int curIndex = 0;
27
28
public bool IsFirstForm
29
{
30
get
{ return curIndex == 0;}
31
}
32
public bool IsLastForm
33
{
34
get
{return curIndex == this.WizardForms.Count - 1;}
35
}
36
public void GoNext()
37
{
38
if(curIndex+1 < WizardForms.Count)
39
{
40
((frmBase)WizardForms[curIndex]).Visible = false;
41
curIndex++;
42
}
43
else
44