Wednesday, March 10, 2010

Combo box Auto Complete Property in C# and VB using .NET

Combo box Auto Complete Property in C# and VB using .NET

When you have a requirement for combo box control like: when user types some text/number then combo box should display/select value as per user typing the characters from keyboard.

Ex: If Combo Box having following items (Source code load the combo box value)

ComboBox1.Items.Add("ABC123")

ComboBox1.Items.Add("AABC123")

ComboBox1.Items.Add("AAABC123")

ComboBox1.Items.Add("AAAABC123")

ComboBox1.Items.Add("CCCEF123")

ComboBox1.Items.Add("1123ABC")

ComboBox1.Items.Add("2234BCD")

ComboBox1.AutoCompleteSource = AutoCompleteSource.ListItems

ComboBox1.DropDownStyle = ComboBoxStyle.DropDownList

and requirement is when user type in the combo box the relative text should display

Ex: When user type "A" then combo box should select "ABC123" value from the list

When user type "AA" then combo box should select "AABCD123" value from the list

When user type "AAA" then combo box should select "AAABC123" value from the list

etc.

To achieve above requirements, very simple in .NET to use "ComboBox1.AutoCompleteMode" property, it has 4 mode and default is "None"

1) AutoCompleteMode.Append
This Mode will append the value as user type the character one by one and select the nearest value in the combo box
2) AutoCompleteMode.Suggest
This Mode will select the nearest value in the combo box when user type ONLY first characher in the combo box and will open (Drop) the combo box to suggest the other nearest possible value present in the combo box
Ex:
3) AutoCompleteMode.SuggestAppend
This Mode will append the value as user type the characters one by one and select the nearest value in the combo box and also will open (Drop) the combo box to suggest the other nearest possible value present in the combo box
4) AutoCompleteMode.None (Default)
This Mode will select the nearest value in the combo box when user type ONLY first characher in the combo box