Powershell adding action to combobox selection
at the moment, i want to build a powershell gui. the form is running fine, so there are no problems with it. now i want to add an event for my selection, for example starting another script choosing "item1" and another script choosing "item2" and so on. how can i achieve that? thanfs for any help. how can i add a second combobox? This is what i have actually.
[void] [System.Reflection.Assembly]::LoadWithPartialName("System.Windows.Forms")
[void] [System.Reflection.Assembly]::LoadWithPartialName("System.Drawing")
$objForm = New-Object System.Windows.Forms.Form
$objForm.Text = "test gui"
$objForm.Size = New-Object System.Drawing.Size(300,200)
$objForm.StartPosition = "CenterScreen"
$objForm.KeyPreview = $True
$objForm.Add_KeyDown({if ($_.KeyCode -eq "Enter")
{
foreach ($objItem in $objCombobox.SelectedItem)
{$x += $objItem}
$objForm.Close()
}
})
$objForm.Add_KeyDown({if ($_.KeyCode -eq "Escape")
{$objForm.Close()}})
$OKButton = New-Object System.Windows.Forms.Button
$OKButton.Location = New-Object System.Drawing.Size(75,120)
$OKButton.Size = New-Object System.Drawing.Size(75,23)
$OKButton.Text = "OK"
$OKButton.Add_Click(
{
foreach ($objItem in $objCombobox.SelectedItem)
{$x += $objItem}
$objForm.Close()
})
$objForm.Controls.Add($OKButton)
$CancelButton = New-Object System.Windows.Forms.Button
$CancelButton.Location = New-Object System.Drawing.Size(150,120)
$CancelButton.Size = New-Object System.Drawing.Size(75,23)
$CancelButton.Text = "Cancel"
$CancelButton.Add_Click({$objForm.Close()})
$objForm.Controls.Add($CancelButton)
$objLabel = New-Object System.Windows.Forms.Label
$objLabel.Location = New-Object System.Drawing.Size(10,20)
$objLabel.Size = New-Object System.Drawing.Size(280,20)
$objLabel.Text = "please choose"
$objForm.Controls.Add($objLabel)
$objCombobox = New-Object System.Windows.Forms.Combobox
$objCombobox.Location = New-Object System.Drawing.Size(10,40)
$objCombobox.Size = New-Object System.Drawing.Size(260,20)
[void] $objCombobox.Items.Add("Item 1")
[void] $objCombobox.Items.Add("Item 2")
[void] $objCombobox.Items.Add("Item 3")
[void] $objCombobox.Items.Add("Item 4")
[void] $objCombobox.Items.Add("Item 5")
$objCombobox.Height = 70
$objForm.Controls.Add($objCombobox)
$objForm.Topmost = $True
$objForm.Add_Shown({$objForm.Activate()})
[void] $objForm.ShowDialog()
$x
Please also mark the comments that contributed to the solution of the article
Content-ID: 661830
Url: https://administrator.de/contentid/661830
Printed on: November 13, 2024 at 20:11 o'clock
5 Comments
Latest comment
Add-Type -A System.Windows.Forms
Add-Type -A System.Drawing
function Get-UserInput ([string[]]$items) {
$objForm = New-Object System.Windows.Forms.Form -Property @{
Text = "test gui"
Size = '300,200'
StartPosition = "CenterScreen"
Topmost = $True
KeyPreview = $True
add_KeyDown = {
switch -Regex ($_.KeyCode.toString()){
'Enter|Escape' {$objForm.Close()}
}
}
add_Shown = {$objForm.Activate()}
}
$OKButton = New-Object System.Windows.Forms.Button -Property @{
Location = '75,120'
Size = '75,23'
Text = "OK"
add_Click = {
$objForm.DialogResult = 'OK'
$objForm.Close()
}
}
$CancelButton = New-Object System.Windows.Forms.Button -Property @{
Location = '150,120'
Size = '75,23'
Text = "Cancel"
Add_Click = {
$objForm.DialogResult = 'Cancel'
$objForm.Close()
}
}
$objLabel = New-Object System.Windows.Forms.Label -Property @{
Location = '10,20'
Size = '280,20'
Text = "please choose"
}
$objCombobox = New-Object System.Windows.Forms.Combobox -Property @{
Location = '10,40'
Size = '260,20'
}
$objCombobox2 = New-Object System.Windows.Forms.Combobox -Property @{
Location = '10,70'
Size = '260,20'
}
[void]$objCombobox2.Items.Add("Whatever you want here")
[void]$objCombobox.Items.AddRange(@($items))
$objForm.Controls.AddRange(@($OKButton,$CancelButton,$objLabel,$objCombobox,$objCombobox2))
[void] $objForm.ShowDialog()
if ($objForm.DialogResult -eq 'OK'){
return $objCombobox.SelectedItem
}
}
$list = [ordered]@{
'Item 1' = 'D:\script1.ps1'
'Item 2' = 'D:\script2.ps1'
'Item 3' = 'D:\script3.ps1'
'Item 4' = 'D:\script4.ps1'
}
$userinput = (Get-UserInput $list.Keys)
if ($userinput){
"Do something with " + $list.Item($userinput)
}
No big problem, just place the switch condition directly inside the OK Button-Click Event and do "not" set AcceptButton Property (which would normaly close the form automatically), then the form remains open until you either close the form by the x or the cancel button.
Finally please mark the thread as "solved", thanks.
Regards SK
Add-Type -AssemblyName System.Windows.Forms
Add-Type -AssemblyName System.Drawing
$objForm = New-Object System.Windows.Forms.Form
$objForm.Text = "Combobox"
$objForm.Size = New-Object System.Drawing.Size(300,200)
$objForm.StartPosition = "CenterScreen"
$objForm.KeyPreview = $True
$objForm.Topmost = $True
$OKButton = New-Object System.Windows.Forms.Button
$OKButton.Location = New-Object System.Drawing.Size(75,120)
$OKButton.Size = New-Object System.Drawing.Size(75,23)
$OKButton.Text = "Run script"
$OKButton.add_Click({
switch ($objCombobox.SelectedItem) {
"Item 1" { [System.Windows.Forms.MessageBox]::Show("Run script 1")}
"Item 2" { [System.Windows.Forms.MessageBox]::Show("Run script 2")}
"Item 3" { [System.Windows.Forms.MessageBox]::Show("Run script 3")}
"Item 4" { [System.Windows.Forms.MessageBox]::Show("Run script 4")}
"Item 5" { [System.Windows.Forms.MessageBox]::Show("Run script 5")}
}
})
$objForm.Controls.Add($OKButton)
$CancelButton = New-Object System.Windows.Forms.Button
$CancelButton.Location = New-Object System.Drawing.Size(150,120)
$CancelButton.Size = New-Object System.Drawing.Size(75,23)
$CancelButton.Text = "Cancel"
$CancelButton.DialogResult = [System.Windows.Forms.DialogResult]::Cancel
$objForm.Controls.Add($CancelButton)
$objForm.CancelButton = $CancelButton
$objLabel = New-Object System.Windows.Forms.Label
$objLabel.Location = New-Object System.Drawing.Size(10,20)
$objLabel.Size = New-Object System.Drawing.Size(280,20)
$objLabel.Text = "please choose"
$objForm.Controls.Add($objLabel)
$objCombobox = New-Object System.Windows.Forms.Combobox
$objCombobox.Location = New-Object System.Drawing.Size(10,40)
$objCombobox.Size = New-Object System.Drawing.Size(260,20)
$objCombobox.Height = 70
[void] $objCombobox.Items.Add("Item 1")
[void] $objCombobox.Items.Add("Item 2")
[void] $objCombobox.Items.Add("Item 3")
[void] $objCombobox.Items.Add("Item 4")
[void] $objCombobox.Items.Add("Item 5")
$objForm.Controls.Add($objCombobox)
$objForm.Add_Shown({$objForm.Activate()})
$dialogResult = $objForm.ShowDialog()
# IMPORTANT clean up the form when done
$objForm.Dispose()
Finally please mark the thread as "solved", thanks.
Regards SK
Well, but why posting threads when you do it yourself anyway ... ?? 🐟