Valhalla Legends Archive

Programming => General Programming => Topic started by: -kP-FuZioN on July 10, 2003, 10:02 PM

Title: Autocomplete
Post by: -kP-FuZioN on July 10, 2003, 10:02 PM
I'm using vb 6 and I want to make some sort of mechanism for autocomplete, like the ones where you type the first part, and then the rest of the word is filled in, but highlighted.  I don't know how to highlight things on text boxes.  Can someone help me out?
Title: Re:Autocomplete
Post by: iago on July 11, 2003, 07:10 AM
To highlight, say, the first 5 characters, use this:
text1.selstart = 5
text1.sellength = len(text1.text) - 5
Title: Re:Autocomplete
Post by: dxoigmn on July 11, 2003, 08:24 AM
Quote from: iago on July 11, 2003, 07:10 AM
To highlight, say, the first 5 characters, use this:
text1.selstart = 5
text1.sellength = len(text1.text) - 5



Wouldn't that highlight all the characters after the 5th one?
Title: Re:Autocomplete
Post by: DarkMinion on July 11, 2003, 01:12 PM
Yes, selstart should be 0, and sellength should be just 5 if you want the first 5 characters....
Title: Re:Autocomplete
Post by: iago on July 11, 2003, 04:12 PM
That's what I meant to say.. my bad :)
Title: Re:Autocomplete
Post by: ioSys on July 12, 2003, 02:38 AM
Mail me and i can send you the code for it. its not that hard. [email protected]
Title: Re:Autocomplete
Post by: TheMinistered on July 12, 2003, 07:44 AM
Here is a combo box autocomplete from extreme vb code library:

Private Declare Function SendMessage Lib "user32" Alias "SendMessageA" ( _
   ByVal hWnd As Long, ByVal wMsg As Long, ByVal wParam As Long, lParam As Any) _
   As Long

Private Const CB_FINDSTRING As Long = &H14C

Public Sub ComboAutoComplete(ByRef SourceCtl As VB.ComboBox, _
   ByRef KeyAscii As Integer, ByRef LeftOffPos As Long)
 Dim iStart As Long
 Dim sSearchKey As String
 
 With SourceCtl
   'If text entered so far matches item(s) in the list, use autocomplete
   Select Case Chr$(KeyAscii)
     Case vbBack
       'Let backspace characters process as usual; otherwise try to match text
     Case Else
       If Chr$(KeyAscii) <> vbBack Then
         .SelText = Chr$(KeyAscii)
         
         iStart = .SelStart
         
         If LeftOffPos <> 0 Then
           .SelStart = LeftOffPos
           iStart = LeftOffPos
         End If
         
         sSearchKey = CStr(Left$(.Text, iStart))
         .ListIndex = SendMessage(.hWnd, CB_FINDSTRING, -1, _
             ByVal CStr(Left$(.Text, iStart)))
         
         If .ListIndex = -1 Then
           LeftOffPos = Len(sSearchKey)
         End If
         
         .SelStart = iStart
         .SelLength = Len(.Text)
         LeftOffPos = 0
         
         KeyAscii = 0
       End If
   End Select
 End With
End Sub

Private Sub Combo1_KeyPress(KeyAscii As Integer)
 Static iLeftOff As Long
 
 ComboAutoComplete Combo1, KeyAscii, iLeftOff
End Sub