Click here to Skip to main content
15,867,756 members
Articles / Programming Languages / C#
Article

Cristi Potlog's Wizard Control for .NET

Rate me:
Please Sign up or sign in to vote.
4.80/5 (54 votes)
21 Sep 2005MIT4 min read 450.5K   5.5K   174   166
This article introduces a sample wizard control for Windows Forms.

Sample Wizard screenshot

Introduction

So I decided to make my contribution to the community. It's time to give something back.

Why do we need yet another wizard control sample, you might ask. My opinion on that is that there's never enough samples on any specific matter.

Background

This control uses VS.NET designers to give you a native support in the IDE. You can add pages to the wizard in the same way you add pages to a tab control. You can place additional controls on the wizard pages.

The control provides run-time events for controlling page navigation. I provided a sample application, with code, to demonstrate this.

The layout of the pages is based on the Wizard 97 Specification from MSDN, particularly on Graphic Design for Wizard Art for welcome and completion pages and interior pages also.

Using the control

Start by creating a new Windows Forms application.

Change the form's name to SampleWizard. Also make the following settings:

C#
FormBorderStyle = FormBorderStyle.FixedDialog;
MaximizeBox = false;
MinimizeBox = false;
StartPosition = FormStartPosition.CenterScreen;
Text = "Sample Wizard";

Add a reference to the compiled Controls.dll assembly. The control should appear in the toolbox now:

The control in the toolbox.

If you double click on the item in the toolbox, an empty wizard will appear on the form:

The control placed on a form.

I set the default docking style of the wizard control to DockingStyle.Fill because usually there is nothing else on a Wizard dialog than the wizard itself.

Notice that the wizard properties appear in the property grid grouped under the Wizard category.

The cotrol's properties.

There are not many properties. This is just a sample control. You can extend it in any way you like, or you may suggest new functionality to me and I just could add those properties in the future.

To add pages to the wizard, you need to click on the Pages properties ellipsis button. The WizardPage Collection Editor appears.

WizardPage Collection Editor.

Here I added the first page and set its style properties to WizardPageStyle.Welcome.

There possible values for the wizard page's Style properties are:

C#
WizardPageStyle.Standard // Standard interior wizard page
                         // with a white banner at the top.
WizardPageStyle.Welcome  // Welcome wizard page with white background
                         // and large logo on the left.
WizardPageStyle.Finish   // Finish wizard page with white background,
                         // a large logo on the left and OK button.
WizardPageStyle.Custom   // Blank wizard page that you can
                         // customize any way you like.

There are also two properties used to specify the title and description of each page. The page draws these texts at the right location depending on the page style. Note that a Custom page does not draw its texts, you are responsible for drawing them or you may not set them at all.

To set the images that appear on the page you need to go on the wizard itself. Here is an example using the pictures I provided with this sample:

Control's image properties set.

I chose to implement the images properties at the main control level to provide a consistent look to the wizard pages. If you want each page to have its own images you may change this implementation.

The welcome page should look now like this screenshot:

The welcome page preview.

Adding and setting the other pages is pretty straightforward.

You can use the control in the same way you use a Tab control. You add pages to the Pages collection and then you place additional controls on the wizard page, just like on a TabPage.

The coolest thing is that you can switch wizard pages at design time by clicking on the Back and Next buttons.

Using the code

Here are some sample code bits of handling the wizard control's events to provide the user with validation and more interaction:

C#
/// <summary>
/// Handles the AfterSwitchPages event of wizardSample.
/// </summary>
private void wizardSample_AfterSwitchPages(object sender, 
        CristiPotlog.Controls.Wizard.AfterSwitchPagesEventArgs e)
{
    // get wizard page to be displayed
    WizardPage newPage = this.wizardSample.Pages[e.NewIndex];

    // check if license page
    if (newPage == this.pageLicense)
    {
        // sync next button's state with check box
        this.wizardSample.NextEnabled = this.checkIAgree.Checked;
    }
    // check if progress page
    else if (newPage == this.pageProgress)
    {
        // start the sample task
        this.StartTask();
    }
}

The above code shows you how to provide custom initialization of controls in the page about to be displayed.

C#
/// <summary>
/// Handles the BeforeSwitchPages event of wizardSample.
/// </summary>
private void wizardSample_BeforeSwitchPages(object sender, 
             CristiPotlog.Controls.Wizard.BeforeSwitchPagesEventArgs e)
{
    // get wizard page already displayed
    WizardPage oldPage = this.wizardSample.Pages[e.OldIndex];

    // check if we're going forward from options page
    if (oldPage == this.pageOptions && e.NewIndex > e.OldIndex)
    {
        // check if user selected one option
        if (this.optionCheck.Checked == false && 
            this.optionSkip.Checked == false)
        {
            // display hint & cancel step
            MessageBox.Show("Please chose one of the options presented.",
                            "Sample Wizard",
                            MessageBoxButtons.OK,
                            MessageBoxIcon.Information);
            e.Cancel = true;
        }
        // check if user choosed to skip validation
        else if (this.optionSkip.Checked)
        {
            // skip the next page
            e.NewIndex++;
        }
    }
}

The above code shows you how to provide custom validation before leaving a page.

C#
/// <summary>
/// Handles the Cancel event of wizardSample.
/// </summary>
private void wizardSample_Cancel(object sender, 
             System.ComponentModel.CancelEventArgs e)
{
    // check if task is running
    bool isTaskRunning = this.timerTask.Enabled;
    // stop the task
    this.timerTask.Enabled = false;

    // ask user to confirm
    if (MessageBox.Show("Are you sure you wand to exit the Sample Wizard?",
                        "Sample Wizard",
                        MessageBoxButtons.YesNo,
                        MessageBoxIcon.Question) != DialogResult.Yes)
    {
        // cancel closing
        e.Cancel = true;
        // restart the task
        this.timerTask.Enabled = isTaskRunning;
    }
}

This code shows you how to display a confirmation message to the user when one cancels the wizard.

C#
/// <summary>
/// Handles the Finish event of wizardSample.
/// </summary>
private void wizardSample_Finish(object sender, System.EventArgs e)
{
    MessageBox.Show("The Sample Wizard finished succesfuly.",
                    this.Text,
                    MessageBoxButtons.OK,
                    MessageBoxIcon.Information);
}

The above code simply displays a notification message after the wizard finishes.

C#
/// <summary>
/// Handles the Help event of wizardSample.
/// </summary>
private void wizardSample_Help(object sender, System.EventArgs e)
{
    MessageBox.Show("This is a realy cool wizard control!\n:-)",
                    this.Text,
                    MessageBoxButtons.OK,
                    MessageBoxIcon.Information);
}

The above code simply displays a notification when the user presses the Help button.

Credits

I need to mention that the code is based on Al Gardner's article on wizards: Designer centric Wizard control from which I took the design-time clicking functionality. I simplified the design of the component using less classes. Everything else is pretty much built from scratch.

History

  • September 5th 2005. Minor corrections.
  • August 12th 2005. Overrode NewIndex on BeforeSwitchPagesEventArgs to allow page skipping and provided a new sample page to be skipped.
  • July 2nd 2005. Fixed some behaviour and added Help button.
  • June 24th 2005. Initial release.

License

This article, along with any associated source code and files, is licensed under The MIT License


Written By
Software Developer
Romania Romania
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
BugNext and Back Buttons Disable Pin
dday3313-Oct-16 11:34
dday3313-Oct-16 11:34 
GeneralJust Wanted to say thanks Pin
kinothe116-Oct-14 8:54
kinothe116-Oct-14 8:54 
QuestionChanging Button Text Pin
Sinan Direk17-Apr-14 4:17
Sinan Direk17-Apr-14 4:17 
AnswerRe: Changing Button Text Pin
NiloPaim20-Oct-14 1:19
NiloPaim20-Oct-14 1:19 
GeneralRe: Changing Button Text Pin
NiloPaim20-Oct-14 3:10
NiloPaim20-Oct-14 3:10 
GeneralMy vote of 1 Pin
Oleksandr Kucherenko25-May-12 12:07
Oleksandr Kucherenko25-May-12 12:07 
QuestionWizard control not appearing in toolbox Pin
Member 81389305-Aug-11 5:46
Member 81389305-Aug-11 5:46 
AnswerRe: Wizard control not appearing in toolbox Pin
THEGAFF21-Sep-11 4:26
THEGAFF21-Sep-11 4:26 
AnswerRe: Wizard control not appearing in toolbox Pin
Member 43130936-Dec-11 13:18
Member 43130936-Dec-11 13:18 
Question120 DPI again Pin
daneluta12-Jul-11 7:57
daneluta12-Jul-11 7:57 
GeneralGreat control dude Pin
marcel_an10-May-11 2:42
marcel_an10-May-11 2:42 
GeneralYour sample code not working. Pin
reachrishikh23-Jul-09 3:23
reachrishikh23-Jul-09 3:23 
AnswerRe: Your sample code not working. Pin
Tim8w19-Jul-18 11:53
Tim8w19-Jul-18 11:53 
Generalwonderful Pin
geigy1-Jul-09 16:19
geigy1-Jul-09 16:19 
QuestionMade some changes to your control Pin
ItalianCousin19-Nov-08 7:28
ItalianCousin19-Nov-08 7:28 
QuestionThis is EXACTLY what I was looking for! But, I need a little help... Please..? Pin
Willem Le Roux18-Sep-08 4:24
Willem Le Roux18-Sep-08 4:24 
Generalkeeping wizard open after finish event Pin
kkumgul23-Jul-08 8:57
kkumgul23-Jul-08 8:57 
GeneralRe: keeping wizard open after finish event Pin
Cristi Potlog19-Jan-09 21:08
Cristi Potlog19-Jan-09 21:08 
GeneralUpdate for 120 DPI Pin
Glen Harvy22-May-08 11:48
Glen Harvy22-May-08 11:48 
GeneralRe: Update for 120 DPI [modified] Pin
StephenKearney29-May-08 16:18
StephenKearney29-May-08 16:18 
AnswerRe: Update for 120 DPI Pin
dgis9-Dec-09 4:25
dgis9-Dec-09 4:25 
GeneralPlagiate!!! [modified] Pin
Oleksandr Kucherenko13-Mar-08 22:36
Oleksandr Kucherenko13-Mar-08 22:36 
GeneralRe: NOT quite Plagiate!!! Pin
Cristi Potlog20-Mar-08 22:01
Cristi Potlog20-Mar-08 22:01 
GeneralRe: NOT quite Plagiate!!! Pin
Oleksandr Kucherenko7-Apr-08 2:24
Oleksandr Kucherenko7-Apr-08 2:24 
GeneralRe: Plagiate!!! Pin
Valeri Makarov13-Apr-08 18:22
Valeri Makarov13-Apr-08 18:22 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.