-
c#设计一个向导框架wizard框架
在现实的软件中,经常可以看到一些向导(Wizard)的存在,如何给自己的应用程序实现一个向导呢?下面给出一个使用面向对象的思想设计出来的应用程序向导框架,虽然很简单,但希望能给人帮助。
其中有三个比较关键的类,一个是向导窗体要收集的信息封装成的类Information,一个是所有向导窗体都要继承的窗体基类frmBase,还有一个就是最关键的类,向导控制类Controller。
有了基类frmBase,设计一个子类窗体非常简单,只需从frmBase类中派生一个新窗体,设计完用户界面之后重写其UpdateInfo()方法即可。
所有代码(VS2008版)如下,通俗易懂,不再做说明:
(注:C#中,事件中尽量只调用方法,逻辑的实现都放到方法中实现,尽量不要在事件中写太多的代码。)
解决方案的结构:
Information类:
Code
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace Wizard
{
public class Information
{
public Information()
{
}
public string Name = "";
public Boolean IsMale = true;
public string ProgrameLanguage = "";
}
}
frmBase类:
Code
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace Wizard
{
public partial class frmBase : Form
{
public frmBase()
{
InitializeComponent();
}
public Controller controller = null;
public void DisableButton()
{
if (controller==null)
return;
if (this.controller.IsFirstForm)
btnGoPrev.Enabled = false;
else
btnGoPrev.Enabled = true;
if (this.controller.IsLastForm)
btnGoNext.Enabled = false;
else
btnGoNext.Enabled = true;
}
protected virtual void UpdateInfor()
{
}
protected virtual void GoPrev()
{
UpdateInfor();
controller.GoPrev();
}
protected virtual void GoNext()
{
UpdateInfor();
controller.GoNext();
}
protected virtual void Finish()
{
UpdateInfor();
controller.Finish();
this.Visible = false;
}
protected virtual void Cancel()
{
this.controller.info = null;
this.Close();
}
private void btnGoPrev_Click(object sender, EventArgs e)
{
this.GoPrev();
}
private void btnGoNext_Click(object sender, EventArgs e)
{
this.GoNext();
}
private void btnFinish_Click(object sender, EventArgs e)
{
this.Finish();
}
private void btnCancel_Click(object sender, EventArgs e)
{
this.Cancel();
}
}
}
向导控制器Controller类:
Code
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace Wizard
{
public class Controller
{
private List<Form> frmList = new List<Form>();
public Information info = new Information();
private int curIndex = 0;
public Controller()
{
frmList.Add(new frmStep1());
frmList.Add(new frmStep2());
frmList.Add(new frmStep3());
foreach (frmBase frm in frmList)
{
frm.controller = this;
//frm.DisableButton();
}
}
public Boolean IsFirstForm
{
get { return curIndex == 0; }
}
public Boolean IsLastForm
{
get { return curIndex==frmList.Count-1;}
}
public void GoNext()
{
if (curIndex + 1 < frmList.Count)
{
((frmBase)frmList[curIndex]).Visible = false;
curIndex++;
}
else
return;
((frmBase)frmList[curIndex]).Show();
((frmBase)frmList[curIndex]).DisableButton();
}
public void GoPrev()
{
if (curIndex - 1 >= 0)
{
((frmBase)frmList[curIndex]).Visible = false;
curIndex--;
}
else
return;
((frmBase)frmList[curIndex]).Show();
((frmBase)frmList[curIndex]).DisableButton();
}
public void Begin()
{
((frmBase)frmList[0]).Show();
((frmBase)frmList[0]).DisableButton();
}
public void Finish()
{
curIndex = 0;
Dispose();
}
public void Dispose()
{
foreach (Form frm in frmList)
frm.Close();
}
}
}
第一个子窗体,继承自父窗体frmBase:
Code
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
namespace Wizard
{
public partial class frmStep1 : Wizard.frmBase
{
public frmStep1()
{
InitializeComponent();
}
protected override void UpdateInfor()
{
controller.info.Name = this.txtbName.Text;
}
}
}
第二个子窗体:
Code
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
namespace Wizard
{
public partial class frmStep2 : Wizard.frmBase
{
public frmStep2()
{
InitializeComponent();
}
protected override void UpdateInfor()
{
if (rbtnMale.Checked == true)
controller.info.IsMale = true;
else
controller.info.IsMale = false;
}
}
}
第三个子窗体:
Code
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
namespace Wizard
{
public partial class frmStep3 : Wizard.frmBase
{
public frmStep3()
{
InitializeComponent();
}
protected override void UpdateInfor()
{
string str=" ";
foreach (Control ctrl in panel1.Controls)
if (ctrl.GetType().Name == "CheckBox")
if ((ctrl as CheckBox).Checked)
str += ctrl.Text + ",";
controller.info.ProgrameLanguage = str;
}
}
}
测试Demo:
Code
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace Wizard
{
public partial class frmMain : Form
{
private Controller con;
public frmMain()
{
InitializeComponent();
}
private void showInf()
{
if (con == null)
MessageBox.Show("Please Input Information");
else
{
this.txtbName.Text = con.info.Name;
if (con.info.IsMale == true)
this.txtSex.Text = "Male";
else
this.txtSex.Text = "Female";
this.txtbProLanguage.Text = con.info.ProgrameLanguage;
}
}
private void btnNew_Click(object sender, EventArgs e)
{
con = new Controller();
con.Begin();
}
private void btnCancel_Click(object sender, EventArgs e)
{
this.Close();
}
private void btnOK_Click(object sender, EventArgs e)
{
showInf();
}
}
}
运行结果:
点击NewInf,弹出子窗体:
点击Next到达下一个窗体,点击Cancel退出向导:
点击Previous回到上一个窗体,点击Next到达下一个窗体:
点击Previous回到上一个窗体,点击Finish结束向导。
向导控制器Controller类:
Code
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace Wizard
{
public class Controller
{
private List<Form> frmList = new List<Form>();
public Information info = new Information();
private int curIndex = 0;
public Controller()
{
frmList.Add(new frmStep1());
frmList.Add(new frmStep2());
frmList.Add(new frmStep3());
foreach (frmBase frm in frmList)
{
frm.controller = this;
//frm.DisableButton();
}
}
public Boolean IsFirstForm
{
get { return curIndex == 0; }
}
public Boolean IsLastForm
{
get { return curIndex==frmList.Count-1;}
}
public void GoNext()
{
if (curIndex + 1 < frmList.Count)
{
((frmBase)frmList[curIndex]).Visible = false;
curIndex++;
}
else
return;
((frmBase)frmList[curIndex]).Show();
((frmBase)frmList[curIndex]).DisableButton();
}
public void GoPrev()
{
if (curIndex - 1 >= 0)
{
((frmBase)frmList[curIndex]).Visible = false;
curIndex--;
}
else
return;
((frmBase)frmList[curIndex]).Show();
((frmBase)frmList[curIndex]).DisableButton();
}
public void Begin()
{
((frmBase)frmList[0]).Show();
((frmBase)frmList[0]).DisableButton();
}
public void Finish()
{
curIndex = 0;
Dispose();
}
public void Dispose()
{
foreach (Form frm in frmList)
frm.Close();
}
}
}