Tag Archives: Combobox

How to add Auto Complete to Textbox or Combobox Controls in Visual Basic 2005/2008, VB.NET 2010, and newer

Click Star to Rate Post
1 Star2 Stars3 Stars4 Stars5 Stars (6 votes, average: 2.83 out of 5)
 

Note: This is an article which was taken from my vbcodesource.com website and is quite old. So take that into consideration… 🙂

As you will see, adding Auto-Complete support to textbox and combobox controls has NEVER been easier than it is now in Visual Basic.NET 2005. No longer do you HAVE to use the Windows API’s to get the Auto Complete functionality.

I will assume you already have a ‘Windows Application’ based project open. You can go ahead and add a textbox control or combobox control to the form. Select the textbox and goto the properties window. (I will assume you added a textbox, but the process is the same for a combobox also. I will just refer to textbox instead of combobox.)

There are 3x properties that relate to the ‘Auto Complete’ feature. You have “AutoCompleteCustomSource”, “AutoCompleteMode” and “AutoCompleteSource”. AutoCompleteCustomSource is exactly how it sounds. You can have your own text that you want the user to be able to use when they are typing in the control. But, I am not interested in going over that property right now though. There is really nothing more to it than what I already said. 🙂

OK, the “AutoCompleteMode” property is simply how the AutoComplete text responds. Click on the “AutoCompleteMode” property and then open the dropDown list associated with it. You will see 4x ‘modes’ available. “None, “Suggest”, “Append”, and “SuggestAppend”. This property MUST be selected either “Append”, “Suggest”, or “SuggestAppend” for the AutoComplete feature to work. If you select “Suggest” mode, then ‘AutoComplete’ will display a list that matches the text that has been typed thus far that you can scroll through and choose from. If you select “Append” mode, then as you are typing, it will automatically highlight the closest match to the text thus far in the textbox/combobox control, usually in alphabetical order. Plus, while you are typing, you CAN use the ‘Up/Down’ buttons on the keyboard to scroll through the list of text that is similar to the text that was typed in the textbox control. The “SuggestAppend” mode, is of course the combination of the other 2x modes. As you are typing the text, it will highlight the first occurance of matching text while also displaying a list of all the text that matches what has been typed in the control.

The “AutoCompleteSource” property is exactly how it sounds. It is the location on the computer system to get the ‘AutoComplete’ list from. Click on that property and then click on the box to dropdown the enumerator values that are available as the source. Available values, at least on my computer are: “FileSystem”, “HistoryList”, “RecentlyUsedList”, “AllUrl”, “AllSystemSources”, “FileSystemDirectories”, “CustomSource”. Just like the “AutoCompleteMode” property, you MUST select one of these values for you to get ‘AutoComplete’ functionality. Since all of the values are self-explanatory, I will not go over them.

Just to test out the ‘AutoComplete’ feature. With a textbox control, (Or Combobox), click on the “AutoCompleteMode” property and select the “Suggest” mode. Then click on the “AutoCompleteSource” property and then select “AllUrl” as the source. Run the project, start typing in the textbox ‘www’ and you should see a list of url’s you’ve visited that start with ‘www’ popup. You can then click on one of the urls and it will put that url text in the textbox.

And thats ALL there is to it! Enjoy!

Jason

 

Auto Complete Support in Textbox and Combobox Controls using VB 6.0 and VB.NET

Click Star to Rate Post
1 Star2 Stars3 Stars4 Stars5 Stars (3 votes, average: 2.00 out of 5)
 

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.

  • Append
  • Suggest
  • Append & Suggest

Visual Basic.NET

    '
    'The API call that enables the autoComplete feature to your combo or textbox controls.
    Private Declare Sub SHAutoComplete Lib "shlwapi.dll" (ByVal controlHandle As Int32, ByVal _
        completeFlags As Int32)

Visual Basic 6.0

    '
    'The API call that enables the autoComplete feature to your combo or textbox controls.
    Private Declare Sub SHAutoComplete Lib "shlwapi.dll" (ByVal controlHandle As Long, ByVal _
        completeFlags As Long)

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.


 

    '
    'Various Flags you can set to be the source.
    '
    Private Const Defaults = &H0

    Private Const Filesystem = &H1

    Private Const HistoryList = &H2

    Private Const URLRecentlyUsedList = &H4

    Private Const AllURLs = (HistoryList Or URLRecentlyUsedList)

    Private Const AllSystemsSources = AllURLs Or FileSystem

The flags below are used to specify how you want your autocomplete feature to react.

    '
    'Flags you can use to specify how your want autocomplete to react.
    '
    Private Const append = &H40000000 Or &H20000000

    Private Const suggest = &H10000000 Or &H80000000

    Private Const suggestAppend = &H40000000 Or &H10000000

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.


Visual Basic.NET

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

Visual Basic 6.0

    '
    '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!

Jason

How to Autocomplete ComboBoxes like Textboxes using VB and VB.NET

Click Star to Rate Post
1 Star2 Stars3 Stars4 Stars5 Stars (No Ratings Yet)
 

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.

    Private Declare Function FindWindowEx Lib "user32" Alias "FindWindowExA" (ByVal hWnd1 As  _
        Int32, ByVal hwndChildAfter As Integer, ByVal lpszClass As String, ByVal lpszWindow As String) As Int32

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.

               Jason

Revised: 2015

Add ‘all’ Date/Time Formats to Listbox/Combobox – VB.NET

Click Star to Rate Post
1 Star2 Stars3 Stars4 Stars5 Stars (No Ratings Yet)
 


This will add all of the available Date and Time formats to a listbox control or to a combobox control. This works for all versions of Visual Basic.NET.

 

        'Should add all of the available Date and Time formats to a combobox or listbox control.

        Dim dateTime As DateTime = New DateTime(Now.Year, Now.Month, Now.Day, Now.Hour, Now.Minute, Now.Second)

        Dim d() As String = Nothing

        d = dateTime.GetDateTimeFormats()

        ListBox1.Items.AddRange(d)

 

Add all Known GDI+ Web Colors to a Combobox or Listbox control.

Click Star to Rate Post
1 Star2 Stars3 Stars4 Stars5 Stars (No Ratings Yet)
 

This will add the list of all known colors to a combobox or listbox control.

Imports System.Drawing

'

Dim kColor As KnownColor

For kColor = KnownColor.AliceBlue To KnownColor.YellowGreen 

  Combobox1.Items.Add(kColor)

Next

Add all Date and Time Formats to Listbox or Combobox using VB.NET and Higher

Click Star to Rate Post
1 Star2 Stars3 Stars4 Stars5 Stars (No Ratings Yet)
 

This will add all of the available Date and Time formats to a Listbox or Combobox control. This works for all versions of Visual Basic.NET.

 

        '
        'Should add all of the available Date and Time formats to a combobox or listbox control.
        '
        Dim dateTime As DateTime = New  _
                DateTime(Now.Year, Now.Month, Now.Day, Now.Hour, Now.Minute, Now.Second)

        Dim d() As String = Nothing

        d = DateTime.GetDateTimeFormats()

        ListBox.Items.AddRange(d)

 

Why it Beeps Pressing Enter in a Textbox Control and How to Fix it in .NET

Click Star to Rate Post
1 Star2 Stars3 Stars4 Stars5 Stars (No Ratings Yet)
 

I’ve seen many questions on how to disable the beep that occurs when you press the ‘Enter’ key in a single line Textbox control or a Combobox control. From my understanding the reason is because in Single Line mode, the textbox control doesn’t know how to handle the Enter/Return key, and is basically reporting an error when you get the beep. But it is pretty simple to keep that from happening.

Visual Basic 2005, VB 2008, and VB 2010

        ' This code will disable the 'Beeping' you get when you press the 'Enter' key in a Textbox
        'control. This code needs to go into the 'Textbox_KeyDown' event.
        If e.KeyCode = Keys.Enter Then

            e.SuppressKeyPress = True

        End If

Visual Basic.NET 2002/2003

The code below will also work with VB 05, 2008, and Visual Basic 2010. But the above snippet will NOT work on Visual Basic.NET 2002 or .NET 2003.

        '
        'This is for VB.NET 2002, .NET 2003, and Higher
        'Put in your textbox controls Textbox_KeyPress
        '
        If e.KeyChar = Chr(13) Then

            e.Handled = True

        End If
        '
        'Or you can use the code below. Which will be the same result as above.
        '
        If e.KeyChar = vbCr Then

            e.Handled = True

        End If

That should take care of the beeping problem. Take care.