Search - Articles
DevASP.NET for ASP.NET, VB.NET, XML and C# (C-Sharp) Developers Tuesday, March 03, 2009
Dev Articles
Search Directory
ASP.NET
VB.Net
C-Sharp
SQL Server
 

How to use intelli-sense in ComboBox Control

Author: DevASP
Download Source Code : 294_ComboBoxControl.zip

In this article I will try to explain you how you can use intelli-sense in ComboBox Control. Here I will create sample application using Visual Basic.Net (VB.Net).

ComboBox is a combination of a TextBox and a ListBox. The ComboBox displays an editing field (TextBox) combined with a ListBox allowing us to select from the list or to enter new text. ComboBox displays data in a drop-down style format. The ComboBox class is derived from the ListBox class.

 

To start working with this application follow the following steps:

·         Create a new project in Visual Studio 2005 (VS2005).

·         Start a new window application by clicking on the ‘FILE’ menu and then on the ‘PROJECT’. A window will be displayed asking to select the type of your project, name of the project and location of the project. Enter this information and click ‘OK’ to start a new window application.

·         Drag and Drop ComboBox control on window form from your toolbar.

·         Select the ComboBox control by single clicking on it.

·         If the properties window is not displayed, go to view menu and click properties window or simply press F4 to display the properties window.

·         Set the name property to ‘cmbAutoComplete’ in the properties window.

·         Drop two Checkboxes on the form. Set their name in the properties window as ‘cbAutoComplete’ & ‘cbLimitToList’ and their text property to ‘Auto Complete’ and ‘Limit to List’ respectively.

·         Press F7 or select the form in solution explorer and click at ‘view code’ button in solution explorer to open the code window.

·         Go to InitilizeComponent() call.

·         Write the following code after the InitializeComponent() call. This code in necessary to fill the ComboBox control with the values. Let say for example the names of USA Cities/States:

 

cmbAutoComplete.Items.Add("NewYork")

cmbAutoComplete.Items.Add("California")

cmbAutoComplete.Items.Add("Georgia")

cmbAutoComplete.Items.Add("Florida")

cmbAutoComplete.Items.Add("Michigan")

·         Write the following function to implement the auto complete functionality.

 

Public Sub AutoComplete(ByRef cb As ComboBox, _

  ByVal e As System.Windows.Forms.KeyPressEventArgs, _

  Optional ByVal blnLimitToList As Boolean = False)

 

Dim strFindStr As String

 

If e.KeyChar = Chr(8) Then 'Check For Backspace

  If cb.SelectionStart <= 1 Then

     cb.Text = ""

     Exit Sub

  End If

 

If cb.SelectionLength = 0 Then

   strFindStr = cb.Text.Substring(0, cb.Text.Length - 1)

Else

   strFindStr = cb.Text.Substring(0, cb.SelectionStart - 1)

 End If

 

Else

 

If cb.SelectionLength = 0 Then

   strFindStr = cb.Text & e.KeyChar

Else

   strFindStr = cb.Text.Substring(0, cb.SelectionStart) & e.KeyChar

End If

End If

 

Dim intIdx As Integer = -1

 

‘Search the string in the ComboBox List.

intIdx = cb.FindString(strFindStr)

 

If intIdx <> -1 Then ' String found in the List.

 

 cb.SelectedText = ""

 cb.SelectedIndex = intIdx

 cb.SelectionStart = strFindStr.Length

 cb.SelectionLength = cb.Text.Length

 e.Handled = True

 

Else

 

If blnLimitToList = True Then

 

   e.Handled = True

 

Else

   e.Handled = False

 

End If

End If

End Sub

·         Call this function in the KeyPress event of the ComboBox as below.

 

If cbAutoComplete.Checked = True Then

 If cbLimitToList.Checked = True Then

    AutoComplete(cmbAutoComplete, e, True)

 Else

    AutoComplete(cmbAutoComplete, e, False)

 End If

End If

·         Run your application and check this functionality.

Article Comments
Hi
This is very helpful bt it i want one more thing in this. I want to not to accept those items wich are not listed in the combo.

Posted on 4/9/2007 2:28:51 AM by vikas

hi... kindly give me some program codes bout library system or give me some example of your design together with the codes, i need it for our defense. thanks a lot.. i need ur reply asap

Posted on 2/1/2008 12:44:31 PM by janus chagas

I have problem with
cb.SelectionStart
cb.SelectionLength

vb.net says it isn't a member of system.windows.form.comboBox

Posted on 4/29/2008 12:02:54 PM by Adrian

   
Add Article Comment:
Name :
Email Address :
   
Comments :
 
   
<< How to Add, Remove and Clear Items Form ListView Control

Disclaimer - Privacy
© 2002-2017 DevASP.net