无法清除或重置列表框

时间:2018-11-27 03:08:37

标签: windows powershell listbox

我无法清除在PowerShell表单中创建的列表框。如果我调用listbox.Item.Clear方法,将清除存储在listbox对象中的数据,但from中显示的项目将保留。即使我尝试创建一个新的空白列表框,仍会显示旧项目。列表框是在通过单击按钮调用的函数中创建的。我希望每次单击按钮时都清除并重新填充列表框。通过执行类似$textbox.Text = "new text"的操作,我可以轻松清除文本并将其重新添加到文本框中。有没有一种方法可以使用列表框?这是我所拥有的:

function AddMembers() {
    Add-Type -AssemblyName System.Windows.Forms
    Add-Type -AssemblyName System.Drawing

    $form = New-Object System.Windows.Forms.Form
    $form.Text = 'Select a Computer'
    $form.Size = New-Object System.Drawing.Size(610,450)
    $form.StartPosition = 'CenterScreen'

    # Create the TextBox used to capture the user's text.
    $textBox = New-Object System.Windows.Forms.Textbox
    $textBox.Location = '10,40'
    $textBox.Size = '375,170'
    $textBox.AcceptsReturn = $true
    $textBox.Multiline = $true
    #$textBox.Text = $UserInputData
    $form.Controls.Add($textBox)

    function Global:GetTheSearchString() {
        # If any text is present in the text box, convert it to a sting, parse, then conver to array $Global:UserInput2
        $UserInput = $textBox.ToString()
        $UserInput = $UserInput -replace "System.Windows.Forms.TextBox, Text: "
        $Global:UserInput2 = $UserInput -split "`r`n"
        Write-Host $Global:UserInput2
        $InputLength = $UserInput2.Length
        Write-Host $InputLength
        $Global:lastLine = $UserInput2[$InputLength -1]
        Write-Host "$Global:lastLine lastLine"
        #return $Global:lastLine
    }

    # Take an input string, search AD, and return the results as an array.
    function Global:SearchAD($SearchString) {
        $Groups = Get-ADObject -LDAPFilter "(cn=$SearchString*)" | select Name | Out-String
        $Groups = $Groups -replace "Name"
        $Groups = $Groups -replace "----"
        $Groups = $Groups.Trim()
        $GroupsArray = $Groups -split "`r`n"
        #Write-Host $GroupsArray
        return $GroupsArray
    }

    # Create the list box for displaying search results
    function CreateListBoxResults($searchResults, $param) {
        $listBox = New-Object System.Windows.Forms.ListBox

        #$form.Controls.Add($listBox)
        #$listBox.Items.Clear()
        #$listBox = New-Object System.Windows.Forms.ListBox

        $listBox.Location = New-Object System.Drawing.Point(10,225)
        $listBox.Size = New-Object System.Drawing.Size(375,20)
        $listBox.Height = 150

        # Add values to the list box
        foreach ($line in $searchResults) {
            [void] $listBox.Items.Add($line)
            Write-Host $line
        }

        #$listBox.Refresh()
        $form.Controls.Add($listBox)
        Write-Host "$param param"
        #Write-Host "$listBox listbox"
        $form.Controls.Add($listBox)
        if ($param -eq $true) {
            Write-Host "$param param"
            $listBox.Dispose()
            $listBox.Items.Clear()
            $form.Controls.Add($listBox)
        }
    }

    #$selectButton.Add_Click({ [string]$selection =  $listBox.SelectedItem.ToString(); Write-Host $selection; Global:ResetSearchBox($selection)})
    #$selectButton.Add_Click({ Write-Host $Global:listBox; $click = $true; $Global:listBox.Items.Clear(); $form.Controls.Remove($Global:listBox); $form.Controls.Add($Global:listBox)})

    Write-Host $UserInput2
    $checkButton = New-Object System.Windows.Forms.Button
    $checkButton.Location = New-Object System.Drawing.Point(9,380)
    $checkButton.Size = New-Object System.Drawing.Size(85,23)
    $checkButton.Text = 'Check Name'
    $form.Controls.Add($checkButton)
    $checkButton.Add_Click({ Global:GetTheSearchString; Write-Host "$Global:lastLine check"; CreateListBoxResults (Global:SearchAD($Global:lastLine)) $false })

    $selectButton = New-Object System.Windows.Forms.Button
    $selectButton.Location = New-Object System.Drawing.Point(102,380)
    $selectButton.Size = New-Object System.Drawing.Size(75,23)
    $selectButton.Text = 'Select'
    $form.Controls.Add($selectButton)
    $selectButton.Add_Click({ CreateListBoxResults $nul $true})

    #$form.Controls.Add($listBox)
    $form.Topmost = $true
    $form.Add_Shown({$form.Activate()})
    $form.ShowDialog() > $nul
}

AddMembers

1 个答案:

答案 0 :(得分:0)

要清除列表框,请使用listbox.Item.Clear()

但是,还有其他一些有用的方法:

$listBox.Dispose()

The Dispose method leaves the Component in an unusable state.(您可能不想要这个。)

$form.Controls.Remove($listBox)

从表单中删除列表框,而不是删除对象。

$listBox.Visible = $false (or $true)

隐藏列表框而不是将其删除

$listBox.Items.Clear()
$listBox.Items.AddRange(*SomeArrayHere*)

您能找到的最接近$textbox.text = "NewText"


使用其中的一些功能可能看起来像这样:

function CreateListBoxResults($searchResults, $param) {

    # Only create a listbox if it doesn't exist
    if (!$listBox) {
        $listBox = New-Object System.Windows.Forms.ListBox
        $listBox.Location = New-Object System.Drawing.Point(10,225)
        $listBox.Size = New-Object System.Drawing.Size(375,20)
        $listBox.Height = 150
    }

    # Add listbox to form
    $form.Controls.Add($listBox)

    # Add values to the list box
    $listBox.Items.Clear()
    foreach ($line in $searchResults) {
        [void] $listBox.Items.Add($line)
        Write-Host $line
    }

    # Remove listbox from form if $param = $true 
    Write-Host "$param param"
    if ($param -eq $true) {
        Write-Host "$param param"
        $form.Controls.Remove($listBox)
    }
}