VB.NET Combobox - auto-complete behavior for numeric values

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 :)

+7
source share
1 answer

It seems that the problem occurs when the combo box displays the data, since even if you configured a custom source, it reorders in alphabetical order:

 ComboBox1.Items.Add("10") ComboBox1.Items.Add("92") ComboBox1.Items.Add("9000") ComboBox1.Items.Add("9001") ComboBox1.AutoCompleteCustomSource.Add("10") ComboBox1.AutoCompleteCustomSource.Add("92") ComboBox1.AutoCompleteCustomSource.Add("9000") ComboBox1.AutoCompleteCustomSource.Add("9001") ComboBox1.AutoCompleteSource = AutoCompleteSource.CustomSource 

I think the only way I can imagine is to create my own autocomplete example (untested):

 Dim cbotxt As String = ComboBox1.Text Dim key As String key = ChrW(e.KeyCode) ListBox1.Items.Clear() For Each i In ComboBox1.Items Dim s As String = i.ToString() If s.StartsWith(ComboBox1.Text & key) Then ListBox1.Items.Add(s) End If Next If ListBox1.Items.Count > 0 Then ListBox1.Visible = True ComboBox1.Text = ListBox1.Items(0) End If 

Edit:

Good approach for many elements (I use for 10000+ in the application):

The first change from a list in a datagridview. Then declare a list of lines and fill in the values ​​you want to autocomplete

  Dim Numberlist as List<Of String> ' Fill List using Numberlist.Add("String") 

Then in the text change property:

 Filter = NumberList.FindAll(AddressOf checkNum) DataGridView1.DataSource = Filter 

And add a function to check the lines.

 Function checkNum(ByVal b As String) As Boolean If b.StartsWith(ComboBox1.Text) Then Return True Else Return False End If End Function 

This method works on my machine with parameters 10k faster than I can print.

+3
source

All Articles