37

I am new in Creating Wizards for Windows Forms Application in C# .Net. So i don't have any idea in wizard creation. Please give me some ideas about creating Multiple wizard.

Regards, ravi

Hans Passant
  • 922,412
  • 146
  • 1,693
  • 2,536
Ravi
  • 1,263
  • 6
  • 17
  • 23

3 Answers3

142

Lots of ways to do it. Creating a form for each wizard step is possible, but very awkward. And ugly, lots of flickering when the user changes the step. Making each step a UserControl can work, you simply switch them in and out of the form's Controls collection. Or make one of them Visible = true for each step. The UC design tends to get convoluted though, you have to add public properties for each UI item.

The easy and RAD way is to use a TabControl. Works very well in the designer since it allows you to switch tabs at design time and drop controls on each tab. Switching steps is trivial, just change the SelectedIndex property. The only thing non-trivial is to hide the tabs at runtime. Still easy to do by processing a Windows message. Add a new class to your form and paste the code shown below. Compile. Drop the new control from the top of the toolbox onto your form.

using System;
using System.Windows.Forms;

class WizardPages : TabControl {
  protected override void WndProc(ref Message m) {
    // Hide tabs by trapping the TCM_ADJUSTRECT message
    if (m.Msg == 0x1328 && !DesignMode) m.Result = (IntPtr)1;
    else base.WndProc(ref m);
  }
}
Hans Passant
  • 922,412
  • 146
  • 1,693
  • 2,536
  • 5
    +51: and so now you can use a tab control, hiding the tabs ...and switch between tab pages with the tab index ...??? I'll be using this ...so much nicer than managing overlapping panels. – IAbstract Sep 10 '11 at 15:43
  • @Hans Nice answer Hans! I am just courius : does this completely hide tabs so user can not click, or activate them by hittink TAB key or something? – Dumbo Jul 09 '12 at 09:39
  • @Sean87 hitting CTRL+TAB actually moves across tab pages – ccalboni Jul 20 '12 at 13:41
  • 5
    @The souloution for tab is just to set `TabStop` to false – Dumbo Jul 20 '12 at 14:07
  • @Hans I tried this solution, while it works great for most part. The problem is that it doesn't remove the tabcontrol border's and frame at runtime. Is it possible to do that? – tunafish24 Nov 06 '15 at 20:35
29
class WizardPages : TabControl
{
    protected override void WndProc(ref Message m)
    {
        // Hide tabs by trapping the TCM_ADJUSTRECT message
        if (m.Msg == 0x1328 && !DesignMode) m.Result = (IntPtr)1;
        else base.WndProc(ref m);
    }        

    protected override void OnKeyDown(KeyEventArgs ke)
    {
        // Block Ctrl+Tab and Ctrl+Shift+Tab hotkeys
        if (ke.Control && ke.KeyCode == Keys.Tab) 
            return;
        base.OnKeyDown(ke);
    }
}
Cluster
  • 998
  • 10
  • 14
  • What about other keys? PageUp and PageDown? Or Left and Right arrow keys? You cant add them because You wont be able to move inside text box that is child of that control. – Misiu Sep 18 '13 at 08:51
  • If you handle `Ctrl`+`Tab` and `Ctrl`+`Shift`+`Tab` do not forget also their equivalents `Ctrl`+`PgUp` and `Ctrl`+`PgDn`. The latter are more dominant in many scenarios where `Ctrl`+`Tab` and `Ctrl`+`Shift`+`Tab` are taken by inner control, e.g. multiline input box. – miroxlav Dec 05 '13 at 09:51
  • @Misiu keys restricted above still work well in child controls (I have just tested that) – miroxlav Dec 05 '13 at 10:52
  • 1
    @miroxlav - I didn't check that, I've wrote simple control that is not extending TabControl. I've used this article: http://www.codeproject.com/Articles/18674/A-Simple-Wizard-Control-for-Net-2-0-with-Full-Desi – Misiu Dec 05 '13 at 12:25
1

You need to create your own to meet your own preferences. A tip will be for you to create a base form named like "frmWizard" then all your wizard windows will inherit from it. You should put common objects or wizard objects on the base class and modify \ override them on the derived class if needed.

Jojo Sardez
  • 8,400
  • 3
  • 27
  • 38
  • 1
    It is useful to combine base form (suggested here) + custom tab control (described in other answers). Creation of foundation for wizards is useful especially when application has more than one wizard. It helps to avoid copy-paste coding horror. – miroxlav Dec 05 '13 at 10:49
  • Useful methods I usually implement in the base class are.. `OnPageEntered`, `OnPageExited`, `AllowNext`, `AllowBack`, etc. – KDecker Mar 21 '16 at 13:20