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