natalie.solero
Goto Top

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

Content-Key: 661830

Url: https://administrator.de/contentid/661830

Ausgedruckt am: 28.03.2024 um 15:03 Uhr

Mitglied: 147669
147669 12.03.2021, aktualisiert am 16.05.2023 um 15:17:24 Uhr
Goto Top
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)  
}
Mitglied: natalie.solero
natalie.solero 15.03.2021, aktualisiert am 16.05.2023 um 15:17:24 Uhr
Goto Top
hi @147669,
many thanks for your response. In the meantime I've also tried to continue. I think everything works so far. maybe you can help me again. i want the program to remain open according to choice. this is what i have
$x =  # the return variable which is updated in the SelectedIndexChanged event handler
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         = "OK"  
$OKButton.DialogResult = [System.Windows.Forms.DialogResult]::OK
$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.AcceptButton = $OKButton
$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")  
$objCombobox.Add_SelectedIndexChanged({
   
    $script:x = $this.SelectedItem  

  
})
$objForm.Controls.Add($objCombobox) 

$objForm.Add_Shown({$objForm.Activate()})
$dialogResult = $objForm.ShowDialog()

# IMPORTANT clean up the form when done
$objForm.Dispose()

if ($dialogResult -eq 'OK' -and ![string]::IsNullOrWhiteSpace($x)) {  
   


    Write-Host "You have selected '$($x)'" -ForegroundColor Cyan  

    switch ($x) {
        "Item 1" { <# do something when Item 1 is selected #> }  
        "Item 2" { <# do something when Item 2 is selected #> }  
        "Item 3" { <# do something when Item 3 is selected #> }  
        "Item 4" { <# do something when Item 4 is selected #> }  
        "Item 5" { <# do something when Item 5 is selected #> }  
    }
}
else {
    Write-Host "close"  
}
Mitglied: 147669
Lösung 147669 15.03.2021, aktualisiert am 16.05.2023 um 15:17:24 Uhr
Goto Top
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.
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
Mitglied: natalie.solero
natalie.solero 18.03.2021, aktualisiert am 16.05.2023 um 15:17:24 Uhr
Goto Top
Nn the meantime i managed it too. Nevertheless many thanks.
Mitglied: 147669
147669 18.03.2021, aktualisiert am 16.05.2023 um 15:17:24 Uhr
Goto Top
Zitat von @natalie.solero:

Nn the meantime i managed it too.
Well, but why posting threads when you do it yourself anyway ... ?? 🐟