1.FormBase界面:有“帮助,上一步,下一步,取消”按钮,这些按钮放置在一个Panel上。
namespace DataBase
{
public partial class FormBase : Form
{
public FormBase()
{
InitializeComponent();
}
public WizardController controller = null;
#region 函数
/// <summary>
/// 自定义函数
/// </summary>
public void DisableButton()
{
if (this.controller == null)
return;
if (this.controller.IsFirstForm)
{
this.buttonPrev.Enabled = false;
}
else
{
this.buttonPrev.Enabled = true;
}
if (this.controller.IsLastForm) //如果是向导的最后一个页面,显示“完成”
{
this.buttonNext.Text = "完成";
}
else //否则显示“下一步”
{
this.buttonNext.Text = "下一步";
}
}
protected virtual void UpdateInfo() //由“每一步FormStep1,FormStep2…”的界面 具体实现
{
}
protected virtual void GoNext()
{
UpdateInfo();
if (this.controller.IsLastForm) //如果是向导的最后一个页面,则调用“FinishWizard()”
{
controller.FinishWizard();
this.Visible = false;
}
else//如果是向导的最后一个页面,则调用“GoNext()”
{
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();
}
#endregion
#region 界面控件事件
/// <summary>
/// 界面控件事件
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void buttonPrev_Click(object sender, EventArgs e)
{
GoPrev();
}
private void buttonNext_Click(object sender, EventArgs e)
{
GoNext();
}
//private void buttonFinish_Click(object sender, EventArgs e)
//{
// Finish();
//}
private void buttonCancel_Click(object sender, EventArgs e)
{
Cancel();
}
#endregion
}
}
2.一个WizardController.cs文件(非界面)
namespace DataBase
{
public class WizardController
{
private ArrayList WizardForms = new ArrayList();
private int curIndex = 0;
#region 构造函数
/// <summary>
///构造函数
/// </summary>
public WizardController()
{
WizardForms.Add(new FormStep1()); //frmStep1继承自FormBase
WizardForms.Add(new FormStep2());
WizardForms.Add(new FormStep3());
foreach (FormBase frm in WizardForms)
{
frm.controller = this;
frm.DisableButton();
}
}
#endregion
#region 自定义函数
/// <summary>
/// 自定义函数
/// </summary>
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)
{
((FormBase)WizardForms[curIndex]).Visible = false;
curIndex++;
}
else
{
return;
}
((FormBase)WizardForms[curIndex]).Show();
((FormBase)WizardForms[curIndex]).DisableButton();
}
public void GoPrev()
{
if (curIndex - 1 >= 0)
{
((FormBase)WizardForms[curIndex]).Visible = false;
curIndex--;
}
else
{
return;
}
((FormBase)WizardForms[curIndex]).Show();
((FormBase)WizardForms[curIndex]).DisableButton();
}
public void BeginWizard()
{
((FormBase)WizardForms[0]).Show();
((FormBase)WizardForms[curIndex]).DisableButton();
}
public void FinishWizard()
{
curIndex = 0;
Dispose();
}
private void Dispose()
{
foreach (FormBase frm in WizardForms)
{
frm.Close();
}
}
#endregion
}
}
3. FormStep1界面继承自FormBase,上面有一个Label控件,Text属性“This is FormStep1”
namespace DataBase
{
public partial class FormStep1 : DataBase.FormBase
{
public FormStep1()
{
InitializeComponent();
}
protected override void UpdateInfo()
{
MessageBox.Show("第一步!","Step1",MessageBoxButtons.OK,MessageBoxIcon.Information);
}
}
}
4. FormStep2界面继承自FormBase,上面有一个Label控件,Text属性“This is FormStep2”
namespace DataBase
{
public partial class FormStep2 : DataBase.FormBase
{
public FormStep2()
{
InitializeComponent();
}
protected override void UpdateInfo()
{
MessageBox.Show("第二步!", "Step2", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
}
}
5. FormStep3界面继承自FormBase,上面有一个Label控件,Text属性“This is FormStep3”
namespace DataBase
{
public partial class FormStep3 : DataBase.FormBase
{
public FormStep3()
{
InitializeComponent();
}
protected override void UpdateInfo()
{
MessageBox.Show("第三步!", "Step3", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
}
}
6.FormTestWizard界面,上面一个button,Text属性“启动向导”
namespace DataBase
{
public partial class FormTestWizard : Form
{
public FormTestWizard()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
WizardController controller = new WizardController();
controller.BeginWizard();
}
}
}
7.修改程序的启动界面为“FormTestWizard”
namespace DataBase
{
static class Program
{
/// <summary>
/// 应用程序的主入口点。
/// </summary>
[STAThread]
static void Main()
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.Run(new FormTestWizard()); //启动界面
}
}
}