Tag Archives: FindwindowEx

Autocomplete is a very useful feature that Microsoft added with the release of Internet Explorer 5.0 and therefore Internet Explorer 5.0 or higher needs to be installed on the target computer. Adding autocomplete to your applications can be a real benefit to the user and is a easy feature to implement.

Note: Visual Basic 2005 and Visual Basic 2008 already has Autocomplete capabilities in the Textbox and Combobox controls by default.

There are a few different locations you can get the autocomplete data from…

File System

History List

URLs Recently Used

ALL URLs

ALL System Sources

There are likewise a few different options available to set the way you want AutoComplete to react.

The SHAutoComplete API Sub is easy to use. The first parameter is the Handle to the Edit (Combobox/Textbox) control you want to enable Auto Complete too. The second Parameter is how you want your Autocomplete to react and display.

Now that the API based codes are out of the way, you can setup the edit based controls (Like Textbox and Combobox) to have Autocomplete capabilities. Below is a simple example of adding autocomplete to a textbox control that I named txt.

'Set autocomplete to the textbox using the system defaults.
SHAutoComplete(txt.hWnd, Defaults)
'You can also specify how it will react and what it will display.
SHAutoComplete(txt.hWnd, suggest Or AllURLs)

If your using .NET, just change txt.hWnd to txt.Handle.ToInt32.

Unfortunately adding this feature to a Combobox isn’t as straight forward. Thats because when you use the Handle/Hwnd Property for the Combobox it returns the Main Window Portion of the control and NOT the Edit Window portion. There are a couple ways to get around this though. One way it to use the GetComboBoxInfo API or to use the FindWindowEx API call. I chose to go ahead and use the FindWindowEx API.

'
'API Function used to get the Edit window of a combobox control.
Private Declare Function FindWindowEx Lib "user32" Alias "FindWindowExA" (ByVal handle As _
Long, ByVal hwndChildAfter As Long, ByVal lpszClass As String, ByVal lpszWindow As String) As Long

Now you just need to use the FindWindowEx Function using the 1st and 3rd parameter. And once you call the FindWindow function, simply pass its return value to the SHAutoComplete Method.

'
'Simply change this variable type to a Integer if your using VB.NET.
Dim cmbHandle As Long
'Retrieve the handle to the ‘Edit’ Window of the combobox. I named my Combobox control cmb.
cmbHandle = FindWindowEx(cmb.hWnd, 0, "Edit", vbNullString)
'Activate Autocomplete to the combobox edit window.
SHAutoComplete(cmb.hWnd, Defaults)

Tip: Here is how you can specify exactly how to react and display….

'
'You can also specify how it will react and what it will display.
SHAutoComplete(cmb.hWnd, suggest Or AllURLs)

Again, change cmb.hWnd to cmb.Handle.ToInt32 if your using Visual Basic.NET.

Thats all there is to it! Depending on your application, you or your apps user will really appreciate you putting that feature in your program. Well, I guess there is nothing more to add. Have fun!

I’ve seen this question before and the answer is pretty simple when you get an understanding of the combo control. When you use the handle associated with the Combobox, it is the main portion of the Control that the handle refers to and NOT the edit box which is the important part.

Note:IF your using Classic Visual Basic (5.0 or 6.0) then simply change the int32 and Integer variable types to Long types since the codes here are setup for VB.NET.

To get the handle to this area of the Combobox will require a windows api call. The API Call is: FindWindowEx. This call will get the Handle of the specified control according to the specified Class Name.

The parameters we are interested in is hWnd and lpszClass. You can set the others to Null. The Handle would be the handle to the main portion of a control. Which is what the ComboBox.Handle.ToInt32 method returns. The class name is the class type you want to get the handle too. Which happens to be “Edit”

'Will hold the “Edit” handle given by the FindWindowEx API call from the ComboBox.
Dim cmbHandle As Int32 = FindWindowEx(ComboBox.Handle.ToInt32, 0, "Edit", vbNullString)

And thats all you need to make the combobox Autocomplete capable. Simply specify the cmbHandle integer in the SHAutoComplete ‘handle’ parameter and it will give your combobox the Autocomplete feature!

More Info:If you want the complete source code to autocomplete combobox controls and textbox controls with the actual SHAutoComplete source codes as well, then simply click on this link to view the post.