Important alert: (current site time 7/16/2013 1:58:59 AM EDT)
 

VB icon

a AutoComplete Very Simple!

Email
Submitted on: 3/10/2003 3:01:06 PM
By: ( . Y . ) 
Level: Beginner
User Rating: By 19 Users
Compatibility: VB 5.0, VB 6.0
Views: 34253
 
     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!
 
Terms of Agreement:   
By using this code, you agree to the following terms...   
  1. You may use this code in your own programs (and may compile it into a program and distribute it in compiled format for languages that allow it) freely and with no charge.
  2. You MAY NOT redistribute this code (for example to a web site) without written permission from the original author. Failure to do so is a violation of copyright laws.   
  3. You may link to this code from another website, but ONLY if it is not wrapped in a frame. 
  4. You will abide by any additional copyright restrictions which the author may have placed in the code or code's description.
				
'**************************************
' 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 . )
'
'This code is copyrighted and has' limited warranties.Please see http://www.Planet-Source-Code.com/vb/scripts/ShowCode.asp?txtCodeId=43911&lngWId=1'for details.'**************************************

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 10 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

3/11/2003 9:38:46 AMChristopher Reason

This is the best AutoComplete I've been able to find, THANKS! You've done a great job with this.

Just one question; Help me understand why the text has to be converted to uppdercase? And is there a way of setting it back to exact case of the item in the combo list?

Thanks again, Christopher. 5/5
(If this comment was disrespectful, please report it.)

 
3/11/2003 3:05:04 PMJames Berard

for the application im building I needed the charchters entered to be converted to upper case... I have uploaded new code to address this issue. Simply supply a boolean in the function to allow uppercase or not. Of course this default can be changed to false or removed totally. Curently the code sets the default for bUpperCase = True
(If this comment was disrespectful, please report it.)

 
6/9/2003 7:13:49 AMDracarys

With some modifications this is the best AutoComplete EVER. I got around to do it just like windows does. (i.e. handling of DEL and backspace)

Great *****
(If this comment was disrespectful, please report it.)

 
12/27/2003 11:40:41 PM

Great code. Just one question tho, how would I modify this code so that a New Input thats not in the combo box be allowed to be entered?
(If this comment was disrespectful, please report it.)

 
12/28/2003 2:48:02 PMJames Berard

Just rem out the code where the string is not found... like this

If lngFind = -1 Then '// Not found
'.Text = tStr
'.SelStart = intPos
'.SelLength = (Len(.Text) - intPos)
'AutoComplete = 0
'Exit Function
Else '// If String found

(If this comment was disrespectful, please report it.)

 
2/13/2004 1:24:18 PM

Thanks now i can put this code in my homework proyect para impresionar a mi profesor...
The most important es que i learn too.
GRacias
(If this comment was disrespectful, please report it.)

 
3/26/2004 12:12:09 PM

*dont worry about my english, i am brasilian and my english isnt´good
-----------
plz help-me

when i run my program, whit the code of this page
vb show me an error

----------------------ERROR--------------
compile error:

Constans, fixed-lenght strings, arrays, user-defined types and declare statements not allowed as public members of object modules
----------------------LINE---------------

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


please i need this code.
thanks.
(If this comment was disrespectful, please report it.)

 
4/2/2004 2:43:05 AM

The reason you are getting the compile error is because VB won't let you declare API functions as public inside a Form. It will allow you to declare it as Public inside a Module. Just change the "Public Declare SendMessage" to "Private Declare SendMessage" and you should be fine.
(If this comment was disrespectful, please report it.)

 
6/27/2004 7:54:30 AMMuhammed Arif

hey sorry to say but ur Boolean option doesnt work, either u set it to true or false it changes the case to upper case, and if u remove the code of if buppercase part then the combo box does not accepts any keystrokes.... can u solve the upper case thing??? i tried my best but invain.
(If this comment was disrespectful, please report it.)

 
6/27/2004 1:01:25 PMJames Berard

the answer is in this part of the code... if bUpperCase is false you dont need to use the ucase() function. just set .SelText = Chr(sKeyAscii)


If bUpperCase = True Then
.SelText = UCase(Chr(sKeyAscii)) '// change string. (uppercase only)
Else
.SelText = Chr(sKeyAscii) '// change string. (leave case alone)
End If

hope that helps.
(If this comment was disrespectful, please report it.)

 
7/11/2004 9:05:45 PMCor!

Great code, very small and efficient. To replicate the instinctive MS behaviours more precisely, I changed the following code at the start:

If .SelStart = 0 Then Exit Function

To this:

If .SelStart = 0 Then .SelText = "": Exit Function

Thanks for sharing, 5 globes.
(If this comment was disrespectful, please report it.)

 
9/16/2004 8:23:53 AMirish_bloke

Superb and the best and easiest ive seen. 5 stars
(If this comment was disrespectful, please report it.)

 
11/29/2005 8:56:06 AMDean

Brilliant. Far better than anything else I have seen so far. But I have one problem. I need to know the list index of the selected. At the moment I only get -1. I need to know this because I am also using the Item Data property. I also have multiple Combo box's on the same form using the same control.
(If this comment was disrespectful, please report it.)

 
5/26/2006 3:01:04 AMstar

Intelligent code.There r many Autocomplete ways but the problem arises when there are enomrous no of entries in combo box thus the efficency of sendmessage is a life saver .Those who do not want to use API or with problems like of Mr. Dean Alternative Could be using a binary search routine for combobox.List array & return Value or Index anything.
(If this comment was disrespectful, please report it.)

 
9/16/2006 4:24:50 AMismael

hey i did those corrections from the comments........but i encounter problems.......it did autocomplete but always the first index only....and the first letter duplicates........
(If this comment was disrespectful, please report it.)

 
9/16/2006 4:30:22 AMismael

darn!,.........................wht a dumb of me........i did'nt copy those declaritons above......ooooooopppppppsssss.....sorry,.......5 globes.......t u =)
(If this comment was disrespectful, please report it.)

 
3/12/2007 7:04:15 PMN_E_0

WOW!!! nothing else to say!
(If this comment was disrespectful, please report it.)

 
4/19/2007 9:45:00 PMMaxAndrews

If you add
lngFind = SendMessage(.hwnd, CB_SETCURSEL, lngFind, 0) '// Find string in combobox

before
.SelStart = intPos '// Set cursor position
Then the cbo will set the listindex

(If this comment was disrespectful, please report it.)

 
7/16/2007 6:34:51 AMMrEnigma

Public Const CB_SETCURSEL = &H14E

(If this comment was disrespectful, please report it.)

 
9/28/2009 1:36:36 AMr_saavedra

thanks pal, it saves a lot of my time... great codes... thank you very much... =)
(If this comment was disrespectful, please report it.)

 

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.