Posted by: Nishant Rana | September 17, 2007

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


Responses

  1. wData is not intializing in forms

  2. replaced while loop with if conditions

    MyInitialization displaying first form and immediately close that.

  3. 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

  4. 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;

  5. Nice one , really helped to create a prototype wizard

  6. you can find answers to all your queries related to any domain (asp.net, c#, vb.net, c++, java, oracle, c, ajax ) and also all interview question , HR interview questions , just have look at:

    http://interviewhelper.org

  7. 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.

    • To eliminate the flickering you can try using panels. Using the methods show() and hide() for those panels with the next and previous buttons. The result is pretty nice!

  8. Thanks!

  9. I’m having the same problem as ashwani, the wData isn’t initializing

  10. Add following to each form
    private WizardData wData;

  11. 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.

  12. Thank you good job.

  13. 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?

    • @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();
      }

  14. Thank you for this
    BTW: Do you live in that place(scenery)? it so beautiful.

    • Thanks Arif !!
      Unfortunately no !! :)


Leave a response

Your response:

Categories