Page 1 of 1

Find&Select

Posted: 19 Jan 2010, 15:29
by Mex
Hi Timo

Let's say i have combobox and some items on it;
Adnrea
Berta
Inga
Timo
Thomas

When the combobox is in focus and i start typing, T (Timo is selected), I (Inga is selected)
Is is possible to disable this feature? What i want is when T and then I is pressed, Timo is still selected and not Inga

BR;
Meelis

Re: Find&Select

Posted: 19 Jan 2010, 21:27
by TiKu
Windows doesn't support this feature, but it should be possible to implement it yourself, based on the SelectItemByText method, the KeyPress event and a timer.

Something like this:
Dim s As String
On Combo_KeyPress: s = s & Chr(keyAscii), SelectItemByText(s, False), reset/start timer
On Timer: s = ""

TiKu

Re: Find&Select

Posted: 11 May 2012, 10:44
by ngyntom
i want auto complete, but i dont know, can u help me?
sorry, my english bad
thanks

Re: Find&Select

Posted: 12 May 2012, 15:11
by TiKu
Hi,

what kind of auto-complete do you need? Do you want to display filesystem paths and provide auto-completion like the "Run" dialog of Microsoft Windows? This can be achieved with the SHAutoComplete API function.
Or do you want to provide auto-completion where the rest of the word is inserted into the edit field and highlighted automatically?

TiKu

Re: Find&Select

Posted: 12 May 2012, 15:28
by TiKu
To provide the second type of auto-completion try this code:

Code: Select all

Option Explicit

  Dim m_enteredText As String


Private Sub ComboBox1_KeyDown(keyCode As Integer, ByVal shift As Integer)
  Dim cbi As ComboBoxItem
  
  If keyCode = vbKeyReturn Then
    ComboBox1.SetSelection Len(ComboBox1.Text), -1
  ElseIf keyCode = vbKeyBack Then
    If Len(m_enteredText) > 1 Then
      m_enteredText = Left$(m_enteredText, Len(m_enteredText) - 1)
      Set cbi = ComboBox1.FindItemByText(m_enteredText, False)
      If Not (cbi Is Nothing) Then
        ComboBox1.Text = cbi.Text
        ComboBox1.SetSelection Len(m_enteredText), Len(ComboBox1.Text)
      End If
      keyCode = 0
    Else
      ComboBox1.Text = ""
    End If
  End If
End Sub

Private Sub ComboBox1_TextChanged()
  Dim cbi As ComboBoxItem
  
  m_enteredText = ComboBox1.Text
  Set cbi = ComboBox1.FindItemByText(m_enteredText, False)
  If Not (cbi Is Nothing) Then
    ComboBox1.Text = cbi.Text
    ComboBox1.SetSelection Len(m_enteredText), Len(ComboBox1.Text)
  End If
End Sub

Private Sub Form_Load()
  With ComboBox1.ComboItems
    .Add "This is item 1"
    .Add "Followed by item 2"
    .Add "To auto-complete"
    .Add "Type into the edit field"
    .Add "The control does not"
    .Add "Support auto-completion"
    .Add "Out of the box"
  End With
  m_enteredText = ""
End Sub

Re: Find&Select

Posted: 14 May 2012, 02:03
by ngyntom
thanks, it run good
orther question. i use event TextChanged but when load data to textbox it not change.
what do i need use event? i try use all event but not change