![]() |
The Combo Box: Creating and Selecting Items |
Like all visual controls, a combo box shares all the basic characteristics of other graphic control: the name, the location, the size, the ability to be enabled or disabled, the ability to hide or show it, the ability to dock or anchor, etc.
|
Probably the most important aspect of a combo box is the list of items it holds. Like the list box and all other list-based controls, the list of a combo box is held by the Items property. You create this list using the exact same techniques we reviewed for the list box. This means that you can use the String Collection Editor. The Items property of the ComboBox class is created from the nested ObjectCollection class. This class has the same functionality and features as its counterpart of the list box. This means that, to programmatically create of items, you can (continuously) call the ObjectCollection.Add() method. Here is an example: public class Exercise : System.Windows.Forms.Form
{
Label lblTitle;
ComboBox cbxAcademicDisciplines;
public Exercise()
{
InitializeComponent();
}
private void InitializeComponent()
{
lblTitle = new Label();
lblTitle.Text = "Academic Disciplines";
lblTitle.Location = new Point(12, 12);
lblTitle.AutoSize = true;
Controls.Add(lblTitle);
cbxAcademicDisciplines = new ComboBox();
cbxAcademicDisciplines.Location = new Point(12, 32);
cbxAcademicDisciplines.Items.Add("Natural sciences");
cbxAcademicDisciplines.Items.Add("Mathematics and Computer sciences");
cbxAcademicDisciplines.Items.Add("Social sciences");
cbxAcademicDisciplines.Items.Add("Humanities");
cbxAcademicDisciplines.Items.Add("Professions and Applied sciences");
Controls.Add(cbxAcademicDisciplines);
}
}
This would produce:
To add an array of items, you can call the AddRange() method. To insert an item somewhere inside the list, you can call the Insert() method. If you want the list of items to be sorted, you can change the value of the Sorted property in the Properties window from False (the default) to True. To sort a list programmatically, you can assign a true value to the Sorted property. You can un-sort the list by changing the value of the Sorted property. This property works exactly like its equivalent in the ListBox control.
To select an item from the list, the user can click it. To programmatically select an item, you can assign a string to the Text property of a DropDown or a Simple combo box. Probably the best way to select an item is to specify its index. The items of a combo box are stored in a zero-based list. To select an item, you can assign its position to the SelectedIndex property. In the same way, to find out what item is selected, you can get the value of the SelectedIndex property. Instead of using of using the index of an item, to select an item using its identity or name, you can use the SelectedItem property. To select an item by its name, assign it to the SelectedItem property.
Instead of simply selecting an item from a combo box, the user may want to find out if a certain string exists in the list. To support this operation, the ComboBox class is equipped with a method named FindString that is overloaded with two versions. One of the syntaxes of this method is: public int FindString(string s); This method takes as argument the string to find in the combo box. If the item is found in the list, the method returns its position. If the list does not have that string, the method return -1. The above syntax of the method would look through the whole list. If you want the search to start at a specific index, you can use the following version of the FindString() method: public int FindString(string s, int startIndex); This version takes as the first argument a string. Instead of start looking for it from the beginning of the list, this method starts at the index specified by the startIndex value. The FindString() method performs its operation without regards to case. This means that it would perform the same search for BlindMan, Blindman, blindMan, or BLINDMAN and would produce the same result for them. If you want the case of the characters to be taken into consideration, use the FindStringExact() method that also is overloaded with two versions. The syntax of the first version is: public int FindStringExact(string s); This method proceeds like the FindString() method by starting to look for the string from the beginning of the list. If you want to specify from where to start looking for the string, you should use the following version: public int FindStringExact(string s, int startIndex);
|
|
|
||
| Previous | Copyright © 2007 FunctionX, Inc. | Next |
|
|
||