【问题标题】:How do I set up autocomplete in powershell 2.0?如何在 powershell 2.0 中设置自动完成功能?
【发布时间】:2015-09-16 12:25:51
【问题描述】:

我有一个由 CSV 文件填充的下拉菜单。我不得不更改我的代码以使其适用于 v2。我想不通的最后一件事是自动完成。我希望能够在列表框中输入并让它从列表中建议一些选项。它在 v3 中运行良好,但在 v2 中运行良好。

这里是重要的部分......

$filecheck = "$dir\Apps\Customers.csv"
If(Test-Path -path $filecheck) {
    $Customers = Import-CSV "$dir\Apps\Customers.csv" -Header $headers
    $List = $customers | Select-Object -Expand name | where {$_ -ne ""} | where {$_ -ne "Name"} | Sort-Object
}

ForEach ($Items in $List) {

    $companybox.Items.Add($Items)

}

$companybox.AutoCompleteSource = 'CustomSource'
$companybox.AutoCompleteMode = 'SuggestAppend'
$List | % {$companybox.AutoCompleteCustomSource.AddRange($_) }

$Form1.Controls.Add($companybox)

任何帮助将不胜感激。我环顾四周,但没有找到太多关于 v2 的内容。

谢谢。

【问题讨论】:

    标签: forms powershell autocomplete powershell-2.0


    【解决方案1】:

    让它工作。我在运行脚本的批处理文件中添加了 -STA 以将其更改为单线程单元。

    我的批处理文件现在看起来像这样以管理员身份在 STA 中运行脚本...

    SET Dir=%~dp0
    SET PSPath=%Dir%Script.ps1
    PowerShell -NoProfile -ExecutionPolicy Bypass -Command "& {Start-Process PowerShell -ArgumentList '-STA -NoProfile -ExecutionPolicy Bypass -File ""%PSPath%""' -Verb RunAs}";
    

    另一种选择是将其放入 PS 脚本中...

    if ([System.Threading.Thread]::CurrentThread.ApartmentState -eq [System.Threading.ApartmentState]::MTA)
    {
    powershell.exe -Sta -File $MyInvocation.MyCommand.Path
    return
    }
    

    但是,第二个选项破坏了我必须在启动时隐藏控制台窗口的代码。

    【讨论】:

      猜你喜欢
      • 2018-09-13
      • 1970-01-01
      • 2011-03-12
      • 2016-10-07
      • 2021-06-24
      • 1970-01-01
      • 2015-05-01
      • 1970-01-01
      • 2017-05-11
      相关资源
      最近更新 更多