PoshCode Logo PowerShell Code Repository

Wizard template by APetrovskiy 9 months ago
embed code: <script type="text/javascript" src="http://PoshCode.org/embed/2699"></script>download | new post

This is a template intended for creating a wizard with PowerShell code from scratch. The sample consists of two sections, the template itself and the wxample of its use.
The 562th line (’# Step 1: Welcome’) is now a border. All the above is the template, the remainder is how to use the template. Good luck, guys!

  1. #######################################################################################################################
  2. # File:             wizard_template.ps1                                                                                                   #
  3. # Author:           Alexander Petrovskiy                                                                              #
  4. # Publisher:        Alexander Petrovskiy, PowerShellDevTools.WordPress.Com                                            #
  5. # Copyright:        © 2010-2011 Alexander Petrovskiy, PowerShellDevTools.WordPress.Com. All rights reserved.          #
  6. # Usage:            To run this example no preparations needed.                                                                                                           #
  7. #                                       There also are the following settings you may be intersted in:                                                                    #
  8. #                   1. the wizard caption                                                                                 #
  9. #                   2. default size of the wizard                                                                                             #
  10. #                   3. default size of the wizard's buttons                                                                           #
  11. #                   4. button labels                                                                                                                      #
  12. #                   5. right-to-left layout                                                                                                                           #
  13. #                   All the settings mentioned are placed inside the 'adjustable settings' region                     #
  14. #                   Please provide feedback in the PowerShellDevTools.WordPress.Com blog.                             #
  15. #######################################################################################################################
  16. cls
  17. Set-StrictMode -Version 2
  18. #region host preparations
  19. if ($Host.Name -eq 'ConsoleHost')
  20. {
  21.         Add-Type -AssemblyName System.Windows.Forms;
  22.         Add-Type -AssemblyName System.Drawing;
  23. }
  24. #endregion host preparations
  25. #region the resulting wizard
  26.         #region adjustable settings
  27.                 #region controls settings
  28.                 # Form size and caption
  29.                 [string]$script:constWizardInitialCaption = `
  30.                         'This is a sample wizard';
  31.                 [int]$script:constWizardWidth = `
  32.                         [System.Windows.Forms.SystemInformation]::VirtualScreen.Width / 2;
  33.                 [int]$script:constWizardHeight = `
  34.                         [System.Windows.Forms.SystemInformation]::VirtualScreen.Height / 2;
  35.                 # Buttons Next, Back, Cancel, Finish
  36.                 [int]$script:constButtonHeight = 23;
  37.                 [int]$script:constButtonWidth = 75;
  38.                 [int]$script:constButtonAreaHeight = `
  39.                         $script:constButtonHeight * 2;
  40.                 [string]$script:constButtonNextName = '&Next';
  41.                 [string]$script:constButtonBackName = '&Back';
  42.                 [string]$script:constButtonFinishName = '&Finish';
  43.                 [string]$script:constButtonCancelName = '&Cancel';
  44.                 # Step display name and description
  45.                 [int]$script:constLabelStepDisplayNameLeft = 5;
  46.                 [int]$script:constLabelStepDisplayNameTop = 0;
  47.                 [float]$script:constLabelStepDisplayNameFontSize = 16;
  48.                 [int]$script:constLabelStepDescriptionLeft = 5;
  49.                 [int]$script:constLabelStepDescriptionTop = 30;
  50.                 # Form properties
  51.                 [bool]$script:constWizardRigthToLeft = $false;
  52.                 # Initial step number
  53.                 [int]$script:currentStep = 0;
  54.                 #endregion controls settings
  55.         #endregion adjustable settings
  56.         #region mandatory settings
  57.                 #region Initialization of the SplitContainer controls
  58.                 # The outer split container
  59.                 [System.Windows.Forms.SplitContainer]$script:splitOuter = `
  60.                         New-Object System.Windows.Forms.SplitContainer;
  61.                         $script:splitOuter.Dock = [System.Windows.Forms.DockStyle]::Fill;
  62.                         $script:splitOuter.IsSplitterFixed = $true;
  63.                         $script:splitOuter.Orientation = `
  64.                                 [System.Windows.Forms.Orientation]::Horizontal;
  65.                         $script:splitOuter.SplitterWidth = 5;
  66.                 # The inner split container
  67.                 [System.Windows.Forms.SplitContainer]$script:splitInner = `
  68.                         New-Object System.Windows.Forms.SplitContainer;
  69.                         $script:splitInner.Dock = [System.Windows.Forms.DockStyle]::Fill;
  70.                         $script:splitInner.IsSplitterFixed = $true;
  71.                         $script:splitInner.Orientation = `
  72.                                 [System.Windows.Forms.Orientation]::Horizontal;
  73.                         $script:splitInner.SplitterWidth = 5;
  74.                         $script:splitOuter.Panel1.Controls.Add($script:splitInner);
  75.                 # The labels for the curent step name and description
  76.                 [System.Windows.Forms.Label]$script:lblStepDisplayName = `
  77.                         New-Object System.Windows.Forms.Label;
  78.                         $script:lblStepDisplayName.Left = `
  79.                                 $script:constLabelStepDisplayNameLeft;
  80.                         $script:lblStepDisplayName.Top = `
  81.                                 $script:constLabelStepDisplayNameTop;
  82.                         [System.Drawing.Font]$private:font = `
  83.                                 $script:lblStepDisplayName.Font;
  84.                         $private:font = `
  85.                                 New-Object System.Drawing.Font($private:font.Name, `
  86.                                                 $script:constLabelStepDisplayNameFontsize, `
  87.                                     $private:font.Style, $private:font.Unit, `
  88.                                     $private:font.GdiCharSet, $private:font.GdiVerticalFont );
  89.                         $script:lblStepDisplayName.Font = $private:font;
  90.                 [System.Windows.Forms.Label]$script:lblStepDescription = `
  91.                         New-Object System.Windows.Forms.Label;
  92.                         $script:lblStepDescription.Left = `
  93.                                 $script:constLabelStepDescriptionLeft;
  94.                         $script:lblStepDescription.Top = `
  95.                                 $script:constLabelStepDescriptionTop;
  96.                 $script:splitInner.Panel1.Controls.AddRange(($script:lblStepDisplayName, `
  97.                         $script:lblStepDescription));
  98.                 #endregion Initialization of the SplitContainer controls
  99.                 #region buttons functions
  100.                 function Enable-FinishButton
  101.                 {
  102.                         $script:btnNext.Enabled = $false;
  103.                         $script:btnNext.Visible = $false;
  104.                         $script:btnFinish.Enabled = $true;
  105.                         $script:btnFinish.Visible = $true;
  106.                 }
  107.                 function Enable-NextButton
  108.                 {
  109.                         $script:btnNext.Enabled = $true;
  110.                         $script:btnNext.Visible = $true;
  111.                         $script:btnFinish.Enabled = $false;
  112.                         $script:btnFinish.Visible = $false;
  113.                 }
  114.                 function Enable-BackButton
  115.                 {
  116.                         $script:btnBack.Enabled = $true;
  117.                         $script:btnBack.Visible = $true;
  118.                         $script:btnCancel.Enabled = $false;
  119.                         $script:btnCancel.Visible = $false;
  120.                 }
  121.                 function Enable-CancelButton
  122.                 {
  123.                         $script:btnBack.Enabled = $false;
  124.                         $script:btnBack.Visible = $false;
  125.                         $script:btnCancel.Enabled = $true;
  126.                         $script:btnCancel.Visible = $true;
  127.                 }
  128.                 #endregion buttons functions
  129.                 #region the Next and Back buttons
  130.                 [System.Windows.Forms.Button]$script:btnNext = `
  131.                         New-Object System.Windows.Forms.Button;
  132.                 $script:btnNext.Anchor = [System.Windows.Forms.AnchorStyles]::Bottom -bor `
  133.                         [System.Windows.Forms.AnchorStyles]::Right -bor `
  134.                         [System.Windows.Forms.AnchorStyles]::Top;
  135.                 $script:btnNext.Text = $script:constButtonNextName;
  136.                 [System.Windows.Forms.Button]$script:btnBack = `
  137.                         New-Object System.Windows.Forms.Button;
  138.                 $script:btnBack.Anchor = [System.Windows.Forms.AnchorStyles]::Bottom -bor `
  139.                         [System.Windows.Forms.AnchorStyles]::Right -bor `
  140.                         [System.Windows.Forms.AnchorStyles]::Top;
  141.                 $script:btnBack.Text = $script:constButtonBackName;
  142.                 $script:splitOuter.Panel2.Controls.AddRange(($script:btnBack, $script:btnNext));
  143.                 #endregion the Next and Back buttons
  144.                 #region the Finish and the Cancel buttons
  145.                 [System.Windows.Forms.Button]$script:btnFinish = `
  146.                         New-Object System.Windows.Forms.Button;
  147.                 $script:btnFinish.Anchor = [System.Windows.Forms.AnchorStyles]::Bottom -bor `
  148.                         [System.Windows.Forms.AnchorStyles]::Right -bor `
  149.                         [System.Windows.Forms.AnchorStyles]::Top;
  150.                 $script:btnFinish.Text = $script:constButtonFinishName;
  151.                 [System.Windows.Forms.Button]$script:btnCancel = `
  152.                         New-Object System.Windows.Forms.Button;
  153.                 $script:btnCancel.Anchor = [System.Windows.Forms.AnchorStyles]::Bottom -bor `
  154.                         [System.Windows.Forms.AnchorStyles]::Right -bor `
  155.                         [System.Windows.Forms.AnchorStyles]::Top;
  156.                 $script:btnCancel.Text = $script:constButtonCancelName;
  157.                 Enable-CancelButton;
  158.                 Enable-NextButton;
  159.                 $script:splitOuter.Panel2.Controls.AddRange(($script:btnCancel, $script:btnFinish));
  160.                 #endregion the Finish and the Cancel buttons
  161.                 #region Initialization of the main form
  162.                 $script:frmWizard = $null;
  163.                 [System.Windows.Forms.Form]$script:frmWizard = `
  164.                         New-Object System.Windows.Forms.Form;
  165.                 $script:frmWizard.Controls.Add($script:splitOuter);
  166.                 # left-to-right and right-to-left
  167.                 if ($script:constWizardRigthToLeft)
  168.                 {
  169.                         $script:frmWizard.RightToLeft = `
  170.                                 [System.Windows.Forms.RightToLeft]::Yes;
  171.                         $script:frmWizard.RightToLeftLayout = $true;
  172.                 }
  173.                 else
  174.                 {
  175.                         $script:frmWizard.RightToLeft = `
  176.                                 [System.Windows.Forms.RightToLeft]::No;
  177.                         $script:frmWizard.RightToLeftLayout = $false;
  178.                 }
  179.                 $script:frmWizard.Text = $script:constWizardInitialCaption;
  180.                 #endregion Initialization of the main form
  181.         #endregion mandatory settings
  182.         #region the Wizard steps
  183.         [System.Collections.ArrayList]$script:wzdSteps = `
  184.                 New-Object System.Collections.ArrayList;
  185.                 # Here we create an 'enumeration' (PSObject)
  186.                 # and begin filling ArrayList $script:wzdSteps with Panel controls
  187.                 [System.EventHandler]$script:hndlRunControlsAdd = `
  188.                         {try{$script:splitInner.Panel2.Controls.Add($script:wzdSteps[$script:currentStep]);}
  189.                         catch{Write-Debug $Error[0]; Write-Debug $global:Error[0];}};
  190.                 #region function New-WizardStep
  191.                 function New-WizardStep
  192.                 {
  193.                         param(
  194.                                   [string]$StepName,
  195.                                   [string]$StepDisplayName,
  196.                                   [string]$StepDescription = ''
  197.                                   )
  198.                         # Storing parameters in step arrays
  199.                         Add-Member -InputObject $script:steps -MemberType NoteProperty `
  200.                                 -Name $StepName -Value $script:wzdSteps.Count;
  201.                         $null = $script:stepDisplayNames.Add($StepDisplayName);
  202.                         $null = $script:stepDescriptions.Add($StepDescription);
  203.                         # Adding a fake scriptblock to keep counters
  204.                         $null = $script:stepscriptblocks.Add({});
  205.                         # Create and add the new step's panel to the array
  206.                         [System.Windows.Forms.Panel]$private:panel = `
  207.                                 New-Object System.Windows.Forms.Panel;
  208.                         $null = $script:wzdSteps.Add($private:panel);
  209.                         $script:currentStep = $script:wzdSteps.Count - 1;
  210.  
  211.                         $script:splitInner.Panel2.Controls.Add($script:wzdSteps[$script:currentStep]);
  212.  
  213.                         $script:wzdSteps[$script:currentStep].Dock = `
  214.                                 [System.Windows.Forms.DockStyle]::Fill;
  215.                         # To restore initial state for this code running before the user accesses the wizard.
  216.                         $script:currentStep = 0;
  217.                 }
  218.                 #endregion function New-WizardStep
  219.                 #region function Add-ControlToStep
  220.                 function Add-ControlToStep
  221.                 # Adds a control of selected type to a wizard step
  222.                 {
  223.                         param(
  224.                                   [Parameter(Mandatory=$true)]
  225.                                   [string]$StepNumber,
  226.                                   [Parameter(Mandatory=$true)]
  227.                                   [string]$ControlType,
  228.                                   [Parameter(Mandatory=$true)]
  229.                                   [string]$ControlName,
  230.                                   [int]$ControlTop,
  231.                                   [int]$ControlLeft,
  232.                                   [int]$ControlHeight,
  233.                                   [int]$ControlWidth,
  234.                                   [string]$ControlData
  235.                                  )
  236.                         $private:ctrl = $null;
  237.                         try{
  238.                                 $private:ctrl = New-Object $ControlType;
  239.                         }catch{Write-Error "Unable to create a control of $($ControlType) type";}
  240.                         try{
  241.                                 $private:ctrl.Name = $ControlName;
  242.                         }catch{Write-Error "Unable to set the Name property with value $($ControlName) to a control of the $($ControlType) type";}
  243.                         try{
  244.                                 $private:ctrl.Top = $ControlTop;
  245.                         }catch{Write-Error "Unable to set the Top property with value $($ControlTop) to a control of the $($ControlType) type";}
  246.                         try{
  247.                                 $private:ctrl.Left = $ControlLeft;
  248.                         }catch{Write-Error "Unable to set the Left property with value $($ControlLeft) to a control of the $($ControlType) type";}
  249.                         try{
  250.                                 $private:ctrl.Height = $ControlHeight;
  251.                         }catch{Write-Error "Unable to set the Height property with value $($ControlHeight) to a control of the $($ControlType) type";}
  252.                         try{
  253.                                 $private:ctrl.Width = $ControlWidth;
  254.                         }catch{Write-Error "Unable to set the Width property with value $($ControlWidth) to a control of the $($ControlType) type";}
  255.                         try{
  256.                                 $private:ctrl.Text = $ControlData;
  257.                         }catch{Write-Error "Unable to set the Text property with value $($ControlData) to a control of the $($ControlType) type";}
  258.                         try{
  259.                                 $wzdSteps[$StepNumber].Controls.Add($private:ctrl);
  260.                         }catch{Write-Error "Unable to add a control of $($ControlType) type to the step $($StepNumber)";}
  261.                 }
  262.                 #endregion function Add-ControlToStep
  263.                 #region function Add-CodeToStep
  264.                 function Add-CodeToStep
  265.                 # Adds a scriptblock to a wizard step
  266.                 {
  267.                         param(
  268.                                   [Parameter(Mandatory=$true)]
  269.                                   [string]$StepNumber,
  270.                                   [Parameter(Mandatory=$true)]
  271.                                   [scriptblock]$StepCode
  272.                                  )
  273.                         $script:stepScriptblocks[$StepNumber] = $StepCode;
  274.                 }
  275.                 #endregion function Add-CodeToStep
  276.                 #region Step data arrays
  277.                 [psobject]$script:steps = New-Object psobject;
  278.                 [System.Collections.ArrayList]$script:stepDisplayNames = `
  279.                         New-Object System.Collections.ArrayList;
  280.                 [System.Collections.ArrayList]$script:stepDescriptions = `
  281.                         New-Object System.Collections.ArrayList;
  282.                 [System.Collections.ArrayList]$script:stepScriptblocks = `
  283.                         New-Object System.Collections.ArrayList;
  284.                 #endregion Step data arrays
  285.         #endregion the Wizard steps
  286.         #region events of the wizard controls
  287.                 #region resizing
  288.                         #region button sizes
  289.         function Set-NextButtonLeft
  290.         {
  291.                 param([int]$Left)
  292.                 $script:btnNext.Left = $Left;
  293.                 $script:btnFinish.Left = $Left;
  294.         }
  295.         function Set-NextButtonTop
  296.         {
  297.                 param([int]$Top)
  298.                 $script:btnNext.Top = $Top;
  299.                 $script:btnFinish.Top = $Top;
  300.         }
  301.         function Set-BackButtonLeft
  302.         {
  303.                 param([int]$Left)
  304.                 $script:btnBack.Left = $Left;
  305.                 $script:btnCancel.Left = $Left;
  306.         }
  307.         function Set-BackButtonTop
  308.         {
  309.                 param([int]$Top)
  310.                 $script:btnBack.Top = $Top;
  311.                 $script:btnCancel.Top = $Top;
  312.         }
  313.                         #endregion button sizes
  314.                         #region function Initialize-WizardControls
  315.         function Initialize-WizardControls
  316.         <#
  317.                 .SYNOPSIS
  318.                         The Initialize-WizardControls function sets the wizard common controls to predefined positions.
  319.        
  320.                 .DESCRIPTION
  321.                         The Initialize-WizardControls function does the following:
  322.                         - sets Top, Left, Width and Height properties of the wizard buttons
  323.                         - positions of the step labels
  324.                         - sets the SplitterDistance property of both Splitcontainer controls
  325.        
  326.                 .EXAMPLE
  327.                         PS C:\> Initialize-WizardControls
  328.                         This example shows how to step the wizard forward.
  329.        
  330.                 .INPUTS
  331.                         No input
  332.        
  333.                 .OUTPUTS
  334.                         No output
  335.         #>
  336.         {
  337.                 # Set sizes of buttons
  338.                 $script:btnNext.Height = $script:constButtonHeight;
  339.                 $script:btnNext.Width = $script:constButtonWidth;
  340.                 $script:btnBack.Height = $script:constButtonHeight;
  341.                 $script:btnBack.Width = $script:constButtonWidth;
  342.                 $script:btnFinish.Height = $script:btnNext.Height;
  343.                 $script:btnFinish.Width = $script:btnNext.Width;
  344.                 $script:btnCancel.Height = $script:btnBack.Height;
  345.                 $script:btnCancel.Width = $script:btnBack.Width;
  346.                 # SplitterDistance of the outer split container
  347.                 # in other words, the area where Next and Back buttons are placed
  348.                 $script:splitOuter.SplitterDistance = `
  349.                         $script:splitOuter.Height - `
  350.                         $script:constButtonAreaHeight;
  351.                
  352.                 # Placements of the buttons
  353.                 if ($script:constWizardRigthToLeft)
  354.                 {
  355.                         Set-NextButtonLeft 10;
  356.                         Set-BackButtonLeft ($script:constButtonWidth + 20);
  357.                 }
  358.                 else
  359.                 {
  360.                         Set-NextButtonLeft ($script:splitOuter.Width - `
  361.                                 $script:constButtonWidth - 10);
  362.                         Set-BackButtonLeft ($script:splitOuter.Width - `
  363.                                 $script:constButtonWidth - `
  364.                                 $script:constButtonWidth - 20);
  365.                 }
  366.                 Set-NextButtonTop (($script:constButtonAreaHeight - $script:constButtonHeight) / 2);
  367.                 Set-BackButtonTop (($script:constButtonAreaHeight - $script:constButtonHeight) / 2);
  368.                
  369.                 # SplitterDistance of the inner split container
  370.                 # this is the place where step name is placed
  371.                 $script:splitInner.SplitterDistance = `
  372.                         $script:constButtonAreaHeight * 1.5;
  373.                
  374.                 # Step Display Name and Description labels
  375.                 $script:lblStepDisplayName.Width = `
  376.                         $script:splitInner.Panel1.Width - `
  377.                         $script:constLabelStepDisplayNameLeft * 2;
  378.                 $script:lblStepDescription.Width = `
  379.                         $script:splitInner.Panel1.Width - `
  380.                         $script:constLabelStepDescriptionLeft * 2;
  381.                        
  382.                 # Refresh after we have changed placements of the controls
  383.                 [System.Windows.Forms.Application]::DoEvents();
  384.         }
  385.                         #endregion function Initialize-WizardControls
  386.         [System.EventHandler]$script:hndlFormResize = {Initialize-WizardControls;}
  387.         [System.EventHandler]$script:hndlFormLoad = {Initialize-WizardControls;}
  388.         #[System.Windows.Forms.MouseEventHandler]$script:hndlSplitMouseMove = `
  389.         #       {Initialize-WizardControls;}
  390.         # Initial arrange on Load form.
  391.         $script:frmWizard.add_Load($script:hndlFormLoad);
  392.         #$script:frmWizard.add_Resize($script:hndlFormResize);
  393.         $script:splitOuter.add_Resize($script:hndlFormResize);
  394.                 #endregion resizing
  395.                 #region steps
  396.                         #region function Invoke-WizardStep
  397.         function Invoke-WizardStep
  398.         <#
  399.                 .SYNOPSIS
  400.                         The Invoke-WizardStep function sets active panel on the wizard form.
  401.        
  402.                 .DESCRIPTION
  403.                         The Invoke-WizardStep function does the following:
  404.                         - changes internal variable $script:currentStep
  405.                         - sets/resets .Enabled property of btnNext and btnBack
  406.                         - changes .Dock and .Left properties of every panel
  407.        
  408.                 .PARAMETER  Forward
  409.                         The optional parameter Forward is used to point out the direction the wizard goes.
  410.        
  411.                 .EXAMPLE
  412.                         PS C:\> Invoke-WizardStep -Forward $true
  413.                         This example shows how to step the wizard forward.
  414.        
  415.                 .INPUTS
  416.                         System.Boolean
  417.        
  418.                 .OUTPUTS
  419.                         No output
  420.         #>
  421.         {
  422.                 [CmdletBinding()]
  423.                 param(
  424.                           [Parameter(Mandatory=$false)]
  425.                           [bool]$Forward = $true
  426.                           )
  427.                 Begin{}
  428.                 Process{
  429.                         # run the step code
  430.                         try{
  431.                                 $script:stepScriptblocks[$script:currentStep].Invoke();
  432.                         } catch {return;} #Cancel step
  433.                         if ($Forward)
  434.                         {
  435.                                 Enable-BackButton;
  436.                                 if ($script:currentStep -lt ($script:wzdSteps.Count - 1))
  437.                                 {$script:currentStep++;}
  438.                                 if ($script:currentStep -lt ($script:wzdSteps.Count - 1))
  439.                                 {
  440.                                         # step forward but not the last step
  441.                                         Enable-NextButton;
  442.                                 }
  443.                                 else
  444.                                 {      
  445.                                         # the last step
  446.                                         Enable-FinishButton;
  447.                                 }
  448.                         }
  449.                         else
  450.                         {
  451.                                 # go backward
  452.                                 Enable-NextButton;
  453.                                 if ($script:currentStep -gt 0)
  454.                                 {$script:currentStep--;}
  455.                                 if ($script:currentStep -gt 0)
  456.                                 {
  457.                                         # step backward but not the first one
  458.                                         Enable-BackButton;
  459.                                 }
  460.                                 else
  461.                                 {
  462.                                         # the first step
  463.                                         Enable-CancelButton;
  464.                                 }
  465.                         }              
  466.                         for($private:i = 0; $private:i -lt $script:wzdSteps.Count;
  467.                                 $private:i++)
  468.                         {
  469.                                 if ($private:i -ne $script:currentStep)
  470.                                 {      
  471.                                         $script:wzdSteps[$private:i].Dock = `
  472.                                                 [System.Windows.Forms.DockStyle]::None;
  473.                                         $script:wzdSteps[$private:i].Left = 10000;
  474.                                 }
  475.                                 else
  476.                                 {
  477.                                         $script:wzdSteps[$private:i].Dock = `
  478.                                                 [System.Windows.Forms.DockStyle]::Fill;
  479.                                         $script:wzdSteps[$private:i].Left = 0;
  480.                                 }
  481.                         }
  482.                         $script:lblStepDisplayName.Text = `
  483.                                 $script:stepDisplayNames[$script:currentStep];
  484.                         $script:lblStepDescription.Text = `
  485.                                 $script:stepDescriptions[$script:currentStep];
  486.                 }
  487.                 End{}
  488.         }
  489.                         #endregion function Invoke-WizardStep
  490.                         #region function Initialize-WizardStep
  491.         function Initialize-WizardStep
  492.         # This is the selfsufficient function doing all the necessary
  493.         # calculations for controls on each panel.
  494.         # Also from the code can be seen how to address the panel you are interesting in
  495.         # using the 'enumeration' created earlier
  496.         # for example, $script:wzdSteps[$script:steps.Welcome]
  497.         {
  498.                 $script:lblStepDisplayName.Text = `
  499.                         $script:stepDisplayNames[$script:currentStep];
  500.                 $script:lblStepDescription.Text = `
  501.                         $script:stepDescriptions[$script:currentStep];
  502.         }
  503.                         #endregion function Initialize-WizardStep
  504.                         #region $hndlStepForward
  505.         [System.EventHandler]$hndlStepForward = {
  506.                 # serve controls' data
  507.                 Initialize-WizardStep;
  508.                 # switch panels
  509.                 Invoke-WizardStep $true;
  510.         }
  511.                         #endregion $hndlStepForward
  512.                         #region $hndlStepBack
  513.         [System.EventHandler]$hndlStepBack = {
  514.                 # switch panels
  515.                 Invoke-WizardStep $false;
  516.         }
  517.                         #endregion $hndlStepBack
  518.                         #region $hndlFinish
  519.         [System.EventHandler]$hndlFinish = {
  520.                 # ask for exit
  521.                 [System.Windows.Forms.DialogResult]$private:res = `
  522.                         [System.Windows.Forms.MessageBox]::Show("Are you sure?", `
  523.                                         "Question on exit", `
  524.                                         [System.Windows.Forms.MessageBoxButtons]::YesNo);
  525.                 if ($res -eq [System.Windows.Forms.DialogResult]::Yes){
  526.                         # close the wizard
  527.                         $frmWizard.Close();
  528.                 }
  529.         }
  530.                         #endregion $hndlFinish
  531.                         #region $hndlCancel
  532.         [System.EventHandler]$hndlCancel = {
  533.                 # close the wizard
  534.                 $frmWizard.Close();
  535.         }
  536.                         #endregion $hndlCancel
  537.                         #region wizard buttons' clicks
  538.         $script:btnNext.add_Click($script:hndlStepForward);
  539.         $script:btnBack.add_Click($script:hndlStepBack);
  540.         $script:btnFinish.add_Click($script:hndlFinish);
  541.         $script:btnCancel.add_Click($script:hndlCancel);
  542.                         #endregion wizard buttons' clicks
  543.                 #endregion steps
  544.         #endregion events of the wizard controls
  545.         #region wizard initialization
  546.                 #region function Initialize-Wizard
  547.         function Initialize-Wizard
  548.         # This is one more selfsufficient function written to make
  549.         # the latest preparations for the form run
  550.         {
  551.                 #region control settings
  552.                 $script:frmWizard.Width = $script:constWizardWidth;
  553.                 $script:frmWizard.Height = $script:constWizardHeight;
  554.                 #endregion control settings
  555.                 Initialize-WizardStep;
  556.         }
  557.                 #endregion function Initialize-Wizard
  558.         #endregion wizard initialization
  559. #endregion the resulting wizard
  560.  
  561.  
  562.                 # Step 1: Welcome
  563.                 New-WizardStep 'Welcome' `
  564.                         'This is the first step' `
  565.                         'Welcome to the PowerShell Wizard, the our dear customer!';
  566.                 # Add a label
  567.                 # Please note that we can use the enumeration $steps which is being created runtime
  568.                 # on a call of the New-WizardStep function
  569.                 Add-ControlToStep $steps.Welcome `
  570.                         System.Windows.Forms.Label `
  571.                         'lblWelcome' 20 10 50 300 `
  572.                         'This Wizard carries you through the steps you need to collect the files from a given path';
  573.                
  574.                 # Step 2
  575.                 New-WizardStep 'Input' `
  576.                         'Step Two' `
  577.                         'Here you type some in controls, plz';
  578.                 # Add a label
  579.                 Add-ControlToStep $steps.Input `
  580.                         System.Windows.Forms.Label `
  581.                         'lblInput' 20 10 20 300 `
  582.                         'Please type the path to a catalog';
  583.                 # Add a text box
  584.                 Add-ControlToStep $steps.Input `
  585.                         System.Windows.Forms.TextBox `
  586.                         'txtInput' 40 10 20 300
  587.                 # Add the code which requires that text box was not empty
  588.                 Add-CodeToStep $steps.Input `
  589.                         -StepCode {
  590.                                         [string]$private:path = `
  591.                                                 $wzdSteps[$steps.Input].Controls['txtInput'].Text;
  592.                                         if ($private:path.Length -eq 0)
  593.                                         {
  594.                                                 # stop the step
  595.                                                 throw;
  596.                                         }
  597.                                         if (-not [System.IO.Directory]::Exists($private:path))
  598.                                         {
  599.                                                 # stop the step
  600.                                                 throw;
  601.                                         }
  602.                                 }
  603.  
  604.                 # Step 3
  605.                 New-WizardStep 'Progress' `
  606.                         'The third one' `
  607.                         'Wait, please. Sip a coffee' ;
  608.                 # Add a progress bar
  609.                 Add-ControlToStep $steps.Progress `
  610.                         'System.Windows.Forms.ProgressBar' `
  611.                         'pbDir' 200 50 100 400
  612.                 Add-CodeToStep $steps.Progress `
  613.                         -StepCode {
  614.                                         # set progress bar maximum
  615.                                         $wzdSteps[$steps.Progress].Controls['pbDir'].Minimum = 0;
  616.                                         $wzdSteps[$steps.Progress].Controls['pbDir'].Value = 0;
  617.                                         $wzdSteps[$steps.Progress].Controls['pbDir'].Maximum = `
  618.                                                 (Get-ChildItem $wzdSteps[$steps.Input].Controls['txtInput'].Text).Length;
  619.                                         # clear the list box (from the next step)
  620.                                         $wzdSteps[$steps.Output].Controls['lbxFiles'].Items.Clear();
  621.                                         # add file names to the list box
  622.                                         Get-ChildItem $wzdSteps[$steps.Input].Controls['txtInput'].Text | %{
  623.                                                         $wzdSteps[$steps.Progress].Controls['pbDir'].Value++;
  624.                                                         $wzdSteps[$steps.Output].Controls['lbxFiles'].Items.Add($_.Name);
  625.                                                 }
  626.                                 }
  627.                
  628.                 # Step 4
  629.                 New-WizardStep 'Output' 'Fourth' `
  630.                         'Now awake and read the output';
  631.                 # Add a list box
  632.                 Add-ControlToStep $steps.Output `
  633.                         System.Windows.Forms.ListBox `
  634.                         lbxFiles 50 50 300 400
  635.        
  636.                 # Step 5: Finish
  637.         New-WizardStep 'Finish' 'Finish!' 'Bye!';
  638.                                
  639.                 Initialize-Wizard;
  640.                 # Set the second step as active
  641.                 Invoke-WizardStep -Forward $true;
  642.                                
  643.                 $script:frmWizard.ShowDialog() | Out-Null;

Submit a correction or amendment below (
click here to make a fresh posting)
After submitting an amendment, you'll be able to view the differences between the old and new posts easily.

Syntax highlighting:


Remember me