Create wizard like window application C#


Hi today we will see how to create wizard like window application in .NET 2.0.

1) Create a new window application.
2) Add three forms in it. I have named the form as FirstStep, SecondStep and ThirdStep.
3) In FirstStep add two button – Next and Cancel
4) In SecondStep add three button- Previous, Next and Cancel
5) In ThirdStep add two button- Previous and Cancel
6) Than add a new class file – name it WizardData.cs
7) Add following code to it

// this wizardData class will have a enumeration and a property to display the appropriate form
public class WizardData
{
public enum wizardForms
{
FirstStep =1, SecondStep=2, ThirdStep=3,Cancel =99
}
private wizardForms formToShow;
public wizardForms FormToShow
{
get
{
return formToShow;
}
set
{
formToShow=value;
}
}
}

8. Then go to program.cs and modify it in the following manner

[STAThread]
static void Main()
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
MyInitialization();

//Replace Application.Run(…. ) with MyInitialization() – custom function
}

9) The code for MyInitialization() goes like this

// Create instances of all the forms to be displayed
private static void MyInitialization()
{
WizardData wData = new WizardData();
wData.FormToShow = WizardData.wizardForms.FirstStep;
Form step1 = new FirstStep(wData);
Form step2 = new SecondStep(wData);
Form step3 = new ThirdStep(wData);
while (wData.FormToShow != WizardData.wizardForms.Cancel)
{
switch (wData.FormToShow)
{
case WizardData.wizardForms.FirstStep:
{
step1.ShowDialog();
break;
}
case WizardData.wizardForms.SecondStep:
{
step2.ShowDialog();
break;
}
case WizardData.wizardForms.ThirdStep:
{
step3.ShowDialog();
break;
}
}
}
10) Now go to your FirstStep Form. Replace the constructor with this

public FirstStep(WizardData wd)
{
this.wData = wd;
InitializeComponent();
}

11) In the cancel button and next button click event handler write the following code

private void btnNext_Click(object sender, EventArgs e)
{
// to show the SecondStep form
wData.FormToShow = WizardData.wizardForms.SecondStep;
this.Close();
}
private void btnCancel_Click(object sender, EventArgs e)
{
wData.FormToShow = WizardData.wizardForms.Cancel;
this.Close();
}

12) Repeat the same step 11 for SecondStep and ThirdStep form.
for previous button click event handler add the following code
private void btnPrevious_Click(object sender, EventArgs e)
{
// replace the wizardForm.(…) with appropriate form to be displayed
wData.FormToShow = WizardData.wizardForms.FirstStep;
this.Close();
}


That’s it

Author: Nishant Rana

I love working in and sharing everything about Microsoft.NET technology !

20 thoughts on “Create wizard like window application C#”

  1. Works fine – and nice how easy you kept it!
    Only flicker in the taskbar and in between changing the forms is not really desireable

    Thanks though

    Like

  2. Works like a charm! Your instructions are almost complete but I would like to add the following:

    Add following to each form:

    private WizardData wData;

    Like

  3. Really Works Fine.
    But flickering is the major issue. Even DoubleBuffering can’t solve the flickering issue. I think it should be a single form displaying the controls based on the Previous & Next button clicks.

    Like

  4. I think that a single form solution with a user control for each step would be more elegant since that would eliminate the taskbar flickering someone mentioned earlier.

    Like

  5. My problem is that I receiving two err:
    No overload for method ‘SecondStep’ takes ‘1’ arguments
    No overload for method ‘ThirdStep’ takes ‘1’ arguments

    Where could be a problem?

    Like

    1. @Martin:

      I think you have created a form1 in different name.
      here in the above explaination the form name is “FirstStep”, where as in my application i have named my form as “Form1”

      replace the name of your form-1-name. with this u should be able to fix those errors.

      all these u have to do in ur form1.cs file… similarly with the case of form 2 and 3.

      public Form1(WizardData wd)
      {
      this.wData = wd;
      InitializeComponent();
      }

      Like

  6. First of all. Thanks very much for your useful post.

    I just came across your blog and wanted to drop you a note telling you how impressed I was with the

    information you have posted here.

    Please let me introduce you some info related to this post and I hope that it is useful for community.

    There is a good C# resource site, Have alook

    http://CSharpTalk.com

    Thanks again
    Rahul

    Like

  7. Thank you for this, but i have the problem .
    for example textBox1.text in the First Step(first Form) from to transfer textbox.text Secon step(second form) how can i do ? (form to form data transfer)

    Like

Please share your thoughts

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.