none
Forwarding selected item out of listbox to click action from button RRS feed

  • Question

  • Hello Technet-members,

    ive got the problem that i cant forward a selected entry from a listbox to a click action from my button which i added to a powershell gui.

    As shown below

    #Create new List Box
    $objListBox = New-Object System.Windows.Forms.ListBox 
    $objListBox.Location = New-Object System.Drawing.Size(10,190) 
    $objListBox.Size = New-Object System.Drawing.Size(400,300) 
    $objListBox.Height = 300
    
    #Add for each Entry a new Row
    Foreach ($Printer in $Printers) {
      [void] $objListBox.Items.Add($Printer."Name")
    }
    $objForm.Controls.Add($objListBox) 
    $objForm.Topmost = $True
    
    # Install action 
    $handler_install_click = {
      $state = $objListBox.SelectedItems()
      $list = @()
      $list = $state.ToString().Split("-")
      [string] $servername = $list[0]
      [string] $printername = $list[1]
      write-host $servername 
      write-host $printername
    }
    
    
    As a result i should receive a splitet $server and $printername of the chosen printer from the list box.

    The error which i receive is:

    Method invocation failed because [System.Windows.Forms.ListBox] does not contain a method named 'SelectedItem'.
    At \\srvfs01\USERHOME$\ToparB\PS_MapPrinters.ps1:104 char:3
    +   $state = $objListBox.SelectedItem()
    +   ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
        + CategoryInfo          : InvalidOperation: (:) [], RuntimeException
        + FullyQualifiedErrorId : MethodNotFound
     
    You cannot call a method on a null-valued expression.
    At \\srvfs01\USERHOME$\ToparB\PS_MapPrinters.ps1:106 char:3
    +   $list = $state.ToString().Split("-")
    +   ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
        + CategoryInfo          : InvalidOperation: (:) [], RuntimeException
        + FullyQualifiedErrorId : InvokeMethodOnNull


    • Moved by Bill_Stewart Tuesday, November 7, 2017 9:58 PM Unanswerable drive-by question
    Thursday, September 21, 2017 1:26 PM

Answers

  • "SelectedItems" is not a method it is a property.  In your case it is an array of strings with the printer name.  If this is not a multiselect list the you want "SelectedItem"

    The following is incorrect or unnecessary:

    Foreach ($Printer in$Printers) {
     
    [void]$objListBox.Items.Add($Printer."Name")
    }

    Use:

    Foreach ($Printer in$Printers) {
     
    [void]$objListBox.Items.Add($Printer.Name)
    }

    The following:

    $handler_install_click = {
      $state
    =$objListBox.SelectedItems()
      $list
    = @()
      $list
    =$state.ToString().Split("-")
     
    [string]$servername =$list[0]
     
    [string]$printername =$list[1]
      write
    -host $servername
      write
    -host $printername
    }

    Is best stated like this: (assuming that your printer names are in this format.

    $handler_install_click = {
     $servername, $printername= $objListBox.SelectedItem -split '-'
     write-host "Server - $servername"
     write-host "Printer - $printername"
    }

    See: https://msdn.microsoft.com/en-us/library/system.windows.forms.listbox.selecteditem(v=vs.110).aspx


    \_(ツ)_/




    • Edited by jrv Thursday, September 21, 2017 2:05 PM
    • Marked as answer by BenTop Monday, January 15, 2018 1:56 PM
    Thursday, September 21, 2017 2:02 PM