我似乎无法弄清楚如何从搜索框中自动选择列表框中的项目。 当我使用搜索框时,我得到一个写主机回来说项目在列表框中,但是我想自动选择我搜索的项目。
搜索框会从搜索框中查找包含文本的项目。当您按下查找按钮时,控制台会显示该项目是否在列表中。
我用$ objListbox.SelectedIndex尝试了一些东西,但我只选择了列表框中的第一个项目
# Alle Variabelen
<#----------------------------------------------------------------------------#>
$reg_bestanden_dir = "\\dataasp01\d$\system\scripts\ADVIESBOX\adviesbox_update_release\Productie\"
#$Listtxt = "\\dataasp01\d$\system\scripts\ADVIESBOX\adviesbox_update_release\script\Test.txt"
$count=1#>
$textBox1 = New-Object System.Windows.Forms.TextBox
$Find = New-Object System.Windows.Forms.Button
$regbestanden= Get-ChildItem $reg_bestanden_dir | where {$_.Attributes -ne 'Directory'} |select name
# Hoofd formulier
<#----------------------------------------------------------------------------#>
[void] [System.Reflection.Assembly]::LoadWithPartialName("System.Windows.Forms")
[void] [System.Reflection.Assembly]::LoadWithPartialName("System.Drawing")
$objForm = New-Object System.Windows.Forms.Form
$objForm.Text = "Adviesbox Omgeving"
$objForm.Size = New-Object System.Drawing.Size(500,500)
$objForm.StartPosition = "CenterScreen"
$objForm.KeyPreview = $True
$objForm.Add_KeyDown({if ($_.KeyCode -eq "Enter")
{$x=$objListBox.SelectedItem;$objForm.Close()}})
$objForm.Add_KeyDown({if ($_.KeyCode -eq "Escape")
{$objForm.Close()}})
# Searchbox
<#----------------------------------------------------------------------------#>
$handler_Find_Click=
{
Foreach ($regbestand in $regbestanden)
{
if($regbestand -match $textbox1.Text)
{
#Select item in listbox
Write-Host "Item is in the list"
}
else
{
Write-Host "Item is not the same"
}
}
}
# Buttons
<#----------------------------------------------------------------------------#>
$StartenButton = New-Object System.Windows.Forms.Button
$StartenButton.Location = New-Object System.Drawing.Size(25,400)
$StartenButton.Size = New-Object System.Drawing.Size(75,23)
$StartenButton.Text = "Starten"
$StartenButton.Add_Click({$x=$objListBox.SelectedItem; Starten})
$objForm.Controls.Add($StartenButton)
$UpdateButton = New-Object System.Windows.Forms.Button
$UpdateButton.Location = New-Object System.Drawing.Size(150,400)
$UpdateButton.Size = New-Object System.Drawing.Size(75,23)
$UpdateButton.Text = "Update"
$UpdateButton.Add_Click({$x=$objListBox.SelectedItem; Updaten})
$objForm.Controls.Add($UpdateButton)
$CancelButton = New-Object System.Windows.Forms.Button
$CancelButton.Location = New-Object System.Drawing.Size(275,400)
$CancelButton.Size = New-Object System.Drawing.Size(75,23)
$CancelButton.Text = "Cancel"
$CancelButton.Add_Click({$objForm.Close()})
$objForm.Controls.Add($CancelButton)
# GUI boxen
<#----------------------------------------------------------------------------#>
$objLabel = New-Object System.Windows.Forms.Label
$objLabel.Location = New-Object System.Drawing.Size(10,20)
$objLabel.Size = New-Object System.Drawing.Size(190,20)
$objLabel.Text = "Selecteer een Adviesbox Omgeving:"
$objForm.Controls.Add($objLabel)
$objListBox = New-Object System.Windows.Forms.ListBox
$objListBox.Location = New-Object System.Drawing.Size(20,40)
$objListBox.Size = New-Object System.Drawing.Size(400,20)
$objListBox.Height = 350
$countListBox = New-Object System.Windows.Forms.ListBox
$countListBox.Location = New-Object System.Drawing.Size (200,18)
$countListBox.Size = New-Object System.Drawing.Size (30,25)
$objForm.Controls.Add($countListBox)
$textBox1.DataBindings.DefaultDataSourceUpdateMode = 0
$System_Drawing_Point = New-Object System.Drawing.Point
$System_Drawing_Point.X = 250
$System_Drawing_Point.Y = 15
$textBox1.Location = $System_Drawing_Point
$textBox1.Name = "textBox1"
$System_Drawing_Size = New-Object System.Drawing.Size
$System_Drawing_Size.Height = 20
$System_Drawing_Size.Width = 120
$textBox1.Size = $System_Drawing_Size
$textBox1.TabIndex = 0
$objForm.Controls.Add($textBox1)
<# Autocomplete deels werkend
----------------------------------------------------------------------------
$textBox1.AutoCompleteSource = 'CustomSource'
$textBox1.AutoCompleteMode = 'SuggestAppend'
$textBox1.AutoCompleteCustomSource = $autocomplete
Get-Content $Listtxt | % {$textbox1.AutoCompleteCustomSource.AddRange($_)}
#>
$Find.DataBindings.DefaultDataSourceUpdateMode = 0
$System_Drawing_Point = New-Object System.Drawing.Point
$System_Drawing_Point.X = 375
$System_Drawing_Point.Y = 15
$Find.Location = $System_Drawing_Point
$Find.Name = "Find"
$System_Drawing_Size = New-Object System.Drawing.Size
$System_Drawing_Size.Height = 20
$System_Drawing_Size.Width = 50
$Find.Size = $System_Drawing_Size
$Find.TabIndex = 1
$Find.Text = "Find"
$Find.UseVisualStyleBackColor = $True
$Find.add_Click($handler_Find_Click)
$objForm.Controls.Add($Find)
# Functies
<#----------------------------------------------------------------------------#>
Function Starten
{
Set-Location -Path $reg_bestanden_dir
reg import $objListBox.SelectedItem
#invoke-item $adviesb0x
}
Function Updaten
{
Invoke-Item $objListBox.SelectedItem
}
# Warning box voor de juiste omgeving
<#----------------------------------------------------------------------------#>
<#$objListbox.add_SelectedIndexChanged(
{
[System.Windows.MessageBox]::Show($objlistBox.SelectedItem, "Omgeving:")
}
)#>
# Laat alle omgevingen zien in de listbox
<#----------------------------------------------------------------------------#>
<# foreach ($regbestand in $regbestanden) {
$regbestand.name
}#>
$regbestanden | ForEach-Object {
[void] $objListBox.Items.Add($_.name)
}
# Telt de aantal omgevingen (teller)
<#----------------------------------------------------------------------------#>
$countListBox.Items.Add($regbestanden.count)
$objForm.Controls.Add($objListBox)
$objForm.Topmost = $True
$objForm.Add_Shown({$objForm.Activate()})
[void] $objForm.ShowDialog()
$x
<#Backup#>
#https://technet.microsoft.com/en-us/library/ff730949.aspx
#Get-Content $Listtxt | ForEach-Object {[void] $objListBox.Items.Add($_)}
#$objForm.WindowState = [System.Windows.Forms.FormWindowState]::Minimized
<#Set-Location -Path "\\dataasp01\d$\system\scripts\ADVIESBOX\adviesbox_update_release\Productie\"
Write-Host "Item is in list"
$findeditems = Get-ChildItem $objListBox.Items | where {$objListbox.Items -eq $textbox1.Text}
$objListbox.Items.Add($findeditems)#>
答案 0 :(得分:0)
$handler_Find_Click=
{
Foreach ($regbestand in $regbestanden)
{
For ($i = 0; $i -lt $objListBox.Items.Count; $i++) {
if($regbestand -match $objListBox.Items[$i])
{
$objListBox.SetSelected($i, $True)
Write-Host "Item is in the list"
}
else
{
Write-Host "Item is not the same"
}
}
}
}
我不确定您是否要在ListBox中选择多个项目(取决于SelectionMode),但您可能必须先取消选择它们:
For ($i = 0; $i -lt $objListBox.Items.Count; $i++) {
$objListBox.SetSelected($i, $False)
}