C#设计的一个向导程序(Wizard)框架--VS2008版 (一)
在现实的软件中,经常可以看到一些向导(Wizard)的存在,如何给自己的应用程序实现一个向导呢?
下面是一个使用面向对象的思想设计出来的应用程序向导框架,模拟OutLook配置邮箱。
其中有三个比较关键的类,一个是向导窗体要收集的信息封装成的类MailInfo,一个是所有向导窗体都要继承的窗体基类MailInfoBase,还有一个就是最关键的类,向导控制类MailInfoController。
有了基类MailInfoBase,设计一个子类窗体非常简单,只需从MailInfoBase类中派生一个新窗体,设计完用户界面之后重写其UpdateInfo()方法即可。
所有代码(VS2008版)如下,通俗易懂,不再做说明:
项目MailControl,输出为类库,其中
MailInfo.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
![]()
namespace MailControl
{
public class MailInfo
{
public MailInfo()
{
![]()
}
public string NickName = "";
public string UsrName = "";
public string Email = "";
public string Password = "";
public string Pop3Server = "";
public string SmtpServer = "";
public string Port = "";
public bool SPA = false;
}
}
MailInfoBase.cs
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 MailControl
{
public partial class MailInfoBase : Form
{
public MailInfoBase()
{
InitializeComponent();
}
![]()
public MailInfoController 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 UpdataInfo()
{
![]()
}
protected virtual void GoNext()
{
UpdataInfo();
controller.GoNext();
}
protected virtual void GoPrev()
{
UpdataInfo();
controller.GoPrev();
}
protected virtual void Finish()
{
UpdataInfo();
controller.FinishWizard();
this.Visible = false;
}
protected virtual void Cancel()
{
this.controller.info = null;
this.Close();
}
![]()
private void btnGoPrev_Click(object sender, EventArgs e)
{
GoPrev();
}
![]()
private void btnGoNext_Click(object sender, EventArgs e)
{
GoNext();
}
![]()
private void btnOver_Click(object sender, EventArgs e)
{
Finish();
}
![]()
private void btnCancel_Click(object sender, EventArgs e)
{
Cancel();
}
}
}
MailInfoBase.designer.cs
namespace MailControl
{
partial class MailInfoBase
{
/// <summary>
/// Required designer variable.
/// </summary>
private System.ComponentModel.IContainer components = null;
![]()
/// <summary>
/// Clean up any resources being used.
/// </summary>
/// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
protected override void Dispose(bool disposing)
{
if (disposing && (components != null))
{
components.Dispose();
}
base.Dispose(disposing);
}
![]()
Windows Form Designer generated code
![]()
public System.Windows.Forms.Panel panel1;
public System.Windows.Forms.Button btnGoPrev;
public System.Windows.Forms.Button btnGoNext;
public System.Windows.Forms.Button btnOver;
public System.Windows.Forms.Button btnCancel;
}
}
MailController.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections;
using System.Windows.Forms;
![]()
namespace MailControl
{
public class MailInfoController
{
public MailInfoController(ref ArrayList al )
{
WizardForms = al;
foreach (MailInfoBase frm in WizardForms)
{
frm.controller = this;
frm.DisableButton();
}
}
private ArrayList WizardForms = new ArrayList();
public MailInfo info = new MailInfo();
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)
{
((MailInfoBase)WizardForms[curIndex]).Visible = false;
curIndex++;
}
else
{
return;
}
((MailInfoBase)WizardForms[curIndex]).Show();
((MailInfoBase)WizardForms[curIndex]).DisableButton();
}
public void GoPrev()
{
if(curIndex-1>=0)
{
((MailInfoBase)WizardForms[curIndex]).Visible = false;
curIndex--;
}
else
{
return;
}
((MailInfoBase)WizardForms[curIndex]).Show();
((MailInfoBase)WizardForms[curIndex]).DisableButton();
}
public void BeginWizard()
{
((MailInfoBase)WizardForms[0]).Show();
((MailInfoBase)WizardForms[curIndex]).DisableButton();
}
public void FinishWizard()
{
curIndex = 0;
Dispose();
}
private void Dispose()
{
foreach(MailInfoBase frm in WizardForms)
{
frm.Close();
}
}
}
}
program.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Windows.Forms;
![]()
namespace MailControl
{
static class Program
{
/// <summary>
/// 应用程序的主入口点。
/// </summary>
[STAThread]
static void Main()
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.Run(new MailInfoBase());
}
}
}
下面是一个使用面向对象的思想设计出来的应用程序向导框架,模拟OutLook配置邮箱。
其中有三个比较关键的类,一个是向导窗体要收集的信息封装成的类MailInfo,一个是所有向导窗体都要继承的窗体基类MailInfoBase,还有一个就是最关键的类,向导控制类MailInfoController。
有了基类MailInfoBase,设计一个子类窗体非常简单,只需从MailInfoBase类中派生一个新窗体,设计完用户界面之后重写其UpdateInfo()方法即可。
所有代码(VS2008版)如下,通俗易懂,不再做说明:
项目MailControl,输出为类库,其中
MailInfo.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace MailControl
{
public class MailInfo
{
public MailInfo()
{
}
public string NickName = "";
public string UsrName = "";
public string Email = "";
public string Password = "";
public string Pop3Server = "";
public string SmtpServer = "";
public string Port = "";
public bool SPA = false;
}
}
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 MailControl
{
public partial class MailInfoBase : Form
{
public MailInfoBase()
{
InitializeComponent();
}
public MailInfoController 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 UpdataInfo()
{
}
protected virtual void GoNext()
{
UpdataInfo();
controller.GoNext();
}
protected virtual void GoPrev()
{
UpdataInfo();
controller.GoPrev();
}
protected virtual void Finish()
{
UpdataInfo();
controller.FinishWizard();
this.Visible = false;
}
protected virtual void Cancel()
{
this.controller.info = null;
this.Close();
}
private void btnGoPrev_Click(object sender, EventArgs e)
{
GoPrev();
}
private void btnGoNext_Click(object sender, EventArgs e)
{
GoNext();
}
private void btnOver_Click(object sender, EventArgs e)
{
Finish();
}
private void btnCancel_Click(object sender, EventArgs e)
{
Cancel();
}
}
}
namespace MailControl
{
partial class MailInfoBase
{
/// <summary>
/// Required designer variable.
/// </summary>
private System.ComponentModel.IContainer components = null;
/// <summary>
/// Clean up any resources being used.
/// </summary>
/// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
protected override void Dispose(bool disposing)
{
if (disposing && (components != null))
{
components.Dispose();
}
base.Dispose(disposing);
}
Windows Form Designer generated code
public System.Windows.Forms.Panel panel1;
public System.Windows.Forms.Button btnGoPrev;
public System.Windows.Forms.Button btnGoNext;
public System.Windows.Forms.Button btnOver;
public System.Windows.Forms.Button btnCancel;
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections;
using System.Windows.Forms;
namespace MailControl
{
public class MailInfoController
{
public MailInfoController(ref ArrayList al )
{
WizardForms = al;
foreach (MailInfoBase frm in WizardForms)
{
frm.controller = this;
frm.DisableButton();
}
}
private ArrayList WizardForms = new ArrayList();
public MailInfo info = new MailInfo();
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)
{
((MailInfoBase)WizardForms[curIndex]).Visible = false;
curIndex++;
}
else
{
return;
}
((MailInfoBase)WizardForms[curIndex]).Show();
((MailInfoBase)WizardForms[curIndex]).DisableButton();
}
public void GoPrev()
{
if(curIndex-1>=0)
{
((MailInfoBase)WizardForms[curIndex]).Visible = false;
curIndex--;
}
else
{
return;
}
((MailInfoBase)WizardForms[curIndex]).Show();
((MailInfoBase)WizardForms[curIndex]).DisableButton();
}
public void BeginWizard()
{
((MailInfoBase)WizardForms[0]).Show();
((MailInfoBase)WizardForms[curIndex]).DisableButton();
}
public void FinishWizard()
{
curIndex = 0;
Dispose();
}
private void Dispose()
{
foreach(MailInfoBase frm in WizardForms)
{
frm.Close();
}
}
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Windows.Forms;
namespace MailControl
{
static class Program
{
/// <summary>
/// 应用程序的主入口点。
/// </summary>
[STAThread]
static void Main()
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.Run(new MailInfoBase());
}
}
}


浙公网安备 33010602011771号