Introduction
Some month that I have started to develop with .NET framework and C#. Used to
MFC, I was very disappointed to not found a class to make a wizard. I�ve
checked the web, and found the wizard framework of James T. Johnson. But, in
fact, I found it a little complicated to use and design, so I�ve developed my
own. The idea, is to ease the design of the wizard and re-use MFC concept.
With MFC, the creation of a wizard was the same process than create tab
control and tab page. So, I�ve decided to reuse the Visual Studio .NET wizard
to create Tab control, and work around.
The final result is a dialog, with an empty tab control that must be inherited.
At design time, the developer create tab page and fill them with the control
that he wants. At run time, the dialog act as a wizard, hiding the tab control.
Design of a simple wizard
Inheritance of dialog wizard
Use the �Add inherited form� wizard in Visual Studio.net, and select the WizardForm
in the inheritance picker dialog.
You have the following form in your project:
The tab control is empty, but you see the 4 essential buttons : �Cancel�,
�Back�, �Next� and �Finish�.
Creating pages
Create the pages of the wizard, as you create page for a tab control. Take in
mind that the order of the page will be the order of the page in the wizard.
For each tab page, set the text to the text that describe the step of the
wizard. This text will appear in the title of the wizard at run time.
Implement ActivatePage method
The ActivatePage
method is called automatically before a page is
displayed. This method have the zero based number of the displayed page as
parameter. The developer must implement this method to do all the
initialization task of a page. This method is called only one time per page.
Another method, UnActivatePage
is used to deactivate a page, to
allow the ActivatePage
will be called again if the user go back
and forward.
protected override void ActivatePage(int piPageNumber)
{
switch (piPageNumber)
{
case 1:
DataTable oTable = new DataTable();
break;
}
}
Implement ValidatePage method
The
ValidatePage
method is called when the user use the �Next� or
�Finish� button. The number of the current page is in the integer parameter.
This method must return
true
if the page is validated. If this
method return
false
, the �next� or �Finish� button doesn�t do
anything, until the page is validated.
Run wizard
Simply use the ShowDialog method to show the wizard. It would start by the
first tab page. Clicking on the �Finish� button will end the wizard, but
would use the ActivatePage
and ValidatePage
for each
remaining page.
Since the Wizard is inherited, you can freely customize the base dialog, button
and tab control. Just keep in mind that the tab control is hidden, and the
underlying tab page, resized to fill the space used by the tab control.
You can also add button anywhere on the drawing surface of the dialog to
your own purpose.
protected override bool ValidatePage(int piPageNumber)
{
switch (piPageNumber)
{
case 0:
if (textBox1.Text != string.Empty & textBox2.Text != string.Empty)
return true;
MessageBox.Show(this, "Complete data first");
return false;
}
return true;
}
Other methods used
UnActivatePage
This method is used, when the user use the �Back� button, to deactivate a page,
so the
ActivatePage
be called again.
piPageNumber
parameter
is the number of the page that must be deactivated.
AllowBack
This method is called to allow or disallow the back button in the wizard. The
default behaviour is back enabled.
ForwardOffset
This method can be overloaded. This method increment the number of the
displayed page. The default, is an increment by 1. Overload this method if you
want to jump over pages.
PreviousOffset
This method as the same function as
ForwardOffset
, but decrement
the displayed page by 1 per default. Overload this method to have a different
behaviour.
History
December 2002