For my last project I needed to create wizard application using c#. Sadly differently than in VC++, Visual Studio doesn’t have an appropriate project type for .NET platform so I had to write it from scratch.

Google suggested that i have to create function that will replace main application loop.
Finally i’ve decided to create singleton class which will contain wizard pages (Windows forms), navigation functions and main application loop function:

setupWizard

program program class with Main() function.
setupWizard implements singleton pattern. Also it contains run() function that replaces standard Application.Run() in program class.
wizardForm is a standard System.Windows.Forms.Form class.

After creating setupWizard class and three example forms I’ve implemented setupWizard.run() function to display first form as modal window and wait for some return value. Depending on returned value function exits program or display next/previous form (as shown below).

Below I’ve posted source code for program and setupWizard classes (without singelton pattern, just “wizard” parts).

program class

/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main()
{
	Application.EnableVisualStyles();
	Application.SetCompatibleTextRenderingDefault(false);
	setupWizard.Instance.Run();
}

setupWizard class

/// <summary>
/// Wizard application controller.
/// </summary>
public sealed class setupWizard
{
...
/// <summary>
/// Available forms - wizard pages (tag property is 
/// optional, but it can be useful in future)
/// </summary>
private Dictionary<int, Form> forms = 
        new Dictionary<int, Form>() {
    {0, new wizardOne() { Tag = 0}},
    {1, new wizardTwo() { Tag = 1}},
    {2, new wizardThree() { Tag = 2}}
};

/// <summary>
/// Currently displayed form (default to first one)
/// </summary>
private int current = 0;
...
/// <summary>
/// Displays wizard next form
/// </summary>
public void wizardNext()
{
    forms[current].DialogResult = DialogResult.OK;
    forms[current++].Close();
}

/// <summary>
/// Display wizard previous form
/// </summary>
public void wizardPrev()
{
    forms[current].DialogResult = DialogResult.OK;
    forms[current--].Close();
}

/// <summary>
/// Exits wizard (ends program)
/// </summary>
/// <param name="dialogResult">Dialog result of current 
///    form:
///      OK to change form,
///      Abort - abort program.
///      Cancel or other - end program with confirmation.
/// </param>
/// <returns></returns>
public bool wizardExit(DialogResult dialogResult)
{
    if (dialogResult == DialogResult.OK)
        return true;

    if (dialogResult == DialogResult.Abort 
        || MessageBox.Show(
           @"Czy na pewno chcesz zakończyć 
             działanie aplikacji?"
        , "Instalacja"
        , MessageBoxButtons.YesNo
        , MessageBoxIcon.Question) == DialogResult.Yes)
    {
        current = forms.Count;
        return true;
    }
    else return false;
}

/// <summary>
/// Application loop function
/// </summary>
public void Run()
{
    while (forms.ContainsKey(current))
        forms[current].ShowDialog();
}

}

Navigation example (back, next, finish and cancel button):

private void SetupEnd_FormClosing(object sender
         , FormClosingEventArgs e)
{
    e.Cancel = !setupWizard.Instance
        .wizardExit(this.DialogResult);
}

private void btBack_Click(object sender, EventArgs e)
{
    setupWizard.Instance.wizardPrev();
}

private void btNext_Click(object sender, EventArgs e)
{
    setupWizard.Instance.wizardNext();
}

private void btFinish_Click(object sender, EventArgs e)
{
    setupWizard.Instance.wizardExit(DialogResult.Abort);
}

private void btCancel_Click(object sender
        , FormClosingEventArgs e)
{
    e.Cancel = !setupWizard.Instance
        .wizardExit(DialogResult.Cancel);
}

Finally I wanted my application looks like any other install wizard (specialy those made in InnoSetup).
Here’s some screenshots and download link:

Download

Facebook comments:

Comment now!
















Trackbacks