Updated: April 2009
Represents a Windows combo box control.
Namespace:
System.Windows.Forms
Assembly:
System.Windows.Forms (in System.Windows.Forms.dll)
Visual Basic (Declaration)
<ClassInterfaceAttribute(ClassInterfaceType.AutoDispatch)> _
<DefaultBindingPropertyAttribute("Text")> _
<ComVisibleAttribute(True)> _
Public Class ComboBox _
Inherits ListControl
[ClassInterfaceAttribute(ClassInterfaceType.AutoDispatch)]
[DefaultBindingPropertyAttribute("Text")]
[ComVisibleAttribute(true)]
public class ComboBox : ListControl
[ClassInterfaceAttribute(ClassInterfaceType::AutoDispatch)]
[DefaultBindingPropertyAttribute(L"Text")]
[ComVisibleAttribute(true)]
public ref class ComboBox : public ListControl
public class ComboBox extends ListControl
A ComboBox displays a text box combined with a ListBox, which enables the user to select items from the list or enter a new value.
The DropDownStyle property specifies whether the list is always displayed or whether the list is displayed in a drop-down. The DropDownStyle property also specifies whether the text portion can be edited. See ComboBoxStyle for the available settings and their effects. There is no setting to always display the list and disallow entering a new value. To display a list to which no new values can be added, use a ListBox control.
To add or remove objects in the list at run time, use methods of the ComboBox..::.ObjectCollection class (through the Items property of the ComboBox). You can assign an array of object references with the AddRange method. The list then displays the default string value for each object. You can add individual objects with the Add method. You can delete items with the Remove()()() method or clear the entire list with the Clear method.
In addition to display and selection functionality, the ComboBox also provides features that enable you to efficiently add items to the ComboBox and to find text within the items of the list. With the BeginUpdate and EndUpdate methods, you can add a large number of items to the ComboBox without the control being repainted each time an item is added to the list. The FindString and FindStringExact methods enable you to search for an item in the list that contains a specific search string.
You can use these properties to manage the currently selected item in the list, the Text property to specify the string displayed in the editing field, the SelectedIndex property to get or set the current item, and the SelectedItem property to get or set a reference to the object.
Note: |
|---|
If you have a ListBox, ComboBox, or CheckedListBox on a base Windows Forms page and want to modify the string collections of those controls in a derived form, the string collections of those controls in the base form must be empty. If the string collections are not empty, they become read-only when you derive another form. |
The following code example is a complete application showing how you can use the Add method to add items to a ComboBox, the FindString method to find items in a ComboBox, and the BeginUpdate and EndUpdate methods to efficiently add a large number items to a ComboBox. The ability to store values that are different from displayed text is inherited from ListControl. For an example of how to use this feature, see the ListControl class.
For instructions about how to run this example in Visual Studio, see How to: Compile and Run a Complete Windows Forms Code Example Using Visual Studio. You must add references to the System.Drawing and System.Windows.Forms namespaces to run this example.
Imports System
Imports System.Windows.Forms
Namespace ComboBoxSampleNamespace
Public Class ComboBoxSample
Inherits System.Windows.Forms.Form
Private addButton As System.Windows.Forms.Button
Private textBox2 As System.Windows.Forms.TextBox
Private addGrandButton As System.Windows.Forms.Button
Private comboBox1 As System.Windows.Forms.ComboBox
Private showSelectedButton As System.Windows.Forms.Button
Private textBox1 As System.Windows.Forms.TextBox
Private findButton As System.Windows.Forms.Button
Private label1 As System.Windows.Forms.Label
Public Sub New()
MyBase.New()
Me.InitializeComponent()
End Sub
<System.STAThreadAttribute()> Public Shared Sub Main()
System.Windows.Forms.Application.Run(New ComboBoxSample())
End Sub
Private Sub InitializeComponent()
Me.addButton = New System.Windows.Forms.Button()
Me.textBox2 = New System.Windows.Forms.TextBox()
Me.addGrandButton = New System.Windows.Forms.Button()
Me.comboBox1 = New System.Windows.Forms.ComboBox()
Me.showSelectedButton = New System.Windows.Forms.Button()
Me.textBox1 = New System.Windows.Forms.TextBox()
Me.findButton = New System.Windows.Forms.Button()
Me.label1 = New System.Windows.Forms.Label()
Me.addButton.Location = New System.Drawing.Point(248, 32)
Me.addButton.Size = New System.Drawing.Size(40, 24)
Me.addButton.TabIndex = 1
Me.addButton.Text = "Add"
AddHandler Me.addButton.Click, AddressOf Me.addButton_Click
Me.textBox2.Location = New System.Drawing.Point(8, 64)
Me.textBox2.Size = New System.Drawing.Size(232, 20)
Me.textBox2.TabIndex = 6
Me.textBox2.Text = ""
Me.addGrandButton.Location = New System.Drawing.Point(8, 96)
Me.addGrandButton.Size = New System.Drawing.Size(280, 23)
Me.addGrandButton.TabIndex = 2
Me.addGrandButton.Text = "Add 1,000 Items"
AddHandler Me.addGrandButton.Click, AddressOf Me.addGrandButton_Click
Me.comboBox1.Anchor = ((System.Windows.Forms.AnchorStyles.Bottom Or System.Windows.Forms.AnchorStyles.Left) _
Or System.Windows.Forms.AnchorStyles.Right)
Me.comboBox1.DropDownWidth = 280
Me.comboBox1.Items.AddRange(New Object() {"Item 1", "Item 2", "Item 3", "Item 4", "Item 5"})
Me.comboBox1.Location = New System.Drawing.Point(8, 248)
Me.comboBox1.Size = New System.Drawing.Size(280, 21)
Me.comboBox1.TabIndex = 7
Me.showSelectedButton.Location = New System.Drawing.Point(8, 128)
Me.showSelectedButton.Size = New System.Drawing.Size(280, 24)
Me.showSelectedButton.TabIndex = 4
Me.showSelectedButton.Text = "What Item is Selected?"
AddHandler Me.showSelectedButton.Click, AddressOf Me.showSelectedButton_Click
Me.textBox1.Location = New System.Drawing.Point(8, 32)
Me.textBox1.Size = New System.Drawing.Size(232, 20)
Me.textBox1.TabIndex = 5
Me.textBox1.Text = ""
Me.findButton.Location = New System.Drawing.Point(248, 64)
Me.findButton.Size = New System.Drawing.Size(40, 24)
Me.findButton.TabIndex = 3
Me.findButton.Text = "Find"
AddHandler Me.findButton.Click, AddressOf Me.findButton_Click
Me.label1.Location = New System.Drawing.Point(8, 224)
Me.label1.Size = New System.Drawing.Size(144, 23)
Me.label1.TabIndex = 0
Me.label1.Text = "Test ComboBox"
Me.ClientSize = New System.Drawing.Size(292, 273)
Me.Controls.AddRange(New System.Windows.Forms.Control() {Me.comboBox1, Me.textBox2, Me.textBox1, Me.showSelectedButton, Me.findButton, Me.addGrandButton, Me.addButton, Me.label1})
Me.Text = "ComboBox Sample"
End Sub
Private Sub addButton_Click(ByVal sender As Object, ByVal e As System.EventArgs)
comboBox1.Items.Add(textBox1.Text)
End Sub
Private Sub findButton_Click(ByVal sender As Object, ByVal e As System.EventArgs)
Dim index As Integer
index = comboBox1.FindString(textBox2.Text)
comboBox1.SelectedIndex = index
End Sub
Private Sub addGrandButton_Click(ByVal sender As Object, ByVal e As System.EventArgs)
comboBox1.BeginUpdate()
Dim I As Integer
For I = 0 To 1000
comboBox1.Items.Add("Item 1" + i.ToString())
Next
comboBox1.EndUpdate()
End Sub
Private Sub showSelectedButton_Click(ByVal sender As Object, ByVal e As System.EventArgs)
Dim selectedIndex As Integer
selectedIndex = comboBox1.SelectedIndex
Dim selectedItem As Object
selectedItem = comboBox1.SelectedItem
MessageBox.Show("Selected Item Text: " & selectedItem.ToString() & Microsoft.VisualBasic.Constants.vbCrLf & _
"Index: " & selectedIndex.ToString())
End Sub
End Class
End Namespace
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() {
this.InitializeComponent();
}
[System.STAThreadAttribute()]
public static void Main() {
System.Windows.Forms.Application.Run(new Win32Form1());
}
private void InitializeComponent() {
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.AnchorStyles.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.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) {
comboBox1.BeginUpdate();
for (int i = 0; i < 1000; i++) {
comboBox1.Items.Add("Item 1" + i.ToString());
}
comboBox1.EndUpdate();
}
private void findButton_Click(object sender, System.EventArgs e) {
int 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: " + selectedIndex.ToString());
}
}
}
#using <System.dll>
#using <System.Drawing.dll>
#using <System.Windows.Forms.dll>
using namespace System;
using namespace System::Windows::Forms;
namespace Win32Form1Namespace
{
public ref class Win32Form1: public System::Windows::Forms::Form
{
private:
System::Windows::Forms::Button^ addButton;
System::Windows::Forms::TextBox^ textBox2;
System::Windows::Forms::Button^ addGrandButton;
System::Windows::Forms::ComboBox^ comboBox1;
System::Windows::Forms::Button^ showSelectedButton;
System::Windows::Forms::TextBox^ textBox1;
System::Windows::Forms::Button^ findButton;
System::Windows::Forms::Label ^ label1;
public:
Win32Form1()
{
this->InitializeComponent();
}
private:
void InitializeComponent()
{
this->addButton = gcnew System::Windows::Forms::Button;
this->textBox2 = gcnew System::Windows::Forms::TextBox;
this->addGrandButton = gcnew System::Windows::Forms::Button;
this->comboBox1 = gcnew System::Windows::Forms::ComboBox;
this->showSelectedButton = gcnew System::Windows::Forms::Button;
this->textBox1 = gcnew System::Windows::Forms::TextBox;
this->findButton = gcnew System::Windows::Forms::Button;
this->label1 = gcnew System::Windows::Forms::Label;
this->addButton->Location = System::Drawing::Point( 248, 32 );
this->addButton->Size = System::Drawing::Size( 40, 24 );
this->addButton->TabIndex = 1;
this->addButton->Text = "Add";
this->addButton->Click += gcnew System::EventHandler(
this, &Win32Form1::addButton_Click );
this->textBox2->Location = System::Drawing::Point( 8, 64 );
this->textBox2->Size = System::Drawing::Size( 232, 20 );
this->textBox2->TabIndex = 6;
this->textBox2->Text = "";
this->addGrandButton->Location = System::Drawing::Point( 8, 96 );
this->addGrandButton->Size = System::Drawing::Size( 280, 23 );
this->addGrandButton->TabIndex = 2;
this->addGrandButton->Text = "Add 1, 000 Items";
this->addGrandButton->Click += gcnew System::EventHandler(
this, &Win32Form1::addGrandButton_Click );
this->comboBox1->Anchor = (System::Windows::Forms::AnchorStyles)(
(System::Windows::Forms::AnchorStyles::Bottom | System::Windows::Forms::AnchorStyles::Left) |
System::Windows::Forms::AnchorStyles::Right);
this->comboBox1->DropDownWidth = 280;
array<Object^>^ objectArray = {"Item 1",
"Item 2",
"Item 3",
"Item 4",
"Item 5"};
this->comboBox1->Items->AddRange( objectArray );
this->comboBox1->Location = System::Drawing::Point( 8, 248 );
this->comboBox1->Size = System::Drawing::Size( 280, 21 );
this->comboBox1->TabIndex = 7;
this->showSelectedButton->Location = System::Drawing::Point( 8, 128 );
this->showSelectedButton->Size = System::Drawing::Size( 280, 24 );
this->showSelectedButton->TabIndex = 4;
this->showSelectedButton->Text = "What Item is Selected?";
this->showSelectedButton->Click += gcnew System::EventHandler(
this, &Win32Form1::showSelectedButton_Click );
this->textBox1->Location = System::Drawing::Point( 8, 32 );
this->textBox1->Size = System::Drawing::Size( 232, 20 );
this->textBox1->TabIndex = 5;
this->textBox1->Text = "";
this->findButton->Location = System::Drawing::Point( 248, 64 );
this->findButton->Size = System::Drawing::Size( 40, 24 );
this->findButton->TabIndex = 3;
this->findButton->Text = "Find";
this->findButton->Click += gcnew System::EventHandler(
this, &Win32Form1::findButton_Click );
this->label1->Location = System::Drawing::Point( 8, 224 );
this->label1->Size = System::Drawing::Size( 144, 23 );
this->label1->TabIndex = 0;
this->label1->Text = "Test ComboBox";
this->ClientSize = System::Drawing::Size( 292, 273 );
array<System::Windows::Forms::Control^>^ controlsArray = {this->comboBox1,
this->textBox2,
this->textBox1,
this->showSelectedButton,
this->findButton,
this->addGrandButton,
this->addButton,
this->label1};
this->Controls->AddRange( controlsArray );
this->Text = "ComboBox Sample";
}
void addButton_Click( Object^ sender, System::EventArgs^ e )
{
comboBox1->Items->Add( textBox1->Text );
}
void addGrandButton_Click( Object^ sender, System::EventArgs^ e )
{
comboBox1->BeginUpdate();
for ( int i = 0; i < 1000; i++ )
{
comboBox1->Items->Add( "Item 1 " + i.ToString() );
}
comboBox1->EndUpdate();
}
void findButton_Click( Object^ sender, System::EventArgs^ e )
{
int index = comboBox1->FindString( textBox2->Text );
comboBox1->SelectedIndex = index;
}
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: " + selectedIndex.ToString() );
}
};
}
[System::STAThreadAttribute]
int main()
{
System::Windows::Forms::Application::Run( gcnew Win32Form1Namespace::Win32Form1 );
}
System..::.Object
System..::.MarshalByRefObject
System.ComponentModel..::.Component
System.Windows.Forms..::.Control
System.Windows.Forms..::.ListControl
System.Windows.Forms..::.ComboBox
Microsoft.VisualBasic.Compatibility.VB6..::.DriveListBox
System.Windows.Forms..::.DataGridViewComboBoxEditingControl
Any public static (Shared in Visual Basic) members of this type are thread safe. Any instance members are not guaranteed to be thread safe.
Windows 7, Windows Vista, Windows XP SP2, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP Starter Edition, Windows Server 2008 R2, Windows Server 2008, Windows Server 2003, Windows Server 2000 SP4, Windows Millennium Edition, Windows 98, Windows CE, Windows Mobile for Smartphone, Windows Mobile for Pocket PC
The .NET Framework and .NET Compact Framework do not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.
.NET Framework
Supported in: 3.5, 3.0, 2.0, 1.1, 1.0
.NET Compact Framework
Supported in: 3.5, 2.0, 1.0
Reference
Date | History | Reason |
|---|
April 2009
| Added instructions for compiling the example and removed a second example that was for a different topic. |
Customer feedback.
|