With this code if I type mixed it finds Mixed content
okay.
How can I modify this to search mid string for example content
and have the dropdown displayed with the matching items that are in ComboBox?
Add-Type -AssemblyName System.Windows.Forms
$Form = New-Object System.Windows.Forms.Form
$Form.StartPosition = 'CenterScreen'
$cbSearch = New-Object System.Windows.Forms.ComboBox
$cbSearch.Width = 280
# This will allow autocomplete but not for mid string.
$cbSearch.AutoCompleteSource = 'ListItems'
$cbSearch.AutoCompleteMode = 'Append'
$cbSearch.Items.AddRange(@('Mixed content', 'More stuff', 'ANOTHER THING CONTENT', 'Item ?', 'Mixed item'))
$cbSearch.Add_SelectedValueChanged({ selectChanged $this $_ })
# Event handler action for the cbSearch control.
function selectChanged ($sender, $event) {
[void][system.windows.forms.messagebox]::Show($cbSearch.Text)
}
$Form.Controls.Add($cbSearch)
[void]$Form.ShowDialog()
The result I was after
If I search content
then the two items are shown.
EDIT:
Here's my latest attempt which filters how I would like using the .Add_TestUpdate
callback but it results in some errors in the console Unhandled Exception: System.AccessViolationException: Attempted to read or write protected memory. This is often an indication that other memory is corrupt.
and only allowing one letter to be typed into the box.
Perhaps someone may know how to fix this issue or perhaps I'm barking up the wrong tree.
Add-Type -AssemblyName System.Windows.Forms
$Form = New-Object System.Windows.Forms.Form
$Form.StartPosition = 'CenterScreen'
$cbSearch = New-Object System.Windows.Forms.ComboBox
$cbSearch.Width = 280
# This will allow autocomplete but not for mid string.
$cbSearch.AutoCompleteSource = 'ListItems'
$cbSearch.AutoCompleteMode = 'SuggestAppend'
$cbArr = @('Mixed', 'More stuff CONTENT', 'ANOTHER THING cONTENT', 'Item ?', 'Mixed item')
$cbSearch.Items.AddRange($cbArr)
# Create a new array for found items.
$foundItems = @()
# TextUpdate callback.
# In an attempt to get mid string searching to work.
$cbSearch.Add_TextUpdate(
{
$foundItems = $script:cbArr | Where-Object {$_ -Match [regex]::Escape($cbSearch.Text)}
# If there's found items, begin the update, clear the filter, add the range, end
# update and the set the dropdown to its downed state.
if ($foundItems.length -gt 0) {
$cbSearch.BeginUpdate()
$cbSearch.Items.Clear()
$cbSearch.Items.AddRange($foundItems)
$cbSearch.EndUpdate()
$cbSearch.DroppedDown = $true
} else {
# If no items are found, do this.
$cbSearch.DroppedDown = $false
$cbSearch.BeginUpdate()
$cbSearch.Items.Clear()
$cbSearch.Items.AddRange($script:cbArr)
$cbSearch.EndUpdate()
}
}
)
# Add controls and start form.
$Form.Controls.Add($cbSearch)
[void]$Form.ShowDialog()