VB icon

a AutoComplete Very Simple!

Email
Submitted on: 1/31/2015 11:51:00 AM
By: ( . Y . ) (from psc cd)  
Level: Beginner
User Rating: By 19 Users
Compatibility: VB 5.0, VB 6.0
Views: 3246
 
     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)
 
code:
Can't Copy and Paste this?
Click here for a copy-and-paste friendly version of this code!
				
'**************************************
' Name: a AutoComplete Very Simple!
' Description: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)
' By: ( . Y . ) (from psc cd)
'**************************************

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


Other 3 submission(s) by this author

 


Report Bad Submission
Use this form to tell us if this entry should be deleted (i.e contains no code, is a virus, etc.).
This submission should be removed because:

Your Vote

What do you think of this code (in the Beginner category)?
(The code with your highest vote will win this month's coding contest!)
Excellent  Good  Average  Below Average  Poor (See voting log ...)
 

Other User Comments


 There are no comments on this submission.
 

Add Your Feedback
Your feedback will be posted below and an email sent to the author. Please remember that the author was kind enough to share this with you, so any criticisms must be stated politely, or they will be deleted. (For feedback not related to this particular code, please click here instead.)
 

To post feedback, first please login.