在现实的软件中,经常可以看到一些向导(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.islastform)
{
this.btngonext.enabled = false;
}
else
{
this.btngonext.enabled = true;
}
}
protected virtual void updateinfo()
{
}
protected virtual void gonext()
{
updateinfo();
controller.gonext();
}
protected virtual void goprev()
{
updateinfo();
controller.goprev();
}
protected virtual void finish()
{
updateinfo();
controller.finishwizard();
this.visible = false;
}
protected virtual void cancel()
{
this.controller.info = null;
this.close();
}
private void btngoprev_click(object sender, system.eventargs e)
{
goprev();
}
private void btngonext_click(object sender, system.eventargs e)
{
gonext();
}
private void btnover_click(object sender, system.eventargs e)
{
finish();
}
private void btncancel_click(object sender, system.eventargs e)
{
cancel();
}
}
}
向导控制器wizardcontroller类:
using system;
using system.collections;
namespace wizard
{
/// <summary>
/// wizardcontroller 的摘要说明。
/// </summary>
public class wizardcontroller
{
public wizardcontroller()
{
//
// todo: 在此处添加构造函数逻辑
//
wizardforms.add(new frmstep1());
wizardforms.add(new frmstep2());
foreach(frmbase frm in wizardforms)
{
frm.controller = this;
frm.disablebutton();
}
}
private arraylist wizardforms = new arraylist();
public information info = new information();
private int curindex = 0;
public bool isfirstform
{
get{ return curindex == 0;}
}
public bool islastform
{
get{return curindex == this.wizardforms.count - 1;}
}
public void gonext()
{
if(curindex+1 < wizardforms.count)
{
((frmbase)wizardforms[curindex]).visible = false;
curindex++;
}
else
{
return;
}
((frmbase)wizardforms[curindex]).show();
((frmbase)wizardforms[curindex]).disablebutton();
}
public void goprev()
{
if(curindex-1 >= 0)
{
((frmbase)wizardforms[curindex]).visible = false;
curindex--;
}
else
{
return;
}
((frmbase)wizardforms[curindex]).show();
((frmbase)wizardforms[curindex]).disablebutton();
}
public void beginwizard()
{
((frmbase)wizardforms[0]).show();
((frmbase)wizardforms[curindex]).disablebutton();
}
public void finishwizard()
{
curindex = 0;
dispose();
}
private void dispose()
{
foreach(frmbase frm in wizardforms)
{
frm.close();
}
}
}
}
第一个子窗体:
using system;
using system.collections;
using system.componentmodel;
using system.drawing;
using system.windows.forms;
namespace wizard
{
public class frmstep1 : wizard.frmbase
{
private system.windows.forms.textbox txtname;
private system.windows.forms.radiobutton rdomale;
private system.windows.forms.radiobutton radiobutton1;
private system.windows.forms.label label1;
private system.windows.forms.label label2;
private system.componentmodel.icontainer components = null;
public frmstep1()
{
// 该调用是 windows 窗体设计器所必需的。
initializecomponent();
// todo: 在 initializecomponent 调用后添加任何初始化
}
/// <summary>
/// 清理所有正在使用的资源。
/// </summary>
protected override void dispose( bool disposing )
{
if( disposing )
{
if (components != null)
{
components.dispose();
}
}
base.dispose( disposing );
}
#region 设计器生成的代码
/// <summary>
/// 设计器支持所需的方法 - 不要使用代码编辑器修改
/// 此方法的内容。
/// </summary>
private void initializecomponent()
{
this.txtname = new system.windows.forms.textbox();
this.rdomale = new system.windows.forms.radiobutton();
this.radiobutton1 = new system.windows.forms.radiobutton();
this.label1 = new system.windows.forms.label();
this.label2 = new system.windows.forms.label();
this.suspendlayout();
//
// txtname
//
this.txtname.location = new system.drawing.point(173, 61);
this.txtname.name = "txtname";
this.txtname.size = new system.drawing.size(152, 21);
this.txtname.tabindex = 1;
this.txtname.text = "";
//
// rdomale
//
this.rdomale.checked = true;
this.rdomale.location = new system.drawing.point(205, 115);
this.rdomale.name = "rdomale";
this.rdomale.size = new system.drawing.size(40, 24);
this.rdomale.tabindex = 2;
this.rdomale.tabstop = true;
this.rdomale.text = "男";
//
// radiobutton1
//
this.radiobutton1.location = new system.drawing.point(253, 115);
this.radiobutton1.name = "radiobutton1";
this.radiobutton1.size = new system.drawing.size(32, 24);
this.radiobutton1.tabindex = 3;
this.radiobutton1.text = "女";
//
// label1
//
this.label1.location = new system.drawing.point(125, 64);
this.label1.name = "label1";
this.label1.size = new system.drawing.size(48, 23);
this.label1.tabindex = 4;
this.label1.text = "姓名";
//
// label2
//
this.label2.location = new system.drawing.point(0, 0);
this.label2.name = "label2";
this.label2.tabindex = 5;
//
// frmstep1
//
this.autoscalebasesize = new system.drawing.size(6, 14);
this.clientsize = new system.drawing.size(450, 239);
this.controls.add(this.label1);
this.controls.add(this.radiobutton1);
this.controls.add(this.rdomale);
this.controls.add(this.txtname);
this.controls.add(this.label2);
this.name = "frmstep1";
this.controls.setchildindex(this.label2, 0);
this.controls.setchildindex(this.txtname, 0);
this.controls.setchildindex(this.rdomale, 0);
this.controls.setchildindex(this.radiobutton1, 0);
this.controls.setchildindex(this.label1, 0);
this.resumelayout(false);
}
#endregion
protected override void updateinfo()
{
this.controller.info.name = txtname.text.trim();
this.controller.info.ismale = rdomale.checked;
}
}
}
第二个子窗体:
using system;
using system.collections;
using system.componentmodel;
using system.drawing;
using system.windows.forms;
namespace wizard
{
public class frmstep2 : wizard.frmbase
{
private system.windows.forms.label label1;
private system.windows.forms.label label2;
private system.windows.forms.combobox cbbedubackground;
private system.windows.forms.checkbox checkbox1;
private system.windows.forms.checkbox checkbox2;
private system.windows.forms.checkbox checkbox3;
private system.windows.forms.checkbox checkbox4;
private system.componentmodel.icontainer components = null;
public frmstep2()
{
// 该调用是 windows 窗体设计器所必需的。
initializecomponent();
// todo: 在 initializecomponent 调用后添加任何初始化
}
/// <summary>
/// 清理所有正在使用的资源。
/// </summary>
protected override void dispose( bool disposing )
{
if( disposing )
{
if (components != null)
{
components.dispose();
}
}
base.dispose( disposing );
}
#region 设计器生成的代码
/// <summary>
/// 设计器支持所需的方法 - 不要使用代码编辑器修改
/// 此方法的内容。
/// </summary>
private void initializecomponent()
{
this.label1 = new system.windows.forms.label();
this.label2 = new system.windows.forms.label();
this.cbbedubackground = new system.windows.forms.combobox();
this.checkbox1 = new system.windows.forms.checkbox();
this.checkbox2 = new system.windows.forms.checkbox();
this.checkbox3 = new system.windows.forms.checkbox();
this.checkbox4 = new system.windows.forms.checkbox();
this.suspendlayout();
//
// label1
//
this.label1.location = new system.drawing.point(98, 72);
this.label1.name = "label1";
this.label1.size = new system.drawing.size(40, 23);
this.label1.tabindex = 3;
this.label1.text = "学历";
//
// label2
//
this.label2.location = new system.drawing.point(98, 128);
this.label2.name = "label2";
this.label2.size = new system.drawing.size(64, 23);
this.label2.tabindex = 4;
this.label2.text = "编程语言";
//
// cbbedubackground
//
this.cbbedubackground.dropdownstyle = system.windows.forms.comboboxstyle.dropdownlist;
this.cbbedubackground.items.addrange(new object[] {
"本科",
"硕士",
"博士"});
this.cbbedubackground.location = new system.drawing.point(170, 68);
this.cbbedubackground.name = "cbbedubackground";
this.cbbedubackground.size = new system.drawing.size(152, 20);
this.cbbedubackground.tabindex = 5;
//
// checkbox1
//
this.checkbox1.location = new system.drawing.point(166, 123);
this.checkbox1.name = "checkbox1";
this.checkbox1.size = new system.drawing.size(48, 24);
this.checkbox1.tabindex = 6;
this.checkbox1.text = "c++";
//
// checkbox2
//
this.checkbox2.location = new system.drawing.point(211, 123);
this.checkbox2.name = "checkbox2";
this.checkbox2.size = new system.drawing.size(48, 24);
this.checkbox2.tabindex = 7;
this.checkbox2.text = "java";
//
// checkbox3
//
this.checkbox3.location = new system.drawing.point(267, 123);
this.checkbox3.name = "checkbox3";
this.checkbox3.size = new system.drawing.size(40, 24);
this.checkbox3.tabindex = 8;
this.checkbox3.text = "vb";
//
// checkbox4
//
this.checkbox4.location = new system.drawing.point(312, 123);
this.checkbox4.name = "checkbox4";
this.checkbox4.size = new system.drawing.size(40, 24);
this.checkbox4.tabindex = 9;
this.checkbox4.text = "c#";
//
// frmstep2
//
this.autoscalebasesize = new system.drawing.size(6, 14);
this.clientsize = new system.drawing.size(450, 239);
this.controls.add(this.checkbox4);
this.controls.add(this.checkbox3);
this.controls.add(this.checkbox2);
this.controls.add(this.checkbox1);
this.controls.add(this.cbbedubackground);
this.controls.add(this.label2);
this.controls.add(this.label1);
this.name = "frmstep2";
this.controls.setchildindex(this.label1, 0);
this.controls.setchildindex(this.label2, 0);
this.controls.setchildindex(this.cbbedubackground, 0);
this.controls.setchildindex(this.checkbox1, 0);
this.controls.setchildindex(this.checkbox2, 0);
this.controls.setchildindex(this.checkbox3, 0);
this.controls.setchildindex(this.checkbox4, 0);
this.resumelayout(false);
}
#endregion
protected override void updateinfo()
{
this.controller.info.edubackground = cbbedubackground.getitemtext(cbbedubackground.selecteditem);
string lang = "";
foreach(control ctl in this.controls)
{
if(ctl is checkbox && ((checkbox)ctl).checked)
{
lang += ctl.text + ";";
}
}
this.controller.info.programelanguage = lang;
}
}
}
测试demo:
using system;
using system.drawing;
using system.collections;
using system.componentmodel;
using system.windows.forms;
namespace wizard
{
/// <summary>
/// frmtest 的摘要说明。
/// </summary>
public class frmtest : system.windows.forms.form
{
/// <summary>
/// 必需的设计器变量。
/// </summary>
private system.componentmodel.container components = null;
private system.windows.forms.button button1;
private system.windows.forms.button button2;
private system.windows.forms.label label1;
private system.windows.forms.label label2;
private system.windows.forms.label label3;
private system.windows.forms.label label4;
private wizardcontroller wizard;
public frmtest()
{
//
// 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.button1 = new system.windows.forms.button();
this.button2 = new system.windows.forms.button();
this.label1 = new system.windows.forms.label();
this.label2 = new system.windows.forms.label();
this.label3 = new system.windows.forms.label();
this.label4 = new system.windows.forms.label();
this.suspendlayout();
//
// button1
//
this.button1.location = new system.drawing.point(48, 216);
this.button1.name = "button1";
this.button1.size = new system.drawing.size(104, 23);
this.button1.tabindex = 0;
this.button1.text = "显示向导";
this.button1.click += new system.eventhandler(this.button1_click);
//
// button2
//
this.button2.location = new system.drawing.point(192, 216);
this.button2.name = "button2";
this.button2.size = new system.drawing.size(104, 23);
this.button2.tabindex = 1;
this.button2.text = "显示信息";
this.button2.click += new system.eventhandler(this.button2_click);
//
// label1
//
this.label1.location = new system.drawing.point(72, 32);
this.label1.name = "label1";
this.label1.size = new system.drawing.size(232, 23);
this.label1.tabindex = 2;
//
// label2
//
this.label2.location = new system.drawing.point(72, 80);
this.label2.name = "label2";
this.label2.size = new system.drawing.size(224, 23);
this.label2.tabindex = 3;
//
// label3
//
this.label3.location = new system.drawing.point(72, 120);
this.label3.name = "label3";
this.label3.size = new system.drawing.size(232, 23);
this.label3.tabindex = 4;
//
// label4
//
this.label4.location = new system.drawing.point(72, 160);
this.label4.name = "label4";
this.label4.size = new system.drawing.size(232, 23);
this.label4.tabindex = 5;
//
// frmtest
//
this.autoscalebasesize = new system.drawing.size(6, 14);
this.clientsize = new system.drawing.size(344, 261);
this.controls.add(this.label4);
this.controls.add(this.label3);
this.controls.add(this.label2);
this.controls.add(this.label1);
this.controls.add(this.button2);
this.controls.add(this.button1);
this.name = "frmtest";
this.text = "向导测试";
this.resumelayout(false);
}
#endregion
/// <summary>
/// 应用程序的主入口点。
/// </summary>
[stathread]
static void main()
{
application.run(new frmtest());
}
private void button1_click(object sender, system.eventargs e)
{
this.wizard = new wizardcontroller();
this.wizard.beginwizard();
}using system;
using system.drawing;
using system.collections;
using system.componentmodel;
using system.windows.forms;
namespace wizard
{
/// <summary>
/// frmtest 的摘要说明。
/// </summary>
public class frmtest : system.windows.forms.form
{
/// <summary>
/// 必需的设计器变量。
/// </summary>
private system.componentmodel.container components = null;
private system.windows.forms.button button1;
private system.windows.forms.button button2;
private system.windows.forms.label label1;
private system.windows.forms.label label2;
private system.windows.forms.label label3;
private system.windows.forms.label label4;
private wizardcontroller wizard;
public frmtest()
{
//
// 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.button1 = new system.windows.forms.button();
this.button2 = new system.windows.forms.button();
this.label1 = new system.windows.forms.label();
this.label2 = new system.windows.forms.label();
this.label3 = new system.windows.forms.label();
this.label4 = new system.windows.forms.label();
this.suspendlayout();
//
// button1
//
this.button1.location = new system.drawing.point(48, 216);
this.button1.name = "button1";
this.button1.size = new system.drawing.size(104, 23);
this.button1.tabindex = 0;
this.button1.text = "显示向导";
this.button1.click += new system.eventhandler(this.button1_click);
//
// button2
//
this.button2.location = new system.drawing.point(192, 216);
this.button2.name = "button2";
this.button2.size = new system.drawing.size(104, 23);
this.button2.tabindex = 1;
this.button2.text = "显示信息";
this.button2.click += new system.eventhandler(this.button2_click);
//
// label1
//
this.label1.location = new system.drawing.point(72, 32);
this.label1.name = "label1";
this.label1.size = new system.drawing.size(232, 23);
this.label1.tabindex = 2;
//
// label2
//
this.label2.location = new system.drawing.point(72, 80);
this.label2.name = "label2";
this.label2.size = new system.drawing.size(224, 23);
this.label2.tabindex = 3;
//
// label3
//
this.label3.location = new system.drawing.point(72, 120);
this.label3.name = "label3";
this.label3.size = new system.drawing.size(232, 23);
this.label3.tabindex = 4;
//
// label4
//
this.label4.location = new system.drawing.point(72, 160);
this.label4.name = "label4";
this.label4.size = new system.drawing.size(232, 23);
this.label4.tabindex = 5;
//
// frmtest
//
this.autoscalebasesize = new system.drawing.size(6, 14);
this.clientsize = new system.drawing.size(344, 261);
this.controls.add(this.label4);
this.controls.add(this.label3);
this.controls.add(this.label2);
this.controls.add(this.label1);
this.controls.add(this.button2);
this.controls.add(this.button1);
this.name = "frmtest";
this.text = "向导测试";
this.resumelayout(false);
}
#endregion
/// <summary>
/// 应用程序的主入口点。
/// </summary>
[stathread]
static void main()
{
application.run(new frmtest());
}
private void button1_click(object sender, system.eventargs e)
{
this.wizard = new wizardcontroller();
this.wizard.beginwizard();
}
private void button2_click(object sender, system.eventargs e)
{
if(this.wizard != null && this.wizard.info != null)
{
this.label1.text = this.wizard.info.name;
if(this.wizard.info.ismale)
this.label2.text = "男";
else
this.label2.text = "女";
this.label3.text = this.wizard.info.edubackground;
this.label4.text = this.wizard.info.programelanguage;
}
else
{
messagebox.show("null");
}
}
}
}
private void button2_click(object sender, system.eventargs e)
{
if(this.wizard != null && this.wizard.info != null)
{
this.label1.text = this.wizard.info.name;
if(this.wizard.info.ismale)
this.label2.text = "男";
else
this.label2.text = "女";
this.label3.text = this.wizard.info.edubackground;
this.label4.text = this.wizard.info.programelanguage;
}
else
{
messagebox.show("null");
}
}
}
}
新闻热点
疑难解答