要达到WebForm下Wizard控件的相似效果,关键的部分设计基本一样,各关键部分如下:
1. enum WizardStepType //向导的类型
2. class WizardStep //单步向导
3. class WizardStepCollection //向导集合
4. class WizardDesigner //提供设计时支持的向导类
5. class Wizard //主要的向导类
首先是WizardStepType的定义,这个与WebForm下的基本一样:
public enum WizardStepType
{
Start,
Step,
Finish,
Complete
}
{
Start,
Step,
Finish,
Complete
}
接着是WizardStep的定义,它的关键成员有以下几个:
1)bool AllowNext //是否允许向导跳到下一步
2) bool isLocked //向导是否处于锁定状态
3) WizardStepType StepType //向导类型
4) Cancel,Previous,Next,Finish Button //向导基本按钮
着重要介绍的是 向导基本按钮的实现:
private void InitStepButtons()
{
CancelButton.Text = "Cancel";
CancelButton.Size = new Size(94, 34);
CancelButton.Location = new Point(30, Height - 30 - CancelButton.Height);
CancelButton.FlatStyle = FlatStyle.System;
CancelButton.Click += CancelButton_Click;
FinishButton.Text = "Finish";
FinishButton.Size = new Size(94, 34);
FinishButton.Location = new Point(Width - 30 - FinishButton.Width, Height - 30 - FinishButton.Height);
FinishButton.FlatStyle = FlatStyle.System;
FinishButton.Click += FinishButton_Click;
NextButton.Text = "Next >";
NextButton.Size = new Size(94, 34);
NextButton.Location = new Point(FinishButton.Location.X - 30 - NextButton.Width, Height - 30 - FinishButton.Height);
NextButton.FlatStyle = FlatStyle.System;
NextButton.Click += NextButton_Click;
PreviousButton.Text = "< Previous";
PreviousButton.Size = new Size(94, 34);
PreviousButton.Location = new Point(NextButton.Location.X - 30 - PreviousButton.Width, Height - 30 - FinishButton.Height);
PreviousButton.FlatStyle = FlatStyle.System;
PreviousButton.Click += PreviousButton_Click;
Controls.Add(PreviousButton);
Controls.Add(NextButton);
Controls.Add(FinishButton);
Controls.Add(CancelButton);
}
private void DrawWizardButton(WizardStepType steptype)
{
switch(steptype)
{
case WizardStepType.Start:
{
PreviousButton.Enabled = false;
NextButton.Enabled = true;
FinishButton.Enabled = false;
CancelButton.Enabled = true;
}
break;
case WizardStepType.Step:
{
PreviousButton.Enabled = true;
NextButton.Enabled = true;
FinishButton.Enabled = false;
CancelButton.Enabled = true;
}
break;
case WizardStepType.Finish :
{
PreviousButton.Enabled = true;
NextButton.Enabled = false;
FinishButton.Enabled = true;
CancelButton.Enabled = true;
}
break;
case WizardStepType.Complete :
{
PreviousButton.Enabled = false;
NextButton.Enabled = false;
FinishButton.Enabled = true;
CancelButton.Enabled = true;
}
break;
}
}
protected override void OnResize(EventArgs eventargs)
{
base.OnResize(eventargs);
CancelButton.Location = new Point(30, Height - 30 - CancelButton.Height);
FinishButton.Location = new Point(Width - 30 - FinishButton.Width, Height - 30 - FinishButton.Height);
NextButton.Location = new Point(FinishButton.Location.X - 30 - NextButton.Width, Height - 30 - FinishButton.Height);
PreviousButton.Location = new Point(NextButton.Location.X - 30 - PreviousButton.Width, Height - 30 - FinishButton.Height);
}
public delegate void CancelButtonEventHandler(object sender, EventArgs e);
public delegate void PreviousButtonEventHandler(object sender, EventArgs e);
public delegate void NextButtonEventHandler(object sender, EventArgs e);
public delegate void FinishButtonEventHandler(object sender, EventArgs e);
public event CancelButtonEventHandler OnCancelButtonClick;
public event PreviousButtonEventHandler OnPreviousButtonClick;
public event NextButtonEventHandler OnNextButtonClick;
public event FinishButtonEventHandler OnFinishButtonClick;
private void CancelButton_Click(object sender, EventArgs e)
{
OnCancelButtonClick.Invoke(sender,e);
}
private void PreviousButton_Click(object sender,EventArgs e)
{
OnPreviousButtonClick.Invoke(sender, e);
}
private void NextButton_Click(object sender, EventArgs e)
{
if(AllowNext)
OnNextButtonClick.Invoke(sender, e);
}
private void FinishButton_Click(object sender, EventArgs e)
{
OnFinishButtonClick.Invoke(sender, e);
}
{
CancelButton.Text = "Cancel";
CancelButton.Size = new Size(94, 34);
CancelButton.Location = new Point(30, Height - 30 - CancelButton.Height);
CancelButton.FlatStyle = FlatStyle.System;
CancelButton.Click += CancelButton_Click;
FinishButton.Text = "Finish";
FinishButton.Size = new Size(94, 34);
FinishButton.Location = new Point(Width - 30 - FinishButton.Width, Height - 30 - FinishButton.Height);
FinishButton.FlatStyle = FlatStyle.System;
FinishButton.Click += FinishButton_Click;
NextButton.Text = "Next >";
NextButton.Size = new Size(94, 34);
NextButton.Location = new Point(FinishButton.Location.X - 30 - NextButton.Width, Height - 30 - FinishButton.Height);
NextButton.FlatStyle = FlatStyle.System;
NextButton.Click += NextButton_Click;
PreviousButton.Text = "< Previous";
PreviousButton.Size = new Size(94, 34);
PreviousButton.Location = new Point(NextButton.Location.X - 30 - PreviousButton.Width, Height - 30 - FinishButton.Height);
PreviousButton.FlatStyle = FlatStyle.System;
PreviousButton.Click += PreviousButton_Click;
Controls.Add(PreviousButton);
Controls.Add(NextButton);
Controls.Add(FinishButton);
Controls.Add(CancelButton);
}
private void DrawWizardButton(WizardStepType steptype)
{
switch(steptype)
{
case WizardStepType.Start:
{
PreviousButton.Enabled = false;
NextButton.Enabled = true;
FinishButton.Enabled = false;
CancelButton.Enabled = true;
}
break;
case WizardStepType.Step:
{
PreviousButton.Enabled = true;
NextButton.Enabled = true;
FinishButton.Enabled = false;
CancelButton.Enabled = true;
}
break;
case WizardStepType.Finish :
{
PreviousButton.Enabled = true;
NextButton.Enabled = false;
FinishButton.Enabled = true;
CancelButton.Enabled = true;
}
break;
case WizardStepType.Complete :
{
PreviousButton.Enabled = false;
NextButton.Enabled = false;
FinishButton.Enabled = true;
CancelButton.Enabled = true;
}
break;
}
}
protected override void OnResize(EventArgs eventargs)
{
base.OnResize(eventargs);
CancelButton.Location = new Point(30, Height - 30 - CancelButton.Height);
FinishButton.Location = new Point(Width - 30 - FinishButton.Width, Height - 30 - FinishButton.Height);
NextButton.Location = new Point(FinishButton.Location.X - 30 - NextButton.Width, Height - 30 - FinishButton.Height);
PreviousButton.Location = new Point(NextButton.Location.X - 30 - PreviousButton.Width, Height - 30 - FinishButton.Height);
}
public delegate void CancelButtonEventHandler(object sender, EventArgs e);
public delegate void PreviousButtonEventHandler(object sender, EventArgs e);
public delegate void NextButtonEventHandler(object sender, EventArgs e);
public delegate void FinishButtonEventHandler(object sender, EventArgs e);
public event CancelButtonEventHandler OnCancelButtonClick;
public event PreviousButtonEventHandler OnPreviousButtonClick;
public event NextButtonEventHandler OnNextButtonClick;
public event FinishButtonEventHandler OnFinishButtonClick;
private void CancelButton_Click(object sender, EventArgs e)
{
OnCancelButtonClick.Invoke(sender,e);
}
private void PreviousButton_Click(object sender,EventArgs e)
{
OnPreviousButtonClick.Invoke(sender, e);
}
private void NextButton_Click(object sender, EventArgs e)
{
if(AllowNext)
OnNextButtonClick.Invoke(sender, e);
}
private void FinishButton_Click(object sender, EventArgs e)
{
OnFinishButtonClick.Invoke(sender, e);
}
在使用控件的时候,记得要为使用的 基本向导按钮 添加 EventHandler, 否则会提示空引用错误。
WizardStepCollection 就是一个普通的集合类,主要是方便对向导中各个步骤的处理。
Wizard 继承于 UserControl, 比较重要的成员是:
1)int ActivateStep //当前向导步骤的Index
2)WizardStep ActivateWizardStep //当前步骤
这两个成员控制了向导步骤的跳转。
WizardDesigner 为 Wizard 提供设计时支持,类似 TabControl 控件一样,具体请看源代码。
以下是使用中的Wizard 控件:
虽然这个控件比较简单,但至少满足了我的需要,希望也能给大家带来帮助!
控件不完美的地方很多,希望大家积极提出批评和建议,小弟虚心接受!
源代码下载:/Files/XiaoFaye/WinForm.Wizard.zip