Page 1 of 1

Autocomplete combobox How to make a combobox with large number of items to select item autma Rate Topic: -----

#1 muddasir  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 8
  • View blog
  • Posts: 114
  • Joined: 14-January 07

Post icon  Posted 15 October 2007 - 12:29 PM

if we have small no of items in a combobox we
can easily select an item from it , but if the item
list it too long we want that if we can start typing
any word it will autmatic be selected in combobox.
which infect is not possible as default in a combobox.
to achieve this we have to do a little bit of coding.
i will try it to explain to you my best

first of all we have to place a comboxbox in the
form combobox1. set the style property of the combobox as 0 dropdown combo

first we have to add items in the combobox for
this we have to add items in the load event of the
form .

Private Sub Form_Load()
combobox1.AddItem ("Apple")
combobox1.AddItem ("Banana")
combobox1.AddItem ("Lemon")
combobox1.AddItem ("Orange")
combobox1.AddItem ("Malta")
end sub


this code fill comboxbox with items

Now for combobox to autocomplete we have to code combobox for two evnet we may change the text
by typing a new one or you may delete some portion
of text

now for the text change we have to do the following coding in the change event of combobox.
the code also explain after each step what functions
it do.



Dim blnAuto As Boolean
Dim strPart As String, iLoop As Integer, iStart As Integer, strItem As String
	'don't do if no text or if change was made by autocomplete coding
	If Not blnAuto And combobox1.Text <> "" Then
		'save the selection start point (cursor position)
		iStart = combobox1.SelStart
		'get the part the user has typed (not selected)
		strPart = Left$(combobox1.Text, iStart)
		For iLoop = 0 To combobox1.ListCount - 1
			'compare each item to the part the user has typed,
			'"complete" with the first good match
			strItem = UCase$(combobox1.List(iLoop))
			If strItem Like UCase$(strPart & "*") And _
					strItem <> UCase$(combobox1.Text) Then
				'partial match but not the whole thing.
				'(if whole thing, nothing to complete!)
				blnAuto = True
				combobox1.SelText = Mid$(combobox1.List(iLoop), iStart + 1) 'add on the new ending
				combobox1.SelStart = iStart   'reset the selection
				combobox1.SelLength = Len(combobox1.Text) - iStart
				blnAuto = False
				Exit For
			End If
		Next iLoop
	End If



and coding for keydown evnet will be

'Unless we watch out for it, backspace or delete will just delete
	'the selected text (the autocomplete part), so we delete it here
	'first so it doesn't interfere with what the user expects
	If KeyCode = vbKeyBack Or KeyCode = vbKeyDelete Then
		blnAuto = True
		combobox1.SelText = ""
		blnAuto = False
	ElseIf KeyCode = vbKeyReturn Then 'Accept autocomplete on 'Enter' keypress
		combobox1_LostFocus
		'the following causes the item to be selected and
		'the cursor placed at the end:
		combobox1.SelStart = Len(combobox1.Text)
		
		'This would select the whole thing instead:
		'combobox1.SelLength = Len(combobox1.Text)
	
		'alternatively, you could move the focus to the next control here
	End If


and on the lost evnet we have to code this,so as
if we type only first few letter of the item auto
fill the remaining.


Dim iLoop As Integer
'Match capitalization if item entered is one on the list
	If combobox1.Text <> "" Then
		For iLoop = 0 To combobox1.ListCount - 1
			If UCase$(combobox1.List(iLoop)) = UCase$(combobox1.Text) Then
				blnAuto = True
				combobox1.Text = combobox1.List(iLoop)
				blnAuto = False
				Exit For
			End If
		Next iLoop
	End If


Now, Run the project in the combobox type letter
A.it shows you the complete item "Apple".
now delete and and change it to any other
word it will automatically selected.

for small list of items it may not look quiet good but
for a larege list of items its help a lot.

Is This A Good Question/Topic? 0
  • +

Replies To: Autocomplete combobox

#2 akhileshbc  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 9
  • View blog
  • Posts: 179
  • Joined: 27-September 08

Posted 28 September 2008 - 03:58 AM

If you set the Style property of combo box to 2-Dropdown, then you can cycle through the first letter of the item. But your code is much useful....:) Thanks yaar..:)
Was This Post Helpful? 0
  • +
  • -

#3 mashuaibu  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 1
  • Joined: 17-December 08

Posted 17 December 2008 - 06:30 AM

View Postakhileshbc, on 28 Sep, 2008 - 02:58 AM, said:

If you set the Style property of combo box to 2-Dropdown, then you can cycle through the first letter of the item. But your code is much useful....:) Thanks yaar..:)

Hi Muddasir,

I went through your combo box write-up (i.e. code and instructions). It was great. I will apply it and see how it will work. I have been looking for something like it. Thanks so much.

Best regards.

Al Amin.
Was This Post Helpful? 0
  • +
  • -

#4 Guest_Daya*


Reputation:

Posted 07 May 2010 - 01:36 AM

This code show error when we delete any character from combo box.
But it is really very good example.
Was This Post Helpful? 0

Page 1 of 1