ComboBox with AutoComplete: How do I select the row containing the suggestion?

Am I missing something? I can populate a ComboBox and enable AutoComplete. Typing some characters will fill in the first match found in the list. But I can’t figure out how to select the matching row and bring it to the front when the PopUp is clicked on.

I would think this should be automatic, but it isn’t. I thought in the KeyDown event I could grab the selected text using ComboBox.Value and search the rows for the match, but when the first character is typed, the Value is not yet set.

It shouldn’t be this difficult. What am I missing? Has someone figured this out? I would think this would be ComboBox 101.

Thanks.

Suggestions, anyone?

You use SelectedRowIndex to set the row.
https://documentation.xojo.com/api/deprecated/popupmenu.html#popupmenu-selectedrowIndex

For example from the TextChanged event.
https://documentation.xojo.com/api/deprecated/combobox.html#combobox-textchanged

Loop tough all values and check if there is a matching one (or best matching one).

Or maybe use keydown to use the Tab key or something and then set the row that matches the string the best.

1 Like

Thanks for your suggestion. That’s exactly what I’m trying to do, but wondered if there was a better way.

There’s still a problem that I can’t figure out. The first keypress brings in an auto-complete string on the screen, but in the KeyDown event, the ComboBox.Value doesn’t yet contain the full text, only the single keypress. So I’m wondering how to obtain the full auto-completed text.

The TextChanged event doesn’t trigger until exiting edit mode.

That is the default behavior. You shouldn’t have to do anything special. Are you saying it doesn’t work?

i made a test this morning

Sub TextChanged() Handles TextChanged
  
  Var count As Integer = 0 
  For i As Integer = 0 To Me.LastRowIndex
    If Me.RowValueAt(i).IndexOf(Me.Value)>-1 Then
      count = count + 1
      If count >1 Then Exit
    End If
  Next
  
  If count = 1 Then
    For i As Integer = 0 To Me.LastRowIndex
      If Me.RowValueAt(i).IndexOf(Me.Value)>-1 And Me.SelectedRowIndex<>i Then
        Me.SelectedRowIndex = i
        Exit
      End If
    Next
  End If
End Sub

2020r1.1

Thanks, Markus, for taking the time to offer that code. The code itself is something I’d already figured out. My stumbling block was that I was trying to use the “Change” event (which doesn’t trigger with the first keypress) instead of the “TextChanged” event. Silly me.

I can now proceed without further difficulty. My point still remains that I would think the ComboBox code should have been written such that the AutoComplete would be linked to selecting the row used to do the completion, eliminating the need for such coding.

Thanks, everyone for your help.