SOURCE: http://tipsntricksbd.blogspot.com/2007/12/combobox-is-one-of-most-common-gui.html
ComboBox is one of the most common GUI elements. It is used to provide the user with the ability to select an item from a list or enter new text. Here I will show you some common and useful functions of ComboBox in C # using Microsoft Visual Studio.Net 2005.
The simplest ComboBox:
In the simplest case, we will add some lines to the list, as shown below:
myComboBox.Items.Add("Bangladesh"); myComboBox.Items.Add("India"); myComboBox.Items.Add("Pakistan"); myComboBox.Items.Add("Srilanka"); myComboBox.Items.Add("Maldives"); myComboBox.Items.Add("Nepal"); myComboBox.Items.Add("Bhutan");
Sorted List:
Typically, users expect options to be displayed in sorted order. To do this, we need to add one line of code -
myComboBox.Sorted = true;
DropDownStyle:
In ComboBox, the user can either enter text or simply select an item from the list. Therefore, the developer must establish his own style. 3 options are available:
ComboBoxStyle.DropDownList: User can just select one item from a list. ComboBoxStyle.DropDown: User can either type a text or select an item from list. ComboBoxStyle.Simple: User can only type a text in text box. Item list is not shown.
Example:
myComboBox.DropDownStyle = ComboBoxStyle.DropDown;
Suggesstion / Dictionary:
When a user enters text, he / she becomes happy if some prompts are displayed just below the combo box during input. For this function we need to write a couple of lines -
myComboBox.AutoCompleteSource = AutoCompleteSource.ListItems; myComboBox.AutoCompleteMode = AutoCompleteMode.Suggest;
Trick:
There may be times when the user selects some readable text, but for the importance of the programmer (not the selected text). For example, in a database project, StudentID is more important to the programmer than StudentName. So, it would be nice if we could add a combination (Name, Value) to the combo box and during the selection of Name we could easily get the corresponding value.
We can do this by adding an object containing the name and value.
class ComboBoxItem { public string Name; public int Value; public ComboBoxItem(string Name, int Value) { this.Name = Name; this.Value = Value; } } myComboBox.Items.Add(new ComboBoxItem("Ashis Saha",1)); myComboBox.Items.Add(new ComboBoxItem("Subrata Roy", 2)); myComboBox.Items.Add(new ComboBoxItem("Aminul Islam", 3)); myComboBox.Items.Add(new ComboBoxItem("Shakibul Alam", 4)); myComboBox.Items.Add(new ComboBoxItem("Tanvir Ahmed", 5));
But if you now see the ComboBox list, you will notice that all elements are the same, and they are the class names of these objects. In fact, elements are nothing but the output of the ToString () function of these objects. Therefore, if we simply redefine the ToString () function to behave as our expectation, we are done.
class ComboBoxItem { public string Name; public int Value; public ComboBoxItem(string Name, int Value) { this.Name = Name; this.Value = Value; }
You can get the selected value as follows:
int selectedValue = ((ComboBoxItem)myComboBox.SelectedItem).Value;