11 December 2007

C#: ComboBox Customization

ComboBox is one of the most common GUI elements. It is used to provide the user the facility of selecting an item from a list or enter a new text. Here I’ll show you some common and useful functionalities of ComboBox in C# using Microsoft Visual Studio .Net 2005.

Simplest ComboBox:

In the simplest case, we add some strings in the list like the following-

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:

Generally users expect that the options will be shown in sorted order. For this purpose, we have to add one line of code-


myComboBox.Sorted = true;

DropDownStyle:

In ComboBox, the user can either enter a text or just select an item from the list. So the developer should set its style. There are 3 options available:

  1. ComboBoxStyle.DropDownList: User can just select one item from a list.
  2. ComboBoxStyle.DropDown: User can either type a text or select an item from list.
  3. 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 a text, he/she becomes happy if some suggestions are shown just below the combo box at the time of typing. For this functionality, we need to write couple of lines-

myComboBox.AutoCompleteSource = AutoCompleteSource.ListItems;
myComboBox.AutoCompleteMode = AutoCompleteMode.Suggest;

A Trick:

There may be a case where user selects some readable text, but for the programmer corresponding value (not the selected text) is important. For example, in database project StudentID is more important for a programmer than StudentName. So, it would be nice if we could add (Name, Value) combination in combo box and at the time of Name selection we could easily get the corresponding value.

We can do it by adding an object containing 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 list of ComboBox, you will notice that all items are same and they are the class names of those objects. In fact, the items are nothing but the output of the ToString() function of those objects. So if we simply override 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;
}

// override ToString() function
public override string ToString()

{
return this.Name;
}

}

You can get the selected value in following way-

int selectedValue = ((ComboBoxItem)myComboBox.SelectedItem).Value;

9 Comments:

Bhaboghure Jhor said...

any idea about regular grammar???

Alor Chhota said...

hi Bhaboghure Jhor, firstly i'm sorry for being late to respond.
and.. i found nothing related with regular grammar in C#. if you find, plz let me know.
finally, thanks for writing here...

Bhaboghure Jhor said...

I spend a lot on that occasion but didn't find any suitable, finally I build one for myself. lemme know it u need that.

Alor Chhota said...

oh sure... i'll be glad to get that. thanks in advance :)
my email address: alorchhota@gmail.com

redbrad0 said...

If you are using this code, how can you preselect the default item when the form loads?

Alor Chhota said...

@redbrad0: it's simple. You simply need to mention the selected index. for example,

myComboBox.SelectedIndex = 0;

If you want to select a specific item, then first find that item's index and then change selected index. For example,

int index = myComboBox.Items.IndexOf(YourItem);
if(index>=0) myComboBox.SelectedIndex = index;

unRheal said...

What does Bhaboghure mean by "regular grammar"?

Thanks for the tip, though! :)

Alor Chhota said...

@unRheal: you can check http://en.wikipedia.org/wiki/Regular_grammar for the definition of regular grammar. And you are welcome :)

Waqas Ahmad said...

Thank you very much for such great post. you saved my lot of time.

 

© 2007 t!ps n tr!cks: C#: ComboBox Customization



Template unik dari rohman


---[[ Skip to top ]]---