Selecting items from a list box.

Welcome Forums General PowerShell Q&A Selecting items from a list box.

Viewing 6 reply threads
  • Author
    Posts
    • #11593
      Participant
      Topics: 2
      Replies: 1
      Points: 0
      Rank: Member

      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.

      <br />$search_properties = "sAMAccountName", "sn"<br />$search = "OU=Test,DC=contoso,DC=com"<br />$lastname = "Smith"<br />$users = Get-ADUser -Filter * -SearchBase $search -Properties $search_properties |<br />    Select-Object @{Name="sAM";Expression={$_.sAMAccountName}},<br /> @{Name="Last Name";Expression={$_.sn}} |<br /> Where {$_."Last Name" -eq $lastname}</p><p>[void] [System.Reflection.Assembly]::LoadWithPartialName("System.Windows.Forms")<br />[void] [System.Reflection.Assembly]::LoadWithPartialName("System.Drawing") </p><p>$objForm = New-Object System.Windows.Forms.Form<br />$objForm.Text = "Select the user to remove"<br />$objForm.Size = New-Object System.Drawing.Size(300,200)<br />$objForm.StartPosition = "CenterScreen"</p><p>$objForm.KeyPreview = $True<br />$objForm.Add_KeyDown({if ($_.KeyCode -eq "Enter")<br />    {$selection=$objListBox.SelectedItem;$objForm.Close()}})<br />$objForm.Add_KeyDown({if ($_.KeyCode -eq "Escape")<br />    {$objForm.Close()}})</p><p>$OKButton = New-Object System.Windows.Forms.Button<br />$OKButton.Location = New-Object System.Drawing.Size(75,120)<br />$OKButton.Size = New-Object System.Drawing.Size(75,23)<br />$OKButton.Text = "OK"<br />$OKButton.Add_Click({$selection=$objListBox.SelectedItem;$objForm.Close()})<br />$objForm.Controls.Add($OKButton)</p><p>$CancelButton = New-Object System.Windows.Forms.Button<br />$CancelButton.Location = New-Object System.Drawing.Size(150,120)<br />$CancelButton.Size = New-Object System.Drawing.Size(75,23)<br />$CancelButton.Text = "Cancel"<br />$CancelButton.Add_Click({$objForm.Close()})<br />$objForm.Controls.Add($CancelButton)</p><p>$objLabel = New-Object System.Windows.Forms.Label<br />$objLabel.Location = New-Object System.Drawing.Size(10,20)<br />$objLabel.Size = New-Object System.Drawing.Size(280,20)<br />$objLabel.Text = "Please select a user:"<br />$objForm.Controls.Add($objLabel) </p><p>$objListBox = New-Object System.Windows.Forms.ListBox<br />$objListBox.Location = New-Object System.Drawing.Size(10,40)<br />$objListBox.Size = New-Object System.Drawing.Size(260,20)<br />$objListBox.Height = 80<br />$objListBox.Sorted = $True</p><p>Foreach ($user in $users) {<br />[void] $objListBox.Items.Add($user."sAM")<br />}</p><p>$objForm.Controls.Add($objListBox) </p><p>$objForm.Topmost = $True</p><p>$objForm.Add_Shown({$objForm.Activate()})<br />[void] $objForm.ShowDialog()</p><p>$selection<br />

    • #11601
      Inactive
      Topics: 9
      Replies: 2322
      Points: 0
      Rank: Member

      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:

      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.)

      • #16989
        Participant
        Topics: 0
        Replies: 1
        Points: 0
        Rank: Member

        Hi Dave,

        Your previous answer helped me out too. Would you mind reworking this one as well? It seems to have a similar problem.

        http://technet.microsoft.com/en-us/library/ff730941.aspx

        [void] [System.Reflection.Assembly]::LoadWithPartialName(“System.Drawing”)
        [void] [System.Reflection.Assembly]::LoadWithPartialName(“System.Windows.Forms”)

        $objForm = New-Object System.Windows.Forms.Form
        $objForm.Text = “Data Entry Form”
        $objForm.Size = New-Object System.Drawing.Size(300,200)
        $objForm.StartPosition = “CenterScreen”

        $objForm.KeyPreview = $True
        $objForm.Add_KeyDown({if ($_.KeyCode -eq “Enter”)
        {$x=$objTextBox.Text;$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({$x=$objTextBox.Text;$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 enter the information in the space below:”
        $objForm.Controls.Add($objLabel)

        $objTextBox = New-Object System.Windows.Forms.TextBox
        $objTextBox.Location = New-Object System.Drawing.Size(10,40)
        $objTextBox.Size = New-Object System.Drawing.Size(260,20)
        $objForm.Controls.Add($objTextBox)

        $objForm.Topmost = $True

        $objForm.Add_Shown({$objForm.Activate()})
        [void] $objForm.ShowDialog()

        $x

    • #11607
      Participant
      Topics: 2
      Replies: 1
      Points: 0
      Rank: Member

      That makes perfect sense. Thanks!

    • #19067
      Participant
      Topics: 0
      Replies: 2
      Points: 0
      Rank: Member

      Thanks for this fix. I had problems with the same code too and was so glad to find this excellent fix!

    • #19071
      Inactive
      Topics: 9
      Replies: 2322
      Points: 0
      Rank: Member

      No problem! Just a heads-up, these fixes have been published to TechNet and via a “Hey, Scripting Guy!” article last month: http://blogs.technet.com/b/heyscriptingguy/archive/2014/08/02/weekend-scripter-fixing-powershell-gui-examples.aspx

    • #19072
      Participant
      Topics: 0
      Replies: 2
      Points: 0
      Rank: Member

      Thanks. Shame they don’t have a link to that article in the original which would have saved me several hours of tearing my hair out earlier!

      Much appreciate your help.

      Jon

    • #88858
      Participant
      Topics: 0
      Replies: 1
      Points: 0
      Rank: Member

      Hi Dave,

      Hope you are well.

      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.

      Would appreciate any tips, help or suggestions.

      Thanks

Viewing 6 reply threads
  • The topic ‘Selecting items from a list box.’ is closed to new replies.