Hey all. I’m working on a list box to choose a user from a provided list, in this case users having the last name of Smith for an example. I’ve referenced the technet article on creating a list box, and while it populates correctly the variable I’m storing the selected user in returns nothing. I’m assuming this is probably due to how I’m passing each user’s sam account name to the list box but I’m not certain.
If anyone code provide some insight, I’d appreciate it. Code below.
I don’t know what the deal is with those sample articles on TechNet. This is the second one I’ve seen where they used the approach of trying to set a variable’s value from inside the OK Button’s Click event. This doesn’t work, as written, due to variable scoping issues. You’re just setting a variable named $selection inside that script block.
The quick and dirty fix for this is to use the script: scope modifier when assigning the variable:
$script:selection = $objListBox.SelectedItem
I don’t actually like this method much, but it does work.
Here’s how I would modify their sample code. (I would have modified yours instead, but copying and pasting it out of the forum post stuck everything on one giant line for some reason.)
[void] [System.Reflection.Assembly]::LoadWithPartialName("System.Windows.Forms")
[void] [System.Reflection.Assembly]::LoadWithPartialName("System.Drawing")
$objForm = New-Object System.Windows.Forms.Form
$objForm.Text = "Select a Computer"
$objForm.Size = New-Object System.Drawing.Size(300,200)
$objForm.StartPosition = "CenterScreen"
# Got rid of the block of code related to KeyPreview and KeyDown events.
$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"
# Got rid of the Click event for the OK button, and instead just assigned its DialogResult property to OK.
$OKButton.DialogResult = [System.Windows.Forms.DialogResult]::OK
$objForm.Controls.Add($OKButton)
# Setting the form's AcceptButton property causes it to automatically intercept the Enter keystroke and
# treat it as clicking the OK button (without having to write your own KeyDown events).
$objForm.AcceptButton = $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"
# Got rid of the Click event for the Cancel button, and instead just assigned its DialogResult property to Cancel.
$CancelButton.DialogResult = [System.Windows.Forms.DialogResult]::Cancel
$objForm.Controls.Add($CancelButton)
# Setting the form's CancelButton property causes it to automatically intercept the Escape keystroke and
# treat it as clicking the OK button (without having to write your own KeyDown events).
$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 select a computer:"
$objForm.Controls.Add($objLabel)
$objListBox = New-Object System.Windows.Forms.ListBox
$objListBox.Location = New-Object System.Drawing.Size(10,40)
$objListBox.Size = New-Object System.Drawing.Size(260,20)
$objListBox.Height = 80
[void] $objListBox.Items.Add("atl-dc-001")
[void] $objListBox.Items.Add("atl-dc-002")
[void] $objListBox.Items.Add("atl-dc-003")
[void] $objListBox.Items.Add("atl-dc-004")
[void] $objListBox.Items.Add("atl-dc-005")
[void] $objListBox.Items.Add("atl-dc-006")
[void] $objListBox.Items.Add("atl-dc-007")
$objForm.Controls.Add($objListBox)
$objForm.Topmost = $True
# Now, instead of having events in the form assign a value to a variable outside of their scope, the code that calls the dialog
# instead checks to see if the user pressed OK and selected something from the box, then grabs that value.
$result = $objForm.ShowDialog()
if ($result -eq [System.Windows.Forms.DialogResult]::OK -and $objListBox.SelectedIndex -ge 0)
{
$selection = $objListBox.SelectedItem
$selection
# Do something with $selection
}
$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 enter the information in the space below:”
$objForm.Controls.Add($objLabel)
Wondering if you can help, I am using the above script to prompt a user to select an App-V package from a folder and then to publish the selected App-V package. However how do I get $selection to run Add-AppvClientPackage | Publish-AppvClientPackage -global. I also need it to Enable-appv using as administrator.