Drop Down menu using Windows.Forms

When creating forms and GUI through PowerShell you can use the .NET Class System.Windows.Forms. This example will describe how to make a basic dopdown form and return the selected value to PowerShell.

First we create an Array holding the DopDown values.


PS > [array]$DropDownArray = "First Choice", 
"Second Choice", 
"Third Choice"

The Array contains 3 choices. You can modify this to your own specifications or you can retrieve a list from an Internet Page, Active-Directory or whatever you want to display. Just make sure that its presented in an Array ( put [array] in front of the variable holding the Array to assure that the object will be an Array).

Next we’ll create a function that defines what will happen after the Form Choice has been made.


PS > function Return-DropDown {

	$Choice = $DropDown.SelectedItem.ToString()
	$Form.Close()
	Write-Host $Choice

}

The function Stores the Choice in a variable and converts it To a String Value, then it closes the form and finally returns the Choice made. Now all we need to do is create a Form.

Step one in creating a form is making PowerShell aware of the Windows.Forms .NET class. We do this by loading the Class into the Global Assembly Cache. We also load the System.Drawing Class since it’s useful when working with Forms.


PS > [System.Reflection.Assembly]::LoadWithPartialName("System.Windows.Forms")

PS > [System.Reflection.Assembly]::LoadWithPartialName("System.Drawing")

You can pipe it to Out-Null if you don’t want the returnvalue displayed.

Now we can create our form! First we create the body of the Form. There are alot of properties that are settable but we’ll keep it simple in this example.


PS > $Form = New-Object System.Windows.Forms.Form

PS > $Form.width = 300
PS > $Form.height = 150
PS > $Form.Text = ”DropDown”

Now that we have a body for our form we can create a Dropdown list. Here we will define Size and Location through System.Drawing. It lets us set the location based on X and Y coordinates where 0,0 is top left corner. The DropDown list is created through Windows.Forms.ComboBox.


PS > $DropDown = new-object System.Windows.Forms.ComboBox
PS > $DropDown.Location = new-object System.Drawing.Size(100,10)
PS > $DropDown.Size = new-object System.Drawing.Size(130,30)

To add our Array to the DropDown List we have to loop through it and use the Add() method on each value in the Array.


PS > ForEach ($Item in $DropDownArray) {
	$DropDown.Items.Add($Item)
}

Now we can add the DropDown List to our Form. This is done through the Controls.Add() method on the $Form object.


PS > $Form.Controls.Add($DropDown)

Adding a Label to the DropDown menu is done through Windows.Forms.Label. By changing the Location property you can move the label around. The Text property sets the Text shown in the Label.


PS > $DropDownLabel = new-object System.Windows.Forms.Label
PS > $DropDownLabel.Location = new-object System.Drawing.Size(10,10) 
PS > $DropDownLabel.size = new-object System.Drawing.Size(100,20) 
PS > $DropDownLabel.Text = "Items"
PS > $Form.Controls.Add($DropDownLabel)

Almost done, now all we need is a confirmation button that executes the function we made earlier. The button is made through Windows.Forms.Button and the the Add_Click() method is used to tell the button what to do when you click on it.


PS > $Button = new-object System.Windows.Forms.Button
PS > $Button.Location = new-object System.Drawing.Size(100,50)
PS > $Button.Size = new-object System.Drawing.Size(100,20)
PS > $Button.Text = "Select an Item"
PS > $Button.Add_Click({Return-DropDown})
PS > $form.Controls.Add($Button)

Now all we have to do is start the Form. First we activate it and then use the ShowDialog() method to display it on screen.


PS > $Form.Add_Shown({$Form.Activate()})
PS > $Form.ShowDialog()

And here is what the form looks like:

windowsforms-01

Below is the code used in this example


# Edit This item to change the DropDown Values

[array]$DropDownArray = "First Choice", "Second Choice", "Third Choice"

# This Function Returns the Selected Value and Closes the Form

function Return-DropDown {

	$Choice = $DropDown.SelectedItem.ToString()
	$Form.Close()
	Write-Host $Choice

}

[System.Reflection.Assembly]::LoadWithPartialName("System.Windows.Forms")
[System.Reflection.Assembly]::LoadWithPartialName("System.Drawing")


$Form = New-Object System.Windows.Forms.Form

$Form.width = 300
$Form.height = 150
$Form.Text = ”DropDown”

$DropDown = new-object System.Windows.Forms.ComboBox
$DropDown.Location = new-object System.Drawing.Size(100,10)
$DropDown.Size = new-object System.Drawing.Size(130,30)

ForEach ($Item in $DropDownArray) {
	$DropDown.Items.Add($Item)
}

$Form.Controls.Add($DropDown)

$DropDownLabel = new-object System.Windows.Forms.Label
$DropDownLabel.Location = new-object System.Drawing.Size(10,10) 
$DropDownLabel.size = new-object System.Drawing.Size(100,20) 
$DropDownLabel.Text = "Items"
$Form.Controls.Add($DropDownLabel)

$Button = new-object System.Windows.Forms.Button
$Button.Location = new-object System.Drawing.Size(100,50)
$Button.Size = new-object System.Drawing.Size(100,20)
$Button.Text = "Select an Item"
$Button.Add_Click({Return-DropDown})
$form.Controls.Add($Button)

$Form.Add_Shown({$Form.Activate()})
$Form.ShowDialog()

Rating 4.50 out of 5
[?]

8 thoughts on “Drop Down menu using Windows.Forms

  1. Hi, I wanted to turn this into a function that returns a value for the selection, but every time it returns from Return-Dropdown it clears the $choice variable, even though I’ve made it a global. Any ideas?

  2. Ok, can you read from an item from a drop down, that the user typed in?
    For example, if I have your list of options, and they type in a new one, can I get that info?

  3. Thank you for the walk through. I have been using powershell for a long time now. I am trying to automate some functions for our Help Desk and these type of tricks will make it much easier for me to control what they are doing.

    Excellent post

  4. Great post! I would like to pipe out the choise into a text file just to see if it works.

    Can anyone pin me in the right direction? I am fairly new in using powershell.

    Best regards

    Pierre

  5. How restrict user manually input in drop down menu, i.e. allow him only select from menu items?

  6. “Pierre on October 5, 2012 at 8:54 am said:

    Great post! I would like to pipe out the choise into a text file just to see if it works.

    Can anyone pin me in the right direction? I am fairly new in using powershell.

    Best regards

    Pierre”

    After creating the combo box you can output the selection to a variable as below;
    $A = $comboBox.SelectedItem.ToString()
    Then pipe the output to the out-file command

    $A | Out-file “c:\temp\TestOutput.txt”
    or
    $A | export-csv “c:\temp\test.csv”

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

Anti-Spam Protection by WP-SpamFree