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 codeprivate 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
RSS - Posts

wData is not intializing in forms
By: ashwani on March 12, 2008
at 1:46 pm
replaced while loop with if conditions
MyInitialization displaying first form and immediately close that.
By: shyamala on May 15, 2008
at 12:14 pm
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
By: timo on June 18, 2008
at 2:48 am
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;
By: ddnepo on July 9, 2008
at 12:14 am
Nice one , really helped to create a prototype wizard
By: Anonymous on July 17, 2008
at 7:47 am
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
By: Shaveta on September 25, 2008
at 10:35 am
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.
By: Murali Krishna on November 3, 2008
at 10:56 am
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!
By: Gonzalo Rafuls on July 1, 2009
at 3:16 am
Thanks!
By: Ruud on February 27, 2009
at 9:36 am
I’m having the same problem as ashwani, the wData isn’t initializing
By: Ricardo on March 23, 2009
at 7:52 pm
Add following to each form
private WizardData wData;
By: Nishant Rana on March 24, 2009
at 7:25 am
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.
By: Bjarki on April 14, 2009
at 11:19 pm
Thank you good job.
By: Raj on June 25, 2009
at 2:11 pm
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?
By: Martin on June 25, 2009
at 5:39 pm
@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();
}
By: Anonymous on July 11, 2009
at 7:10 am
Thank you for this
BTW: Do you live in that place(scenery)? it so beautiful.
By: Arif on July 22, 2009
at 8:59 am
Thanks Arif !!
Unfortunately no !! :)
By: Nishant Rana on July 22, 2009
at 9:15 am