In this example we will create a simple form with a Combo Box, which will act as a drop down list. As an addition, we will add the functionality in the code to detect when the user selects anything from the drop down menu.
To create a simple PowerShell form, we can use the following code:
# Init PowerShell Gui
Add-Type -AssemblyName System.Windows.Forms
# Create a new form
$Form1 = New-Object system.Windows.Forms.Form
# Define the size, title and background color
$Form1.ClientSize = ‘300,250’
$Form1.text = “PowerShell GUI Example”
$Form1.BackColor = “#ffffff”
# Display the form
[void]$Form1.ShowDialog()
Once we run the above code, a simple window should be displayed:
Now lets take it a bit further and add a ComboBox. As mentioned, this will be our drop down list.
# Init PowerShell Gui
Add-Type -AssemblyName System.Windows.Forms
# Create a new form
$Form1 = New-Object system.Windows.Forms.Form
# Define the size, title and background color
$Form1.ClientSize = ‘300,250’
$Form1.text = “PowerShell GUI Example”
$Form1.BackColor = “#ffffff”
$List = New-Object system.Windows.Forms.ComboBox
$List.text = “”
$List.width = 170
$List.autosize = $true
# Add the items in the dropdown list
@(‘Jack’,’Dave’,’Alex’) | ForEach-Object {[void] $List.Items.Add($_)}
# Select the default value
$List.SelectedIndex = 0
$List.location = New-Object System.Drawing.Point(70,100)
$List.Font = ‘Microsoft Sans Serif,10’
$Form1.Controls.Add($List)
# Display the form
[void]$Form1.ShowDialog()
As you can see, our list contains 3 values: Jack, Dave and Alex. Once you run the above code, you should see the following window
Next, I added a label just to demonstrate how we will later on catch the selected index of the list. The last step is to add the actual index catch:
# Init PowerShell Gui
Add-Type -AssemblyName System.Windows.Forms
# Create a new form
$Form1 = New-Object system.Windows.Forms.Form
# Define the size, title and background color
$Form1.ClientSize = ‘300,250’
$Form1.text = “PowerShell GUI Example”
$Form1.BackColor = “#ffffff”
$List = New-Object system.Windows.Forms.ComboBox
$List.text = “”
$List.width = 170
$List.autosize = $true
# Add the items in the dropdown list
@(‘Jack’,’Dave’,’Alex’) | ForEach-Object {[void] $List.Items.Add($_)}
# Select the default value
$List.SelectedIndex = 0
$List.location = New-Object System.Drawing.Point(70,100)
$List.Font = ‘Microsoft Sans Serif,10’
#Add a label
$Description = New-Object system.Windows.Forms.Label
$Description.text = “Selected index: $selected”
$Description.AutoSize = $false
$Description.width = 450
$Description.height = 50
$Description.location = New-Object System.Drawing.Point(20,50)
$Description.Font = ‘Microsoft Sans Serif,10’
#Catch changes to the list
$List.add_SelectedIndexChanged({
$selected = $List.SelectedIndex
write-host $selected
$Description.text = “Selected index: $selected”
})
$Form1.Controls.Add($List)
$Form1.Controls.Add($Description)
# Display the form
[void]$Form1.ShowDialog()
The result of the above could should be something like this: