Using the ComboBox Control


Using the ComboBox Control

The ComboBox control is the ideal control to present a list of choices in a confined amount of screen space. The ComboBox appears as a TextBox control with an arrow on the right-hand side. A list of options drops down below the control when the user clicks the arrow. When the user selects an option or clicks the arrow again, the list of options rolls up again.

Adding items to the ComboBox control can be done both at design time and at runtime. To add items to the ComboBox at design time, simply select the ComboBox in the Form Designer. Then click the ellipsis next to the Items property in the Properties window. This will bring up the String Collection Editor (see Figure 3.9). In the String Collection Editor, enter the list of items to appear in the ComboBox . Each item must appear on a separate line.

Figure 3.9. The String Collection Editor.

graphics/03fig09.jpg

Items can be added to the ComboBox control at runtime, as well. This can be accomplished in two different ways. First, call the Add method on the Items collection property of the ComboBox control. Items can be removed through the Remove method on the Items collection, or all items can be removed by calling the Clear method. The following code snippet adds three strings to a ComboBox control named comboBox1 :

 
 C# comboBox1.Items.Add("Hi"); comboBox1.Items.Add("Howdy"); comboBox1.Items.Add("Wuz Up"); VB comboBox1.Items.Add("Hi") comboBox1.Items.Add("Howdy") comboBox1.Items.Add("Wuz Up") 

You can also add items to a ComboBox at runtime by binding the control to a collection object. This is done by setting the DataSource to the collection object. When the ComboBox attempts to add items to the drop-down list, it will call the ToString method on each item in the DataSource and add that string to the drop-down list. The string can be customized by setting the ComboBox control's DisplayName property. The ComboBox will call the property specified in the DisplayName property and add the returned string to the drop-down list.

Listing 3.1 demonstrates how to bind a ComboBox to a list of custom objects. The Customer class is a custom class that holds the name of a customer. The class has a property named FullName that properly formats the customer's full name. When the ComboBox is bound in the LoadCustomer method, the FullName property is set as the DisplayName .

Listing 3.1
 C# class Customer {   string m_First;   string m_Middle;   string m_Last;   public Customer(string first, string middle, string last) {     m_First = (first == null) ? string.Empty : first;     m_Middle = (middle == null) ? string.Empty : middle;     m_Last = (last == null) ? string.Empty : last;   }   public string FirstName {     get { return m_First; }   }   public string MiddleName {     get { return m_Middle; }   }   public string LastName {     get { return m_Last; }   }   static string FullNameWithInitial = "{0} {1}. {2}";   static string FullNameNoInitial = "{0} {1}";   public string FullName {     get {       return (m_Middle.Length > 0) ?         string.Format(FullNameWithInitial, m_First, m_Middle[0], m_Last) :         string.Format(FullNameNoInitial, m_First, m_Last);     }   } } private void LoadCustomers() {   if(customers != null)     return;     customers = new Customer[6];     customers[0] = new Customer("Ronnie", "Donnell", "Yates");     customers[1] = new Customer("Moya", "Alicia", "Hines");     customers[2] = new Customer("Veronica", "Christine", "Yates");     customers[3] = new Customer("Diane", "", "Taylor");     customers[4] = new Customer("Kindell", "Elisha", "Yates");     customers[5] = new Customer("Zion", "Donnell", "Yates");     this.comboBox1.DataSource = customers;     this.comboBox1.DisplayMember = "FullName"; } VB Public Class Customer     Dim m_First As String     Dim m_Middle As String     Dim m_Last As String     Public Sub New(ByVal first As String, ByVal middle As String,             ByVal last As String)       If first <> Nothing Then         m_First = first       Else         m_First = String.Empty       End If       If middle <> Nothing Then         m_Middle = middle       Else         m_Middle = String.Empty       End If       If last <> Nothing Then         m_Last = last       Else         m_Last = String.Empty       End If     End Sub     Public ReadOnly Property FirstName() As String       Get         Return m_First       End Get     End Property     Public ReadOnly Property MiddleName() As String       Get         Return m_Middle       End Get     End Property     Public ReadOnly Property LastName() As String       Get         Return m_Last       End Get     End Property     Private Shared FullNameWithInitial = "{0} {1}. {2}"     Private Shared FullNameNoInitial = "{0} {1}"     Public ReadOnly Property FullName() As String       Get         If m_Middle.Length > 0 Then           String.Format(FullNameWithInitial, m_First,                   m_Middle.Chars(0), m_Last)       Else         String.Format(FullNameNoInitial, m_First, m_Last)       End If     End Get   End Property End Class Private Sub LoadCustomers()   Dim customers(6) As Customer   customers(0) = New Customer("Ronnie", "Donnell", "Yates")   customers(1) = New Customer("Moya", "Alicia", "Hines")   customers(2) = New Customer("Veronica", "Christine", "Yates")   customers(3) = New Customer("Diane", "", "Taylor")   customers(4) = New Customer("Kindell", "Elisha", "Yates")   customers(5) = New Customer("Zion", "Donnell", "Yates")   ComboBox1.DataSource = customers   ComboBox1.DisplayMember = "FullName" End Sub 

There are two ways to obtain which item is currently selected in the ComboBox . First, the SelectedIndex item property returns the index of the currently selected item. This index can be used to access the selected item from the ComboBox control's Items property. The following code exemplifies the SelectIndex property:

 
 C# string selItem = comboBox1.Items[comboBox1.SelectedIndex].ToString(); VB Dim selItem as string selItem = comboBox1.Items(comboBox1.SelectedIndex).ToString() 

The ComboBox control also provides the SelectedItem property that returns a reference to the currently selected item. Once you have a reference to the currently selected item, you do not need an index into the Items property. The following code demonstrates how to use the SelectedItem property:

 
 C# string selItem = comboBox1.SelectedItem.ToString(); VB Dim selItem as string selItem = comboBox1.SelectedItem.ToString() 


Microsoft.NET Compact Framework Kick Start
Microsoft .NET Compact Framework Kick Start
ISBN: 0672325705
EAN: 2147483647
Year: 2003
Pages: 206

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net