VB.net ComboBox AutoComplete


This article is written by Pon Saravanan  on 21-Jan-11 Last modified on :08-Feb-11





VB.Net Tutorial - ComboBox AutoCompleteMode

ComboBox control is similar to ListBox control, in which you can select one item from a list of items. But it takes less space on screen and it allows you to locate an item by setting value to the ComboBox control’s text property. In simple word, it is an expandable(collapse/expand) ListBox control.

AutoCompleteMode Property

Here we are going to see the AutoCompleteMode property in ComboBox control. This property automatically matches the input string given on runtime (starts with match) of all strings in the source database column. Based on that it will display the matched string in ComboBox. This property is very useful for frequently searching strings. Such as URLs, file name, customer name, or any command that is frequently used. This property go well when there is no duplication in source data. If there is duplication occurs in source data then the AutoCompleteMode property omits the duplication and display only once.

Fetch data from SQL server

There is an easy way to fetch data from the SQL server, first select the “Add New Item” from Project menu. In which, select “LINQ to SQL Classes”, name it as Northwind.dbml and press Add button. Now you get the new Northwind.dbml designer on screen. Now drag the Customer table from server explorer window to Northwind.dbml. Now you can fetch the source data from SQL server by using the following code

ComboBox1.DataSource = (New NorthwindDataContext).Customers.Where(Function(Cust)
Cust.ContactName.Contains(comboBox1.SelectedText))

Implementation of AutoCompleteMode property

Pickup a ComboBox control from the toolbox and place it in your form1.vb[Design]. Now set the “DropDownStyle” property value as DropDown. To execute AutoCompleteMode property, The AutoCompleteModeProperty and AutoCompleteSource property must be used together. First you should set the AutoCompleteMode property value as Suggest and set AutoCompleteCustomSource property value as ListItems. If the value of AutoCompleteCustomSource property is null, then the prefix of the source data that gets matched with the input string will not get listed below the ComboBox .
The following code is used to set both the properties.

ComboBox1.AutoCompleteMode = AutoCompleteMode.Suggest
ComboBox1.AutoCompleteSource = AutoCompleteSource.ListItems


Screen Capture




Source Code

Form1.Vb

Imports System.Linq
Imports System.Collections.Generic
Imports System.Data.SqlClient
Public Class Form1
    Private RowCount As Integer
 
    Private Function GetData() As IEnumerable
        Dim Datacontext As New NorthwindDataContext
        Dim Filtered = Datacontext.Customers.Where(Function(Cust) _
                                    Cust.ContactName.Contains(ComboBox1.SelectedText))
        Return Filtered
    End Function
 
    Private Sub LoadCustomers()
        ComboBox1.DisplayMember = "ContactName"
        ComboBox1.DataSource = GetData()
    End Sub
    Private Sub Form1_Load(ByVal sender As System.Object, _
                           ByVal e As System.EventArgs) Handles MyBase.Load
        ComboBox1.AutoCompleteMode = AutoCompleteMode.Suggest
        ComboBox1.AutoCompleteSource = AutoCompleteSource.ListItems
        LoadCustomers()
    End Sub
End Class










Comments
  • GUEST
    Thk you, I will want used it. I never use autocomplete property of combobox. 5/4/2011 2:48:30 AM

  • GUEST
    Thanks a lot! I tried before, didn't work. Found some code - it was way too complicated. New classes, inheritance, whatever. And didn't work.
    Then I found your tutorial - and everything works. The trick were the properties you mentioned, and in other examples weren't mentioned.
    Great job.
    5/28/2011 8:11:11 AM

  • GUEST
    Thanks!
    8/18/2011 10:24:31 AM

  • GUEST
    Thanks! These is what I was just looking for a minute ago! 9/1/2011 7:22:38 PM

  • GUEST
    This works great!Thanks 9/2/2011 11:05:53 AM

  • GUEST
    Thanks it is useful. 9/7/2011 11:01:42 AM

  • GUEST
    thank u very much 12/28/2011 8:56:51 AM

  • GUEST
    1/3/2012 6:55:27 AM

  • GUEST
    where i download those files? 2/2/2012 6:31:45 PM

  • GUEST
    think u my fr1nd 2/15/2012 6:51:46 AM

  • GUEST
    super...
    How selected first equal text from ComboBoxlist
    2/15/2012 1:12:02 PM


Comments
   
Captcha Image
For you specially:  
Captcha Text Enter the text in the image.(Not Case sensitive)