VBcoders Guest



Don't have an account yet? Register
 


Forgot Password?



a AutoComplete Very Simple

by ( . Y . ) (4 Submissions)
Category: VB function enhancement
Compatability: Visual Basic 5.0
Difficulty: Beginner
Date Added: Wed 3rd February 2021
Rating: (19 Votes)

VERY SIMPLE cut and paste funtion for the Keypress event of a combobox. Just paste this code into a module or form and call the function from the KeyPress event. KeyAscii = AutoComplete(cboCombobox, KeyAscii,Optional UpperCase)

Rate a AutoComplete Very Simple

Option Explicit
Public Const CB_FINDSTRING = &H14C
Public Declare Function SendMessage Lib "user32" Alias "SendMessageA" (ByVal hwnd As Long, ByVal wMsg As Long, ByVal wParam As Long, lParam As Any) As Long
Function AutoComplete(cbCombo As ComboBox, sKeyAscii As Integer, Optional bUpperCase As Boolean = True) As Integer
 Dim lngFind As Long, intPos As Integer, intLength As Integer
 Dim tStr As String
 With cbCombo
 If sKeyAscii = 8 Then
 If .SelStart = 0 Then Exit Function
 .SelStart = .SelStart - 1
 .SelLength = 32000
 .SelText = ""
 Else
 intPos = .SelStart '// save intial cursor position
 tStr = .Text '// save string
 If bUpperCase = True Then
 .SelText = UCase(Chr(sKeyAscii)) '// change string. (uppercase only)
 Else
 .SelText = UCase(Chr(sKeyAscii)) '// change string. (leave case alone)
 End If
 End If
 
 lngFind = SendMessage(.hwnd, CB_FINDSTRING, 0, ByVal .Text) '// Find string in combobox
 If lngFind = -1 Then '// if string not found
 .Text = tStr '// set old string (used for boxes that require charachter monitoring
 .SelStart = intPos '// set cursor position
 .SelLength = (Len(.Text) - intPos) '// set selected length
 AutoComplete = 0 '// return 0 value to KeyAscii
 Exit Function
 
 Else '// If string found
 intPos = .SelStart '// save cursor position
 intLength = Len(.List(lngFind)) - Len(.Text) '// save remaining highlighted text length
 .SelText = .SelText & Right(.List(lngFind), intLength) '// change new text in string
 '.Text = .List(lngFind)'// Use this instead of the above .Seltext line to set the text typed to the exact case of the item selected in the combo box.
 .SelStart = intPos '// set cursor position
 .SelLength = intLength '// set selected length
 End If
 End With
 
End Function

Download this snippet    Add to My Saved Code

a AutoComplete Very Simple Comments

No comments have been posted about a AutoComplete Very Simple. Why not be the first to post a comment about a AutoComplete Very Simple.

Post your comment

Subject:
Message:
0/1000 characters