Powershell文本字段自动完成

时间:2014-12-04 06:13:52

标签: textbox powershell-v3.0

我有一个文本框,我想使用自动完成功能,从文本文件源中读取。我在这里关注MSDN上的一般示例: http://msdn.microsoft.com/en-us/library/system.windows.forms.textbox.autocompletemode(v=vs.110).aspx

我的代码不会抛出错误(哈哈),但是当我启动字符串时,它也没有填充文本框。

$LogList = Get-Content(Resolve-Path "file\logs.txt")
$comp_or_filepath.AutoCompleteSource.CustomSource
$comp_or_filepath.AutoCompleteCustomSource.AddRange($LogList)
$comp_or_filepath.AutoCompleteMode.SuggestAppend

我能在网上看到一个很好的例子吗?我发现一个用于组合框,但我不认为它适用于我的情况。

谢谢!

1 个答案:

答案 0 :(得分:0)

这就是我希望你在近 7 年里一直在寻找的东西?

我正在研究 PowerShell 的 AutoCompleteCustomSource,但关于它的信息很少。

您试图访问不存在的道具。

$comp_or_filepath.AutoCompleteSource.CustomSource
$comp_or_filepath.AutoCompleteMode.SuggestAppend

应该是:

$comp_or_filepath.AutoCompleteSource = 'CustomSource'
$comp_or_filepath.AutoCompleteMode = 'SuggestAppend'

这是带注释的代码:

Add-Type -AssemblyName System.Windows.Forms

#Form setup
$Form                                    = New-Object System.Windows.Forms.Form
$Form.StartPosition                      = 'CenterScreen'

#Set up the control comp_or_filepath
$comp_or_filepath                        = New-Object System.Windows.Forms.ComboBox
$comp_or_filepath.Width                  = 280
$comp_or_filepath.AutoCompleteSource     = 'CustomSource'
$comp_or_filepath.AutoCompleteMode       = 'SuggestAppend'
$cbArr                                   = @('Mixed', 'More stuff', 'ANOTHER THING cONTENT', 'Item ?', 'Mixed item')
$comp_or_filepath.Items.AddRange($cbArr)

# Setup the autocomplete source by reading the file contents and adding each line.
$autoCompleteUsrSrc = New-Object System.Windows.Forms.AutoCompleteStringCollection
Get-Content(Resolve-Path "file\logs.txt") | ForEach-Object {
    $autoCompleteUsrSrc.AddRange($_)
}
$comp_or_filepath.AutoCompleteCustomSource = $autoCompleteUsrSrc

# Add controls and start form.
$Form.Controls.Add($comp_or_filepath)
[void]$Form.ShowDialog()