Generating Wizards using Panels

Wizards can be generated in different ways. This article shows you how to create wizards using panels.


Wizards can be generated in different ways. Here are three common ways -

1. Tab control can be used (in which the tab pages visibility property can be manipulated)

The disadvantage of this is that the tab control color can not be changed (even though the tap page back color can be changed, the look and feel is not great)

2. Using Multiple Forms

Requires more code and care must be taken to transfer data between the forms.

3. Using Panel


The panels, which are on the form, have to be added to the array arrayPanel. If a new panel has to be added, add the panel and then update this array in the required position.

arrayPanel=new Panel[]{panel1,panel2,panel3,panel4};

There are list of boolean variables which has the value whether or not the button (Cancel,Back,Next,Finish) is enabled.

private bool cancelEnabled;
private bool backEnabled;
private bool nextEnabled;
private bool finishEnabled;

The following variables stores the present, previous (back) and next panel

private Panel nextPanel;
private Panel backPanel;
private Panel presentPanel;

The following method (setBtnPanProperty) sets the values of the above variables. The method takes in the array index of the panel (presentPanelPosition) that is displayed (which is front). The backPanel, nextPanel and presentPanel are also set.

If presentPanelPosition is equal to 0(the first panel), the finish and back button is disabled. If presentPanelPosition is the last panel (arrayPanel.Length-1) then the next button has to disabled and finish button enabled.For all the other conditions the finish button is disabled.

private void setBtnPanProperty(int presentPanelPosition )
{
int panelLength=arrayPanel.Length-1;
if(presentPanelPosition==0)
{
//first panel...no back ,no finish
cancelEnabled=true;
backEnabled=
false;
nextEnabled=
true;
finishEnabled=
false;
presentPanel=arrayPanel[presentPanelPosition];
nextPanel=arrayPanel[presentPanelPosition+1];
backPanel=
null;
}
else if(presentPanelPosition==panelLength)
{
//last panel...no next,finish
cancelEnabled=true;
backEnabled=
true;
nextEnabled=
false;
finishEnabled=
true;
presentPanel=arrayPanel[presentPanelPosition];
nextPanel=null;
backPanel=arrayPanel[presentPanelPosition-1];
}
else
{
//no finish,next and back
cancelEnabled=true;
backEnabled=
true;
nextEnabled=
true;
finishEnabled=
false;
presentPanel=arrayPanel[presentPanelPosition];
nextPanel=arrayPanel[presentPanelPosition+1];
backPanel=arrayPanel[presentPanelPosition-1];
}
}

When the next button is clicked, the present panel is set to presentPanel+1 and then the setBtnPanProperty method is called which sets the properties of the buttons and panel.

private void btnNext_Click(object sender, System.EventArgs e)
{
presentPanel=arrayPanel[getPosition(presentPanel)+1];
setBtnPanProperty(getPosition(presentPanel));
refreshLook();
}

When the back button is clicked, the present panel is set to presentPanel-1 and then the setBtnPanProperty method is called which sets the properties of the buttons and panel.

private void btnback_Click(object sender, System.EventArgs e)
{
presentPanel=arrayPanel[getPosition(presentPanel)-1];
setBtnPanProperty(getPosition(presentPanel));
refreshLook();
}