Wednesday, March 14, 2012

VB.net ComboBox AutoComplete


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

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. 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. 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))

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

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

View the original article here

No comments:

Post a Comment