2017-08-16 2 views
1

Je n'arrive pas à comprendre comment sélectionner automatiquement un élément dans ma liste à partir d'une boîte de recherche. Je reçois un retour en écriture pour indiquer qu'un élément est dans la liste lorsque j'utilise la boîte de recherche, mais je voudrais sélectionner automatiquement l'élément que je recherche.Sélection automatique des éléments de la liste de la boîte de recherche

La zone de recherche recherche l'élément contenant le texte dans la zone de recherche. Lorsque vous appuyez sur le bouton de recherche, la console indique si l'élément est dans la liste ou non.

J'ai essayé quelques choses avec $ objListbox.SelectedIndex, mais je me suis aussi loin que seulement sélectionner le premier élément dans la liste

# 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

la prochaine fois, s'il vous plaît minimiser votre script ce qui est important ce habituellement entraîne également une réponse plus rapide. – iRon

Répondre

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

Je ne sais pas si vous souhaitez que plusieurs éléments sélectionnés dans la zone de liste (en fonction du SelectionMode) mais vous pourriez avoir à les désactiver d'abord:

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