VB .Net Tutorial

A tutorial on Visual Basic …

Windows forms Combobox Autocomplete

without comments

VB.Net Tutorial:Combobox Autocomplete

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

Written by admin

January 25th, 2011 at 5:43 am

Posted in Classic VB