Help: How to create a auto-suggest textfield?

Hello,

I need some ideas as to how I can create an auto-suggest TextField, (or better, if someone already has one and is willing to share).

Thanks.

Lennox

What are you trying to do?

Hi Kem,

Thanks, I have a dynamic menu that has a lot of menu items e.g Occupation, so the user can add to that menu.

The menu items are stored in a text file and when the user clicks on the menu the menu items loads.

So I would like the option of start entering text in the associated text field and as the user types the next matching item in the text file is suggested, like in Xojo.

If the suggested text is the required one the user clicks “Tab”.

Something like that or better.

Lennox

capture the TEXTCHANGE event
use the current contents to do a


sql="SELECT word from table_of_words where word like '"+textfield1.text+"%'"

It will be tricky, but you can do something like that using KeyDown. As the user types, see what the word would be be with the letter they just entered and fill in the rest using your suggested word. Leave the rest of the word highlighted so the next letter would replace it. Capture the tab to advance past the selected portion of the text.

You’ll have to track when you selected the word using auto-correct vs. when the user selected it themselves.

OK Dave and Kem, I will try that.
Thanks.
Lennox

Try the AutoCompleteTextField. Or use this:
Create a new Class and set its Super to TextField and rename it to AutoCompleteTextField
Add this to the AutoCompleteTextField

[i]Function KeyDown(Key As String) As Boolean
// If the user is deleting text, don’t run the TextChange event
Suppress = (Chr(8) = Key)
End Function

Sub TextChange()
// Don’t try to AutoComplete if there’s no text or if we’re trying to suppress AutoComplete
if (me.Text.Len = 0 or Suppress) then return

// See if the current text matches part of a word in .Words array
Dim word, low As String
Dim length As Integer = me.Text.Len

// Check every word in our list to see if it matches
For Each word In Me.Words
low = word.Lowercase

// If this word matches
If left(low, length) = me.Text.Lowercase Then
  
  // Set the word
  me.Text = word
  
  // Select the part that hasn't been typed yet (but matches the rest of it)
  me.SelStart = length
  me.SelLength = (me.Text.Len - length)
  
  // And call it a day.
  return
End If

Next
End Sub[/i]

Add a property “Suppress” with type “Boolean” to AutoCompleteTextField
Add a property “Words(-1)” with type Text to AutoCompleteTextField
Place the AutoCompleteTextField to Window1
Add this to Window1

Sub Open()
// Example only
EditField1.Words.Append(“bar”)
EditField1.Words.Append(“foo”)
EditField1.Words.Append(“foobar”)
EditField1.Words.Append(“baz”)
EditField1.Words.Append(“bang”)
EditField1.Words.Append(“bash”)
EditField1.Words.Append(“DingoStick.com”)


// Make lookups faster (?)
EditField1.Words.Sort
End Sub

Well if it isn’t TOO many items, would a combobox with autocomplete work for you? That would give the user a pulldown way of selecting something, or they could type it in directly… I don’t know how many items would be unworkable for that solution though.