【问题标题】:Adding a 'select all' function to drop down GUI添加“全选”功能以下拉 GUI
【发布时间】:2019-10-29 22:56:57
【问题描述】:

我有一个下拉菜单 GUI,但不知道如何添加“选择所有月份”选项。

这个问题非常复杂,因为“$group”输出被用于许多不同的事情,例如 get-childitem 过滤器、带有变量的文件路径,以及使用单个文件名和排序的过滤和组织系统他们按月。那么有没有办法添加一个选项。数字 01-12。或者是否有更复杂的 GUI 可以更轻松地多选数字?


[array]$DropDownArrayItems = "","01","02","03","04","05","06","07","08","09","10","11","12"
[array]$DropDownArray = $DropDownArrayItems | sort

# This Function Returns the Selected Value and Closes the Form

function Return-DropDown {
    if ($DropDown.SelectedItem -eq $null){
        $DropDown.SelectedItem = $DropDown.Items[0]
        $script:Choice = $DropDown.SelectedItem.ToString()
        $Form.Close()
    }
    else{
        $script:Choice = $DropDown.SelectedItem.ToString()
        $Form.Close()
    }
}

function SelectGroup{
    [void] [System.Reflection.Assembly]::LoadWithPartialName("System.Windows.Forms")
    [void] [System.Reflection.Assembly]::LoadWithPartialName("System.Drawing")


    $Form = New-Object System.Windows.Forms.Form

    $Form.width = 300
    $Form.height = 150
    $Form.Text = ”Select Filter Month”

    $DropDown = new-object System.Windows.Forms.ComboBox
    $DropDown.Location = new-object System.Drawing.Size(100,10)
    $DropDown.Size = new-object System.Drawing.Size(130,30)

    ForEach ($Item in $DropDownArray) {
     [void] $DropDown.Items.Add($Item)
    }

    $Form.Controls.Add($DropDown)

    $DropDownLabel = new-object System.Windows.Forms.Label
    $DropDownLabel.Location = new-object System.Drawing.Size(10,10) 
    $DropDownLabel.size = new-object System.Drawing.Size(100,40) 
    $DropDownLabel.Text = "Select Month:"
    $Form.Controls.Add($DropDownLabel)

    $Button = new-object System.Windows.Forms.Button
    $Button.Location = new-object System.Drawing.Size(100,50)
    $Button.Size = new-object System.Drawing.Size(100,20)
    $Button.Text = "OK"
    $Button.Add_Click({Return-DropDown})
    $form.Controls.Add($Button)
    $form.ControlBox = $false

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


    return $script:choice
}

$Group = $null
$Group = SelectGroup
while ($Group -like ""){
    $Group = SelectGroup
}
write-host $Group

【问题讨论】:

    标签: powershell user-interface select drop-down-menu


    【解决方案1】:

    添加选项很简单:

    [array]$DropDownArrayItems = "", "01", "02", "03",
    "04", "05", "06", "07", "08", "09", "10", 
    "11", "12", 'Select all months'
    

    要将array 的integers 作为输出,可以这样做:

    if ($Group -eq 'Select all months') {
        1..12 | ForEach-Object { '{0:00}' -f $_ }
    }
    

    这将输出:

    01
    02
    03
    04
    05
    06
    07
    08
    09
    10
    11
    12
    

    【讨论】:

    • 谢谢,这是我一直在寻找的,但我不太明白在哪里放置 :if ($Group -eq 'Select all') { 1..12 | ForEach-Object { '{0:00}' -f $_ } } 当我写主机时它只是吐出'全选',我对 Powershell 很陌生如果你没有猜到
    • 将write-host $Group 替换为if ($Group -eq 'Select all months') {...。这应该可以解决问题。
    【解决方案2】:

    据我所知 System.Windows.Forms.ComboBox 不支持多选 为什么不试试 System.Windows.Forms.Listbox 并将模式设置为 system.windows.forms.selectionmode(None、One、MutiSimple 或 MultiExtended)

    【讨论】:

      【解决方案3】:

      如果您只需要有一个“全部”选项,那么在组合框中添加一个按钮或数组条目以返回该选项并关闭表单?

      如果您希望能够多选特定条目,例如1,3,6,7,8 然后我会使用列表视图而不是多选,因为您可以使用 $ListViewBox.SelectedItems.Text 或类似方法将它们作为对象返回。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2022-06-13
        • 1970-01-01
        • 2016-02-13
        • 2012-06-07
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多