ComboBox class
Represents a Windows Combination box control.
For a list of all members of this type, see ComboBox members.
System.Object System.MarshalByrefObject System.comPonentModel.component System.Windows.Forms.Control System.Windows.Forms.ListControl System.Windows.Forms.comBobox
[Visual Basic]
Public Class ComboBox
Inherits ListControl
[C #]
Public Class ComboBox: ListControl
[C ]
Public __gc class combobox: public ListControl
[Jscript]
Public Class ComboBox Extends ListControl
Thread security
All public static (shared in Visual Basic) is a thread safe. But not to ensure that any instance member is thread safe.
Note
ComboBox displays the editing field combined with a ListBox, allows users to select or enter new text from the list. The default behavior of ComboBox is to display an editing field that comes with a hidden drop-down list. The DropDownStyle property determines the style of the combo box to display. You can enter a value that allows you to allow the following: Simple drop-down list (always display list), drop-down list box (text section is not editable, and you must select an arrow to view the drop-down list box) or the default drop-down list box (text Partially editable, and users must press the arrow keys to view the list). To always display a list that users cannot edit, use the ListBox control.
To add an object to a list at runtime, use the AddRange method to assign an object reference array. Then, the list shows the default string value of each object. You can add a single object with the Add method.
In addition to display and selection, ComboBox also provides some features to enable you to effectively find items to the ComboBox, and in the list item. BeginUpdate and EndUpdate methods allow you to add a large number to ComboBox without having to re-draw control at each time you add an item. FindString and FindStringexact methods allow you to search for items containing specific search strings in the list.
You can use the currently selected items in the list in these properties. Use the Text property to specify the string displayed in the edit field. Use the SELECTEDEX property to get or set the current item, and use the SelectedItem property to get or set a reference to the object.
Example
[Visual Basic, C #, C ] The following example is a complete application that explains how to use the Add method to add items to the ComboBox, how to use the FINDSTRING method to find items in ComboBox, and how to use the BeginUpdate and EndUpdate methods to efficient way ComboBOX adds a lot of items.
[C #]
Using system;
Using system.windows.forms;
Namespace Win32form1namespace {
Public class win32form1: system.windows.Forms.form {
Private system.windows.Forms.Button AddButton;
Private system.windows.Forms.TextBox textBox2; private system.windows.Forms.Button AddgrandButton
Private system.windows.Forms.comBobox ComboBox1;
Private system.windows.Forms.Button showselectedButton;
Private system.windows.Forms.TextBox textBox1;
Private system.windows.Forms.Button Findbutton;
Private system.windows.Forms.Label label1;
Public Win32Form1 () {
THISINIALIZEC MONENT ();
}
[System.stathreadAttribute ()]
Public static void
Main
() {
System.windows.Forms.Application.run (New Win32form1 ());
}
Private vidinitiRizeComponent () {
This.addButton = new system.windows.forms.button ();
This.TextBox2 = new system.windows.Forms.TextBox ();
This.AddgrandButton = new system.windows.forms.button ();
THIS.comBOBOX1 = New System.windows.Forms.comBobox ();
This.ShowSelectedButton = new system.windows.Forms.Button ();
This.TextBox1 = new system.windows.Forms.TextBox ();
This.FindButton = new system.windows.Forms.Button ();
THIS.LABEL1 = New System.windows.Forms.label ();
this.addbutton.location = new system.drawing.point (248, 32);
this.addbutton.size = new system.drawing.size (40, 24);
this.addButton.tabindex = 1;
this.addButton.text = "add";
This.addButton.Click = new system.eventhandler (this.addbutton_click);
this.TextBox2.Location = new system.drawing.point (8, 64);
THIS.TEXTBOX2.SIZE = New System.drawing.size (232, 20);
THIS.TEXTBOX2.TABINDEX = 6;
THIS.TEXTBOX2.TEXT = "";
This.addgrandButton.location = new system.drawing.point (8, 96);
this.addgrandbutton.size = new system.drawing.size (280, 23);
this.addgrandbutton.tabindex = 2;
this.addgrandButton.text = "Add 1,000 items";
this.addGrandButton.Click = new System.EventHandler (this.addGrandButton_Click); this.comboBox1.Anchor = ((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Left)
System.windows.Forms.AnchoNchorstyles.Right;
this.comboBox1.dropdownWidth = 280;
this.combobox1.items.addrange (new object [] {"item 1",
Item 2,
"Item 3",
Item 4,
"Item 5"});
This.comboBOX1.Location = New System.drawing.Point (8, 248);
This.comboBOX1.Size = New System.drawing.size (280, 21);
This.combobox1.tabindex = 7;
This.ShowSelectedButton.Location = New System.drawing.Point (8, 128);
This.ShowSelectedButton.size = new system.drawing.size (280, 24);
This.ShowSelectedButton.tabindex = 4;
THIS.SHOWSELECTEDBUTTON.TEXT = "What item is SELECTED?";
This.ShowSelectedButton.Click = new system.EventHandler (this.showselectedButton_Click);
This.TextBox1.Location = new system.drawing.point (8, 32);
This.TextBox1.size = new system.drawing.size (232, 20);
This.TextBox1.tabindex = 5;
THIS.TEXTBOX1.TEXT = "";
This.findButton.Location = new system.drawing.point (248, 64);
This.findButton.size = new system.drawing.size (40, 24);
This.findButton.tabindex = 3;
This.findButton.text = "find";
This.FindButton.Click = new system.eventhandler (this.findbutton_click);
This.Label1.Location = new system.drawing.point (8, 224);
This.label1.size = new system.drawing.size (144, 23);
this.label1.tabindex = 0;
THIS.Label1.Text = "Test ComboBox";
THIS.AUTOSCALEBASESIZE = New System.drawing.size (5, 13);
THIS.CLIENTSIZE = New System.drawing.size (292, 273);
this.controls.addrange (new system.windows.forms.control [] {this.combobox1, this.textbox2,
this.TextBox1,
This.ShowSelectedButton,
This.FindButton,
this.addgrandButton,
this.addbutton,
THIS.LABEL1});
This.Text = "ComboBox Sample";
}
Private Void AddButton_Click (Object Sender, System.EventArgs E) {
ComboBox1.items.Add (TextBox1.text);
}
Private void addgrandbutton_click (object sender, system.eventargs e) {
ComboBOBOX1.BEGINUPDATE ();
For (int i = 0; i <1000; i ) {
ComboBox1.Items.Add ("Item 1" i.toString ());
}
ComboBOBOX1.Endupdate ();
}
Private void Findbutton_Click (Object Sender, System.EventArgs E) {
INDEX = ComboBox1.FindString (TextBox2.text);
ComboBoX1.SelectedIndex = index;
}
Private void showselectedButton_Click (Object sender, system.eventargs e) {
INT selectedindex = combobox1.selectedIndex;
Object selecteditem = combobox1.selectedItem;
Messagebox.show ("SELECTED ITEM TEXT:" SELECTEDITEM.TOSTRING () "/ n"
"INDEX:" SELECTEDINDINDEX.TOSTRING ());
}
}
}