none
Trying to pull currently selected item from DropDown ComboBox in Powershell

    Question

  • Greetings! I'm currently attempting to write a script that pulls the names of companies from a .csv then executes an install based on the selected company in the dropdown box. I'm pretty mediocre at scripting though so this has been an endeavor.

    I have the box working and pulling the company names just fine, but for the life of me I cannot get the function to pull the value to work. I've attempted to change the script to just output the name selected and update a label but it doesn't seem to be outputting anything at all. No errors either. Just a whole lotta nothing.

    Clear button works fine though lol.

    Here's what I currently have...

    $scriptpath = $MyInvocation.MyCommand.Path
    $dir = Split-Path $scriptpath
    
    ###initialform
    
    Add-Type -assembly System.Windows.Forms
    $main_form = New-Object System.Windows.Forms.Form
    $main_form.Text ='Install Script'
    $main_form.Width = 550
    $main_form.Height = 110
    
    ###functions
    
    function Clear-Form{
    
    $cbxCust.SelectedIndex = -1
    $cbxCust.SelectedItem = ""
    
    Clear-Host
    
    }
    
    function DataRun {
    
    $cbxCust.add_SelectedIndexChanged(
    {
    write-host $cbxCust.SelectedItems.ToString()
    $CustChoice = $cbxCust.SelectedItems
    $Label2.Text = "$CustChoice"
    })
    
    }
    
    ###combobox
    
    $cbxCust = New-Object System.Windows.Forms.ComboBox
    $cbxCust.DropDownStyle = [System.Windows.Forms.ComboBoxStyle]::DropDownList
    $cbxCust.Width = 300
    $Customers = Import-CSV $dir\CurrentCustomers.csv | %{$cbxCust.Items.Add($_.CustomerName)}
    $cbxCust.Location = New-Object System.Drawing.Point(60,10)
    $main_form.Controls.Add($cbxCust)
    
    ###label2
    
    $Label2 = New-Object System.Windows.Forms.Label
    $Label2.Text = "Select a customer"
    $Label2.Location  = New-Object System.Drawing.Point(0,40)
    $Label2.AutoSize = $true
    $main_form.Controls.Add($Label2)
    
    ###It's a button, don't get too excited...
    
    $Button = New-Object System.Windows.Forms.Button
    $Button.Location = New-Object System.Drawing.Size(400,10)
    $Button.Size = New-Object System.Drawing.Size(120,23)
    $Button.Text = "Select"
    $Button.Add_Click({$DataRun})
    $main_form.Controls.Add($Button)
    
    ###clear
    $ClearButton = New-Object System.Windows.Forms.Button
    $ClearButton.Location = New-Object System.Drawing.Size(400,35)
    $ClearButton.Size = New-Object System.Drawing.Size(120,23)
    $ClearButton.Text = "Clear"
    $ClearButton.Add_Click({Clear-Form})
    $main_form.controls.Add($ClearButton)
    
    $main_form.ShowDialog()

    Any help at all would be great! Thanks ahead of time!

    Tuesday, November 6, 2018 12:34 AM

Answers

  • It needs a lot of sorting out and some attention to creating objects and syntax.

    Don't use ISE.  Use the command line to test or you will have variable issues.

    $scriptpath = $MyInvocation.MyCommand.Path $dir = Split-Path $scriptpath Add-Type -assembly System.Windows.Forms $main_form = New-Object System.Windows.Forms.Form $main_form.Text ='Install Script' $main_form.Width = 550 $main_form.Height = 110
    $main_form.StartPosition = 'CenterScreen' ###combobox $cbxCust = New-Object System.Windows.Forms.ComboBox $main_form.Controls.Add($cbxCust) $cbxCust.DropDownStyle = 'DropDownList' $cbxCust.Width = 300 $cbxCust.Location = '60,10' $cbxCust.add_SelectedIndexChanged({ $Label2.Text = $cbxCust.SelectedItem }) ###label2 $Label2 = New-Object System.Windows.Forms.Label $Label2.Text = "Select a customer" $Label2.Location = '0,40' $Label2.AutoSize = $true $main_form.Controls.Add($Label2) ###It's a button, don't get too excited... $Button = New-Object System.Windows.Forms.Button $main_form.Controls.Add($Button) $Button.Location = '400,10' $Button.Size ='120,23' $Button.Text = "Select" $Button.Add_Click({ $customers = Import-CSV $dir\CurrentCustomers.csv | select -ExpandProperty CustomerName $cbxCust.Items.AddRange($customers) }) ###clear $ClearButton = New-Object System.Windows.Forms.Button $main_form.controls.Add($ClearButton) $ClearButton.Location = '400,35' $ClearButton.Size = '120,23' $ClearButton.Text = "Clear" $ClearButton.Add_Click({ #Clear-Form }) $main_form.ShowDialog()



    \_(ツ)_/


    • Edited by jrv Tuesday, November 6, 2018 1:48 AM
    • Marked as answer by CCrain Tuesday, November 6, 2018 5:53 PM
    Tuesday, November 6, 2018 1:46 AM

All replies

  • It needs a lot of sorting out and some attention to creating objects and syntax.

    Don't use ISE.  Use the command line to test or you will have variable issues.

    $scriptpath = $MyInvocation.MyCommand.Path $dir = Split-Path $scriptpath Add-Type -assembly System.Windows.Forms $main_form = New-Object System.Windows.Forms.Form $main_form.Text ='Install Script' $main_form.Width = 550 $main_form.Height = 110
    $main_form.StartPosition = 'CenterScreen' ###combobox $cbxCust = New-Object System.Windows.Forms.ComboBox $main_form.Controls.Add($cbxCust) $cbxCust.DropDownStyle = 'DropDownList' $cbxCust.Width = 300 $cbxCust.Location = '60,10' $cbxCust.add_SelectedIndexChanged({ $Label2.Text = $cbxCust.SelectedItem }) ###label2 $Label2 = New-Object System.Windows.Forms.Label $Label2.Text = "Select a customer" $Label2.Location = '0,40' $Label2.AutoSize = $true $main_form.Controls.Add($Label2) ###It's a button, don't get too excited... $Button = New-Object System.Windows.Forms.Button $main_form.Controls.Add($Button) $Button.Location = '400,10' $Button.Size ='120,23' $Button.Text = "Select" $Button.Add_Click({ $customers = Import-CSV $dir\CurrentCustomers.csv | select -ExpandProperty CustomerName $cbxCust.Items.AddRange($customers) }) ###clear $ClearButton = New-Object System.Windows.Forms.Button $main_form.controls.Add($ClearButton) $ClearButton.Location = '400,35' $ClearButton.Size = '120,23' $ClearButton.Text = "Clear" $ClearButton.Add_Click({ #Clear-Form }) $main_form.ShowDialog()



    \_(ツ)_/


    • Edited by jrv Tuesday, November 6, 2018 1:48 AM
    • Marked as answer by CCrain Tuesday, November 6, 2018 5:53 PM
    Tuesday, November 6, 2018 1:46 AM
  • Ahh, thank you so much! I'd been using ISE to test actually, nice catch on that one.

    Like I said, I'm not exactly great at this so my syntax is probably really poor. Apologies about that.

    I'll work on it some more with the changes you made! It works perfectly to get the customer list imported and pull the value. I kinda understand where I went wrong now comparing the two versions.

    Tuesday, November 6, 2018 5:52 PM
  • Here is a good resource: https://info.sapien.com/index.php/guis/gui-controls

    \_(ツ)_/


    • Edited by jrv Tuesday, November 6, 2018 5:55 PM
    Tuesday, November 6, 2018 5:55 PM