Go homepage(回首页) Upload pictures (上传图片) Write articles (发文字帖)
The author:(作者)delvpublished in(发表于) 2014/1/8 7:02:47 C#设计的一个向导程序(Wizard)框架_[Asp.Net教程]
在现实的软件中,经常可以看到一些向导(Wizard)的存在,如何给自己的应用程序实现一个向导呢?下面给出一个使用面向对象的思想设计出来的应用程序向导框架,虽然很简单,但希望能给人帮助。
其中有三个比较关键的类,一个是向导窗体要收集的信息封装成的类Information,一个是所有向导窗体都要继承的窗体基类frmBase,还有一个就是最关键的类,向导控制类WizardController。
有了基类frmBase,设计一个子类窗体非常简单,只需从frmBase类中派生一个新窗体,设计完用户界面之后重写其UpdateInfo()方法即可。
所有代码(VS2003版)如下,通俗易懂,不再做说明:
Information类:
using System;
namespace Wizard{ /// /// Information 的摘要说明。 /// public class Information { public Information() { // // TOD 在此处添加构造函数逻辑 // }
//姓名 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{ /// /// frmBase 的摘要说明。 /// 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; /// /// 必需的设计器变量。 /// private System.ComponentModel.Container components = null;
public frmBase() { // // Windows 窗体设计器支持所必需的 // InitializeComponent();
// // TOD 在 InitializeComponent 调用后添加任何构造函数代码 // }
/// /// 清理所有正在使用的资源。 /// protected override void Dispose( bool disposing ) { if( disposing ) { if(components != null) { components.Dispose(); } } base.Dispose( disposing ); }
#region Windows 窗体设计器生成的代码 /// /// 设计器支持所需的方法 - 不要使用代码编辑器修改 /// 此方法的内容。 /// 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{ /// /// WizardController 的摘要说明。 /// public class WizardController { public WizardController() { // // TOD 在此处添加构造函数逻辑 // 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();
// TOD 在 InitializeComponent 调用后添加任何初始化 }
/// /// 清理所有正在使用的资源。 /// protected override void Dispose( bool disposing ) { if( disposing ) { if (components != null) { components.Dispose(); } } base.Dispose( disposing ); }
#region 设计器生成的代码 /// /// 设计器支持所需的方法 - 不要使用代码编辑器修改 /// 此方法的内容。 /// 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);
protected override void UpdateInfo() { this.controller.info.Name = txtName.Text.Trim(); this.controller.info.IsMale = rdoMale.Checked; }
}}
第二个子窗体:
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();
#region 设计器生成的代码 /// /// 设计器支持所需的方法 - 不要使用代码编辑器修改 /// 此方法的内容。 /// 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);
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:
namespace Wizard{ /// /// frmTest 的摘要说明。 /// public class frmTest : System.Windows.Forms.Form { /// /// 必需的设计器变量。 /// 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();
#region Windows 窗体设计器生成的代码 /// /// 设计器支持所需的方法 - 不要使用代码编辑器修改 /// 此方法的内容。 /// 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 /// /// 应用程序的主入口点。 /// [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;
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"); } } }}
来源:网络
赞