Visual Basic Text API Routines
SHAutoComplete: Add AutoComplete to a VB Text Box
     
Posted:   Friday November 26, 1999
Updated:   Monday December 26, 2011
     
Applies to:   VB4-32, VB5, VB6
Developed with:   VB6, Windows NT4
OS restrictions:   None
Author:   VBnet - Randy Birch
     

Related:  

FindWindowEx: Obtain Combo Box Edit Window Handle
GetComboBoxInfo: Obtain Combo Box Edit and List Handles on Win98/NT5
GetVersionEx: Windows Version Info (Wrapper Routines)
     
 Prerequisites
Internet Explorer 5 or greater.

IE5 introduced the AutoComplete feature, and its available for both standard VB text boxes and combo boxes using style 0 or 1. The routine here just demonstrates the textbox, but its easily extended to include the combo by passing the handle of the edit control (not the handle to the combo box - see related topics above). When used with the combo box, the AutoComplete dropdown appears in addition to the standard drop down; its data does not replace the dropdown contents.

Use of this API is limited to systems with IE5 or greater, so a handy routine for determining the IE version is included. And although the MSDN states that CoInitialize must be called before using this API, and CoUninitialize called when the edit control has been destroyed, VB takes care of handling this task naturally.

 BAS Module Code
None.

 Form Code
Add a label (Label1), and text box (Text1) and a command button (Command1) to a form, and add the following:

Option Explicit
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
' Copyright 1996-2011 VBnet/Randy Birch, All Rights Reserved.
' Some pages may also contain other copyrights by the author.
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
' Distribution: You can freely use this code in your own
'               applications, but you may not reproduce 
'               or publish this code on any web site,
'               online service, or distribute as source 
'               on any media without express permission.
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
'Flags to control the operation of SHAutoComplete.
'The first four are used to override the Internet
'Explorer registry settings. The user can change
'these settings manually by launching the Internet
'Options property sheet from the Tools menu and
'clicking the Advanced tab. The last five can be
'used to specify which files or URLs will be
'available for auto append or autosuggest operations.

'Ignore registry default and force feature on
Private Const SHACF_AUTOSUGGEST_FORCE_ON  As Long = &H10000000

'Ignore registry default and force feature off.
Private Const SHACF_AUTOSUGGEST_FORCE_OFF  As Long = &H20000000

'Ignore registry default and force feature on. (Also know as AutoComplete)
Private Const SHACF_AUTOAPPEND_FORCE_ON  As Long = &H40000000

'Ignore registry default and force feature off. (Also know as AutoComplete)
Private Const SHACF_AUTOAPPEND_FORCE_OFF  As Long = &H80000000

'Currently (SHACF_FILESYSTEM | SHACF_URLALL)
Private Const SHACF_DEFAULT  As Long = &H0

'Includes the File System as well as the rest
'of the shell (Desktop\My Computer\Control Panel\)
Private Const SHACF_FILESYSTEM  As Long = &H1

'URLs in the User's History
Private Const SHACF_URLHISTORY  As Long = &H2

'URLs in the User's Recently Used list
Private Const SHACF_URLMRU  As Long = &H4

Private Const SHACF_URLALL  As Long = (SHACF_URLHISTORY Or SHACF_URLMRU)

'Identifies the platform for which the DLL was built.
Private Const DLLVER_PLATFORM_WINDOWS As Long = &H1  'Windows 95
Private Const DLLVER_PLATFORM_NT As Long = &H2       'Windows NT

Private Type DllVersionInfo
   cbSize As Long
   dwMajorVersion As Long
   dwMinorVersion As Long
   dwBuildNumber As Long
   dwPlatformID As Long
End Type

Private Declare Function SHAutoComplete Lib "shlwapi" _
  (ByVal hwndEdit As Long, _
   ByVal dwFlags As Long) As Long

Private Declare Function DllGetVersion Lib "shlwapi" _
  (dwVersion As DllVersionInfo) As Long


Private Function GetIEVersion(DVI As DllVersionInfo) As Long
   
   DVI.cbSize = Len(DVI)
   Call DllGetVersion(DVI)

   GetIEVersion = DVI.dwMajorVersion
   
End Function


Private Function GetIEVersionString() As String
   
   Dim DVI As DllVersionInfo
   
   DVI.cbSize = Len(DVI)
   Call DllGetVersion(DVI)

   GetIEVersionString = "Internet Explorer " & _
                        DVI.dwMajorVersion & "." & _
                        DVI.dwMinorVersion & "." & _
                        DVI.dwBuildNumber
   
End Function


Private Sub Command1_Click()

   Dim DVI As DllVersionInfo

   If GetIEVersion(DVI) >= 5 Then
   
     'Turn on auto-complete
      Call SHAutoComplete(Text1.hWnd, SHACF_DEFAULT)
      
     'update the captions and set focus to the textbox
      Command1.Caption = "SHAutoComplete is On"
      Command1.Enabled = False
      Text1.SetFocus
      Text1.SelStart = Len(Text1.Text)
   
   Else
   
     'damn!
      MsgBox "Sorry ... you need IE5 to use this demo", vbExclamation
      
   End If
   
End Sub


Private Sub Form_Load()

  'dim a DllVersionInfo type
   Dim DVI As DllVersionInfo

  'display the version of Shlwapi
   Label1.Caption = "Using Shlwapi.dll for " & GetIEVersionString
   
  'if not 5 or greater, can't do it
   Command1.Enabled = GetIEVersion(DVI) >= 5
   Command1.Caption = "SHAutoComplete is Off"

End Sub
 Comments

 
 

PayPal Link
Make payments with PayPal - it's fast, free and secure!

 
 
 
 

Copyright 1996-2011 VBnet and Randy Birch. All Rights Reserved.
Terms of Use  |  Your Privacy

 

Hit Counter