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
wData is not intializing in forms
LikeLike
replaced while loop with if conditions
MyInitialization displaying first form and immediately close that.
LikeLike
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
LikeLike
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;
LikeLike
Nice one , really helped to create a prototype wizard
LikeLike
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
LikeLike
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.
LikeLike
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!
LikeLike
Thanks!
LikeLike
I’m having the same problem as ashwani, the wData isn’t initializing
LikeLike
Add following to each form
private WizardData wData;
LikeLike
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.
LikeLike
Thank you good job.
LikeLike
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?
LikeLike
@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();
}
LikeLike
Thank you for this
BTW: Do you live in that place(scenery)? it so beautiful.
LikeLike
Thanks Arif !!
Unfortunately no !! 🙂
LikeLike
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
LikeLike
I have a problem, I can’t close the windows using the icon on the top right.
Do you have any idea?
Thanks
LikeLike
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)
LikeLike