C#设计的一个向导程序(Wizard)框架
在现实的软件中,经常可以看到一些向导(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
}
using System;2

3
namespace Wizard4
{5
/// <summary>6
/// Information 的摘要说明。7
/// </summary>8
public class Information9
{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 窗体设计器生成的代码
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
}
using System;2
using System.Drawing;3
using System.Collections;4
using System.ComponentModel;5
using System.Windows.Forms;6

7
namespace Wizard8
{9
/// <summary>10
/// frmBase 的摘要说明。11
/// </summary>12
public class frmBase : System.Windows.Forms.Form13
{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 窗体设计器生成的代码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
else156
{157
this.btnGoPrev.Enabled = true;158
}159
if(this.controller.IsLastForm)160
{161
this.btnGoNext.Enabled = false;162
}163
else164
{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
{
45
return;
46
}
47
((frmBase)WizardForms[curIndex]).Show();
48
((frmBase)WizardForms[curIndex]).DisableButton();
49
}
50
public void GoPrev()
51
{
52
if(curIndex-1 >= 0)
53
{
54
((frmBase)WizardForms[curIndex]).Visible = false;
55
curIndex--;
56
}
57
else
58
{
59
return;
60
}
61
((frmBase)WizardForms[curIndex]).Show();
62
((frmBase)WizardForms[curIndex]).DisableButton();
63
}
64
public void BeginWizard()
65
{
66
((frmBase)WizardForms[0]).Show();
67
((frmBase)WizardForms[curIndex]).DisableButton();
68
}
69
public void FinishWizard()
70
{
71
curIndex = 0;
72
Dispose();
73
}
74
75
private void Dispose()
76
{
77
foreach(frmBase frm in WizardForms)
78
{
79
frm.Close();
80
}
81
}
82
}
83
}
using System;2
using System.Collections;3

4
namespace Wizard5
{6
/// <summary>7
/// WizardController 的摘要说明。8
/// </summary>9
public class WizardController10
{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 IsFirstForm29
{30
get{ return curIndex == 0;} 31
}32
public bool IsLastForm33
{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
else44
{45
return;46
}47
((frmBase)WizardForms[curIndex]).Show();48
((frmBase)WizardForms[curIndex]).DisableButton();49
}50
public void GoPrev()51
{52
if(curIndex-1 >= 0)53
{54
((frmBase)WizardForms[curIndex]).Visible = false;55
curIndex--;56
}57
else58
{59
return;60
}61
((frmBase)WizardForms[curIndex]).Show();62
((frmBase)WizardForms[curIndex]).DisableButton();63
}64
public void BeginWizard()65
{66
((frmBase)WizardForms[0]).Show();67
((frmBase)WizardForms[curIndex]).DisableButton();68
}69
public void FinishWizard()70
{71
curIndex = 0;72
Dispose();73
}74

75
private void Dispose()76
{77
foreach(frmBase frm in WizardForms)78
{79
frm.Close();80
}81
}82
}83
}第一个子窗体:
1
using System;
2
using System.Collections;
3
using System.ComponentModel;
4
using System.Drawing;
5
using System.Windows.Forms;
6
7
namespace Wizard
8
{
9
public class frmStep1 : Wizard.frmBase
10
{
11
private System.Windows.Forms.TextBox txtName;
12
private System.Windows.Forms.RadioButton rdoMale;
13
private System.Windows.Forms.RadioButton radioButton1;
14
private System.Windows.Forms.Label label1;
15
private System.Windows.Forms.Label label2;
16
private System.ComponentModel.IContainer components = null;
17
18
public frmStep1()
19
{
20
// 该调用是 Windows 窗体设计器所必需的。
21
InitializeComponent();
22
23
// TODO: 在 InitializeComponent 调用后添加任何初始化
24
}
25
26
/// <summary>
27
/// 清理所有正在使用的资源。
28
/// </summary>
29
protected override void Dispose( bool disposing )
30
{
31
if( disposing )
32
{
33
if (components != null)
34
{
35
components.Dispose();
36
}
37
}
38
base.Dispose( disposing );
39
}
40
41
设计器生成的代码
114
115
protected override void UpdateInfo()
116
{
117
this.controller.info.Name = txtName.Text.Trim();
118
this.controller.info.IsMale = rdoMale.Checked;
119
}
120
121
}
122
}
using System;2
using System.Collections;3
using System.ComponentModel;4
using System.Drawing;5
using System.Windows.Forms;6

7
namespace Wizard8
{9
public class frmStep1 : Wizard.frmBase10
{11
private System.Windows.Forms.TextBox txtName;12
private System.Windows.Forms.RadioButton rdoMale;13
private System.Windows.Forms.RadioButton radioButton1;14
private System.Windows.Forms.Label label1;15
private System.Windows.Forms.Label label2;16
private System.ComponentModel.IContainer components = null;17

18
public frmStep1()19
{20
// 该调用是 Windows 窗体设计器所必需的。21
InitializeComponent();22

23
// TODO: 在 InitializeComponent 调用后添加任何初始化24
}25

26
/// <summary>27
/// 清理所有正在使用的资源。28
/// </summary>29
protected override void Dispose( bool disposing )30
{31
if( disposing )32
{33
if (components != null) 34
{35
components.Dispose();36
}37
}38
base.Dispose( disposing );39
}40

41
设计器生成的代码114

115
protected override void UpdateInfo()116
{117
this.controller.info.Name = txtName.Text.Trim();118
this.controller.info.IsMale = rdoMale.Checked;119
}120

121
}122
}第二个子窗体:
1
using System;
2
using System.Collections;
3
using System.ComponentModel;
4
using System.Drawing;
5
using System.Windows.Forms;
6
7
namespace Wizard
8
{
9
public class frmStep2 : Wizard.frmBase
10
{
11
private System.Windows.Forms.Label label1;
12
private System.Windows.Forms.Label label2;
13
private System.Windows.Forms.ComboBox cbbEduBackground;
14
private System.Windows.Forms.CheckBox checkBox1;
15
private System.Windows.Forms.CheckBox checkBox2;
16
private System.Windows.Forms.CheckBox checkBox3;
17
private System.Windows.Forms.CheckBox checkBox4;
18
private System.ComponentModel.IContainer components = null;
19
20
public frmStep2()
21
{
22
// 该调用是 Windows 窗体设计器所必需的。
23
InitializeComponent();
24
25
// TODO: 在 InitializeComponent 调用后添加任何初始化
26
}
27
28
/// <summary>
29
/// 清理所有正在使用的资源。
30
/// </summary>
31
protected override void Dispose( bool disposing )
32
{
33
if( disposing )
34
{
35
if (components != null)
36
{
37
components.Dispose();
38
}
39
}
40
base.Dispose( disposing );
41
}
42
43
设计器生成的代码
142
143
protected override void UpdateInfo()
144
{
145
this.controller.info.EduBackground = cbbEduBackground.GetItemText(cbbEduBackground.SelectedItem);
146
string lang = "";
147
foreach(Control ctl in this.Controls)
148
{
149
if(ctl is CheckBox && ((CheckBox)ctl).Checked)
150
{
151
lang += ctl.Text + ";";
152
}
153
}
154
this.controller.info.ProgrameLanguage = lang;
155
}
156
157
}
158
}
using System;2
using System.Collections;3
using System.ComponentModel;4
using System.Drawing;5
using System.Windows.Forms;6

7
namespace Wizard8
{9
public class frmStep2 : Wizard.frmBase10
{11
private System.Windows.Forms.Label label1;12
private System.Windows.Forms.Label label2;13
private System.Windows.Forms.ComboBox cbbEduBackground;14
private System.Windows.Forms.CheckBox checkBox1;15
private System.Windows.Forms.CheckBox checkBox2;16
private System.Windows.Forms.CheckBox checkBox3;17
private System.Windows.Forms.CheckBox checkBox4;18
private System.ComponentModel.IContainer components = null;19

20
public frmStep2()21
{22
// 该调用是 Windows 窗体设计器所必需的。23
InitializeComponent();24

25
// TODO: 在 InitializeComponent 调用后添加任何初始化26
}27

28
/// <summary>29
/// 清理所有正在使用的资源。30
/// </summary>31
protected override void Dispose( bool disposing )32
{33
if( disposing )34
{35
if (components != null) 36
{37
components.Dispose();38
}39
}40
base.Dispose( disposing );41
}42

43
设计器生成的代码142

143
protected override void UpdateInfo()144
{145
this.controller.info.EduBackground = cbbEduBackground.GetItemText(cbbEduBackground.SelectedItem);146
string lang = "";147
foreach(Control ctl in this.Controls)148
{149
if(ctl is CheckBox && ((CheckBox)ctl).Checked)150
{151
lang += ctl.Text + ";";152
}153
}154
this.controller.info.ProgrameLanguage = lang;155
}156

157
}158
}测试Demo:
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
/// frmTest 的摘要说明。
11
/// </summary>
12
public class frmTest : System.Windows.Forms.Form
13
{
14
/// <summary>
15
/// 必需的设计器变量。
16
/// </summary>
17
private System.ComponentModel.Container components = null;
18
private System.Windows.Forms.Button button1;
19
private System.Windows.Forms.Button button2;
20
private System.Windows.Forms.Label label1;
21
private System.Windows.Forms.Label label2;
22
private System.Windows.Forms.Label label3;
23
private System.Windows.Forms.Label label4;
24
private WizardController wizard;
25
26
public frmTest()
27
{
28
//
29
// Windows 窗体设计器支持所必需的
30
//
31
InitializeComponent();
32
33
//
34
// TODO: 在 InitializeComponent 调用后添加任何构造函数代码
35
//
36
}
37
38
/// <summary>
39
/// 清理所有正在使用的资源。
40
/// </summary>
41
protected override void Dispose( bool disposing )
42
{
43
if( disposing )
44
{
45
if(components != null)
46
{
47
components.Dispose();
48
}
49
}
50
base.Dispose( disposing );
51
}
52
53
Windows 窗体设计器生成的代码
130
/// <summary>
131
/// 应用程序的主入口点。
132
/// </summary>
133
[STAThread]
134
static void Main()
135
{
136
Application.Run(new frmTest());
137
}
138
139
private void button1_Click(object sender, System.EventArgs e)
140
{
141
this.wizard = new WizardController();
142
this.wizard.BeginWizard();
143
}
144
145
private void button2_Click(object sender, System.EventArgs e)
146
{
147
if(this.wizard != null && this.wizard.info != null)
148
{
149
this.label1.Text = this.wizard.info.Name;
150
if(this.wizard.info.IsMale)
151
this.label2.Text = "男";
152
else
153
this.label2.Text = "女";
154
this.label3.Text = this.wizard.info.EduBackground;
155
this.label4.Text = this.wizard.info.ProgrameLanguage;
156
}
157
else
158
{
159
MessageBox.Show("NULL");
160
}
161
}
162
}
163
}
using System;2
using System.Drawing;3
using System.Collections;4
using System.ComponentModel;5
using System.Windows.Forms;6

7
namespace Wizard8
{9
/// <summary>10
/// frmTest 的摘要说明。11
/// </summary>12
public class frmTest : System.Windows.Forms.Form13
{14
/// <summary>15
/// 必需的设计器变量。16
/// </summary>17
private System.ComponentModel.Container components = null;18
private System.Windows.Forms.Button button1;19
private System.Windows.Forms.Button button2;20
private System.Windows.Forms.Label label1;21
private System.Windows.Forms.Label label2;22
private System.Windows.Forms.Label label3;23
private System.Windows.Forms.Label label4;24
private WizardController wizard;25

26
public frmTest()27
{28
//29
// Windows 窗体设计器支持所必需的30
//31
InitializeComponent();32

33
//34
// TODO: 在 InitializeComponent 调用后添加任何构造函数代码35
//36
}37

38
/// <summary>39
/// 清理所有正在使用的资源。40
/// </summary>41
protected override void Dispose( bool disposing )42
{43
if( disposing )44
{45
if(components != null)46
{47
components.Dispose();48
}49
}50
base.Dispose( disposing );51
}52

53
Windows 窗体设计器生成的代码130
/// <summary>131
/// 应用程序的主入口点。132
/// </summary>133
[STAThread]134
static void Main()135
{136
Application.Run(new frmTest());137
}138

139
private void button1_Click(object sender, System.EventArgs e)140
{141
this.wizard = new WizardController();142
this.wizard.BeginWizard();143
}144

145
private void button2_Click(object sender, System.EventArgs e)146
{147
if(this.wizard != null && this.wizard.info != null)148
{149
this.label1.Text = this.wizard.info.Name;150
if(this.wizard.info.IsMale)151
this.label2.Text = "男";152
else153
this.label2.Text = "女";154
this.label3.Text = this.wizard.info.EduBackground;155
this.label4.Text = this.wizard.info.ProgrameLanguage;156
}157
else158
{159
MessageBox.Show("NULL");160
}161
}162
}163
}


浙公网安备 33010602011771号