2017-08-16 96 views
1

我似乎無法弄清楚如何從搜索框中自動選擇列表框中的項目。 當我使用搜索框時,我收到了一個寫入主機,指出某個項目在列表框中,但我想自動選擇要搜索的項目。從搜索框中自動選擇列表框項目

搜索框從搜索框中查找包含文本的項目。當您按下查找按鈕時,控制檯會說明該項目是否在列表中。

我試了幾件事情與$ 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

下一次,請儘量減少你的腳本什麼是重要的,這通常也導致更快的答案。 – iRon

回答

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" 
      } 
     } 
    } 
} 

我不知道你是否要選擇多個項目在列表框(取決於SelectionMode),但你可能必須先取消它們:

For ($i = 0; $i -lt $objListBox.Items.Count; $i++) { 
    $objListBox.SetSelected($i, $False) 
}