如何在PowerShell中使用button.add_click函数更新或刷新comboBox项?

我使用KafkaProducer获取目录。我需要使用ComboBox事件来更新目录。

Button.Click
an_day 回答:如何在PowerShell中使用button.add_click函数更新或刷新comboBox项?

您正在为此填充ComboBox ...

$list = @(Get-ChildItem -Directory ".\").Name
foreach ($lst in $list) {
    $ComboBox1.Items.Add($lst)
}

您想要单击Button来简单地再次执行操作,所以ComboBox将包含最新的目录列表。在这种情况下,您的Click事件处理程序应这样创建...

$Button1.Add_Click({
    # Remove all items from the ComboBox
    $ComboBox1.Items.Clear()

    # Repopulate the ComboBox,just like when it was created
    $list = @(Get-ChildItem -Directory ".\").Name
    foreach ($lst in $list) {
        $ComboBox1.Items.Add($lst)
    }
})

Clear()首先被调用,因此您不会以重复的目录项结尾。

顺便说一句,您可以简化此事...

$list = @(Get-ChildItem -Directory ".\").Name
foreach ($lst in $list) {
    $ComboBox1.Items.Add($lst)
}

...对此...

$list = @(Get-ChildItem -Directory ".\").Name
$ComboBox1.Items.AddRange($list)

...甚至是这个...

$ComboBox1.Items.AddRange(@(Get-ChildItem -Directory ".\").Name)
本文链接:https://www.f2er.com/2996099.html

大家都在问