I have a problem with auto-complete comboboxes behavior in VB.NET (with .NET framework 2.0).
I use combobox to enter numeric values ββand its DropDown list to suggest possible numeric values. This list is sorted in ascending order, for example {"10", "92", "9000", "9001"}.
The combobox properties are set as follows:
- AutoCompleteMode: SuggestAppend
- AutoCompleteSource: ListItems
- DropDownStyle: DropDown
- Sorted: False
The DropDown list is simply populated as follows:
- myCombobox.Items.Add ("10")
- myCombobox.Items.Add ("92")
- myCombobox.Items.Add ("9000")
- myCombobox.Items.Add ("9001")
When I don't type anything, the order of the DropDown list values ββis correct, in source / ascending order. However, when I start typing something, the suggested values ββin the DropDown list are sorted (alphanumeric): if I type "9", the list of sentences becomes {"9000", "9001", "92"}.
I would like to prevent this behavior in order to get list values ββin source / ascending order. I canβt understand how ...
A possible workaround is to fill in the values ββin the list with zeros, for example. {"0010", "0092", "9000", "9001"}, but I would like to avoid this.
Edit:
As suggested by bendataclear, you can use the list box to display offers. This will work for small lists, but does not scale in large lists. This may be useful for some applications. Based on the code provided by bendataclear, I did it like this:
Private Sub ComboBox1_KeyUp(sender As System.Object, e As System.Windows.Forms.KeyEventArgs) Handles ComboBox1.KeyUp
Dim cursorPos As Integer = ComboBox1.SelectionStart ListBox1.Items.Clear() For Each s In ComboBox1.Items If s.StartsWith(ComboBox1.Text) Then ListBox1.Items.Add(s) End If Next If ListBox1.Items.Count > 0 And ComboBox1.Text.Length > 0 Then ComboBox1.Text = ListBox1.Items(0) ComboBox1.SelectionStart = cursorPos ComboBox1.SelectionLength = 0 End If End Sub
code>
The code has not been thoroughly tested and can be improved, but there is a basic idea.
Edit 2:
Using a DataGridView leads to improved performance; that was enough for me. Thanks bendataclear.
Just out of curiosity, any other answer is welcome :)