PowerShell In GUI Blog

PowerShell GUI Box Just In A Few Clicks

PowerShell Wizard. Round 2. It works!

with one comment

After posting the wizard sample, I thought – why don’t finish it today? I added

  • the Finish and the Cancel buttons in a way that the Finish button asks the user whether one wishes to exit now or not. The Cancel is available on the first step and by clicking this the user quits immediately
  • scriptblocks, used as a code that is run on clicking the Next button
  • fixed bugs, added comments here and there and a bit of polishing

At first, I’d like to show what the sample does.

It starts the wizard, sets the second step as the first the user sees (just for example) and provides a label saying that the wizard is awaiting for a path to any directory.

After that the user presses the next button (it stays here if the text box field is empty. The wrong path won’t be accepted too) and goes to the next step called, just for example, the Progress step:

The step called in the sample as Output contains a list box filled with the files list:

After all, at the last step can be written something encouradging, or an error log to be shown, or something else. There comes the Finish button, as well as the Cancel button comes at the first step of the wizard:

Now, after we know what this does, I can post the code running the sample:

		# Step 1: Welcome
		New-WizardStep 'Welcome' `
			'This is the first step' `
			'Welcome to the PowerShell Wizard, the our dear customer!';
		# Add a label
		# Please note that we can use the enumeration $steps which is being created runtime
		# on a call of the New-WizardStep function
		Add-ControlToStep $steps.Welcome `
			System.Windows.Forms.Label `
			'lblWelcome' 20 10 50 300 `
			'This Wizard carries you through the steps you need to collect the files from a given path';

		# Step 2
		New-WizardStep 'Input' `
			'Step Two' `
			'Here you type some in controls, plz';
		# Add a label
		Add-ControlToStep $steps.Input `
			System.Windows.Forms.Label `
			'lblInput' 20 10 20 300 `
			'Please type the path to a catalog';
		# Add a text box
		Add-ControlToStep $steps.Input `
			System.Windows.Forms.TextBox `
			'txtInput' 40 10 20 300
		# Add the code which requires that text box was not empty
		Add-CodeToStep $steps.Input `
			-StepCode {
					[string]$private:path = `
						$wzdSteps[$steps.Input].Controls['txtInput'].Text;
					if ($private:path.Length -eq 0)
					{
						# stop the step
						throw;
					}
					if (-not [System.IO.Directory]::Exists($private:path))
					{
						# stop the step
						throw;
					}
				}

		# Step 3
		New-WizardStep 'Progress' `
			'The third one' `
			'Wait, please. Sip a coffee' ;
		# Add a progress bar
		Add-ControlToStep $steps.Progress `
			'System.Windows.Forms.ProgressBar' `
			'pbDir' 200 50 100 400
		Add-CodeToStep $steps.Progress `
			-StepCode {
					# set progress bar maximum
					$wzdSteps[$steps.Progress].Controls['pbDir'].Minimum = 0;
					$wzdSteps[$steps.Progress].Controls['pbDir'].Value = 0;
					$wzdSteps[$steps.Progress].Controls['pbDir'].Maximum = `
						(Get-ChildItem $wzdSteps[$steps.Input].Controls['txtInput'].Text).Length;
					# clear the list box (from the next step)
					$wzdSteps[$steps.Output].Controls['lbxFiles'].Items.Clear();
					# add file names to the list box
					Get-ChildItem $wzdSteps[$steps.Input].Controls['txtInput'].Text | %{
							$wzdSteps[$steps.Progress].Controls['pbDir'].Value++;
							$wzdSteps[$steps.Output].Controls['lbxFiles'].Items.Add($_.Name);
						}
				}

		# Step 4
		New-WizardStep 'Output' 'Fourth' `
			'Now awake and read the output';
		# Add a list box
		Add-ControlToStep $steps.Output `
			System.Windows.Forms.ListBox `
			lbxFiles 50 50 300 400

		# Step 5: Finish
        New-WizardStep 'Finish' 'Finish!' 'Bye!';

		Initialize-Wizard;
		# Set the second step as active
		Invoke-WizardStep -Forward $true;

		$script:frmWizard.ShowDialog() | Out-Null;

The full version is posted in the box at rigth and published on the PoshCode. Ask for a doc or more comments in the code if you need to.

Advertisements

One Response

Subscribe to comments with RSS.

  1. Hi, this is a great script that I was able to customize and use at work. I do have one question though. I have been able to add a button using Add-ControlToStep, however, I have not been able to get the button to respond to an add_Click event. Do you have any suggestions on how I can get the button to respond?

    Brett

    January 26, 2016 at 11:10 pm


Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: