Saturday, June 9, 2012

Creating Wizard using C# Form and Console (multiple Forms)

This is a simple implementation of Wizard using multiple forms in C#. We can use Wizard templates available but sometimes to have flexibility and control over the operations we can use console application and build workflow to display forms.

For Wizard in order to maintain all the data you can have a separate data layer which if possibly build with Singleton design pattern. Following is the basic example of how to implement workflow of Forms in a console application. It is advisable to refer other examples on net which can be more efficient.

I will appreciate if you give suggestions for more improvement. Use comment section for it.

Implementation:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml.Linq;
using System.Windows.Forms;
using System.Runtime.InteropServices;

namespace SampleExe
{
    class Program
    {
        [DllImport("user32.dll")]
        public static extern IntPtr FindWindow(string lpClassName, string lpWindowName);

        [DllImport("user32.dll")]
        static extern bool ShowWindow(IntPtr hWnd, int nCmdShow);

        [STAThread()]
        static void Main()
        {
            int iDlg = 0;
            // Hide console window
            IntPtr hWnd = FindWindow(null, Console.Title);
            if (hWnd != IntPtr.Zero)
                ShowWindow(hWnd, 0);

            // Initialize all the forms
            // For all the forms as required you need to have Next, Previous and Cancel button
            // Set Dialog Result property
            // Next button -> Yes
            // Previous button -> No
            // Cancel button -> Cancel 
            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);

            Form1 X1 = new Form1();
            Form2 X2 = new Form2();
            Form3 X3 = new Form3();
            Form4 X4 = new Form4();
            Form5 X5 = new Form5();
            DialogResult dlForm = DialogResult.Yes;
            while (dlForm != DialogResult.Cancel)
            {
                if (dlForm == DialogResult.Yes)
                {
                    iDlg++;
                    switch (iDlg)
                    {
                        case 1:
                            dlForm = X1.ShowDialog();
                            break;
                        case 2:
                            dlForm = X2.ShowDialog();
                            break;
                        case 3:
                            dlForm = X3.ShowDialog();
                            break;
                        case 4:
                            dlForm = X4.ShowDialog();
                            break;
                        case 5:
                            dlForm = X5.ShowDialog();
                            break;
                        default:
                            dlForm = DialogResult.Cancel;
                            break;
                    }
                }
                else if (dlForm == DialogResult.No)
                {
                    iDlg--;
                    switch (iDlg)
                    {
                        case 1:
                            dlForm = X1.ShowDialog();
                            break;
                        case 2:
                            dlForm = X2.ShowDialog();
                            break;
                        case 3:
                            dlForm = X3.ShowDialog();
                            break;
                        case 4:
                            dlForm = X4.ShowDialog();
                            break;
                        case 5:
                            dlForm = X5.ShowDialog();
                            break;
                        default:
                            dlForm = DialogResult.Cancel;
                            break;
                    }
                }
            }
        }
    }
}
reference:
http://stackoverflow.com/questions/2211514/suppress-command-window-from-started-command-line-application-with-net?rq=1

No comments:

Post a Comment