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

Another wizard for .NET using tab control

Rate me:
Please Sign up or sign in to vote.
3.93/5 (15 votes)
21 Dec 20023 min read 102.2K   2.5K   26   10
Another wizard for .NET using tab control

Sample Image - WizardForm.jpg

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.

  Image 2

You have the following form in your project:

Image 3

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.

Image 4

  Image 5

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.

C#
protected override void ActivatePage(int piPageNumber) 
{
  switch (piPageNumber)
  { 
    case 1: 
      DataTable oTable = new DataTable();

      // Do some stuff to init the page
      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. 

C#
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

  • Initial posting

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


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

Comments and Discussions

 
QuestionLicense Pin
Postremus1-Jan-13 4:10
Postremus1-Jan-13 4:10 
AnswerRe: License Pin
nimbus3d4-Jan-13 22:04
nimbus3d4-Jan-13 22:04 
No specific licence, you can re-use the code as you want.
Regards
GeneralResize Warning .net 2.0 Pin
Clermond25-Dec-05 4:53
Clermond25-Dec-05 4:53 
GeneralFranework to Wizard from VBPJ Pin
tengtium7-Mar-05 1:53
tengtium7-Mar-05 1:53 
GeneralCreating Tab Index Pin
Anonymous3-Dec-04 20:38
Anonymous3-Dec-04 20:38 
GeneralResize the wizard Pin
Tsjaar26-Oct-04 2:45
Tsjaar26-Oct-04 2:45 
GeneralRe: Resize the wizard Pin
miqel8-Feb-11 0:30
miqel8-Feb-11 0:30 
GeneralTab control Pin
Deepkiss24-May-03 1:58
Deepkiss24-May-03 1:58 
GeneralTab control Pin
Deepkiss24-May-03 1:49
Deepkiss24-May-03 1:49 
GeneralRe: Tab control Pin
nimbus3d24-May-03 2:23
nimbus3d24-May-03 2:23 

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.