Export (0) Print
Expand All

ComboBox Class

Represents a Windows combo box control.

Namespace: System.Windows.Forms
Assembly: System.Windows.Forms (in system.windows.forms.dll)

[ComVisibleAttribute(true)] 
[ClassInterfaceAttribute(ClassInterfaceType.AutoDispatch)] 
public class ComboBox : ListControl
/** @attribute ComVisibleAttribute(true) */ 
/** @attribute ClassInterfaceAttribute(ClassInterfaceType.AutoDispatch) */ 
public class ComboBox extends ListControl
ComVisibleAttribute(true) 
ClassInterfaceAttribute(ClassInterfaceType.AutoDispatch) 
public class ComboBox extends ListControl
Not applicable.

A ComboBox displays a text box edit field combined with a ListBox, enabling the user to select items from the list or to enter new text. The default behavior of ComboBox displays an edit field with a hidden drop-down list. The DropDownStyle property determines the style of combo box to display. You can enter a value that provides a simple drop-down, where the list always displays, a drop-down list box, where the text portion is not editable and you must select an arrow to view the drop-down list box, or the default drop-down list box, where the text portion is editable and the user must press the arrow key to view the list. To display a list that the user cannot edit, use a ListBox control.

To add objects to the list at run time, 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.

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.

NoteNote:

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.

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());
        }
    }
}

package Win32Form1Namespace;

import System.*;
import System.Windows.Forms.*;

public class Win32Form1 extends 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();
    } //Win32Form1

    /** @attribute System.STAThreadAttribute()
     */
    public static void main(String[] args)
    {
        System.Windows.Forms.Application.Run(new Win32Form1());
    } //main
    
    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.set_Location(new System.Drawing.Point(248, 32));
        this.addButton.set_Size(new System.Drawing.Size(40, 24));
        this.addButton.set_TabIndex(1);
        this.addButton.set_Text("Add");
        this.addButton.add_Click(new System.EventHandler(this.addButton_Click));
        this.textBox2.set_Location(new System.Drawing.Point(8, 64));
        this.textBox2.set_Size(new System.Drawing.Size(232, 20));
        this.textBox2.set_TabIndex(6);
        this.textBox2.set_Text("");
        this.addGrandButton.set_Location(new System.Drawing.Point(8, 96));
        this.addGrandButton.set_Size(new System.Drawing.Size(280, 23));
        this.addGrandButton.set_TabIndex(2);
        this.addGrandButton.set_Text("Add 1,000 Items");
        this.addGrandButton.add_Click(new System.EventHandler(
            this.addGrandButton_Click));
        this.comboBox1.set_Anchor(System.Windows.Forms.AnchorStyles.Bottom
            | System.Windows.Forms.AnchorStyles.Left
            | System.Windows.Forms.AnchorStyles.Right);
        this.comboBox1.set_DropDownWidth(280);
        this.comboBox1.get_Items().AddRange(new Object[] 
            { "Item 1", "Item 2", "Item 3", "Item 4", "Item 5" });
        this.comboBox1.set_Location(new System.Drawing.Point(8, 248));
        this.comboBox1.set_Size(new System.Drawing.Size(280, 21));
        this.comboBox1.set_TabIndex(7);
        this.showSelectedButton.set_Location(new System.Drawing.Point(8, 128));
        this.showSelectedButton.set_Size(new System.Drawing.Size(280, 24));
        this.showSelectedButton.set_TabIndex(4);
        this.showSelectedButton.set_Text("What Item is Selected?");
        this.showSelectedButton.add_Click(new System.EventHandler(
            this.showSelectedButton_Click));
        this.textBox1.set_Location(new System.Drawing.Point(8, 32));
        this.textBox1.set_Size(new System.Drawing.Size(232, 20));
        this.textBox1.set_TabIndex(5);
        this.textBox1.set_Text("");
        this.findButton.set_Location(new System.Drawing.Point(248, 64));
        this.findButton.set_Size(new System.Drawing.Size(40, 24));
        this.findButton.set_TabIndex(3);
        this.findButton.set_Text("Find");
        this.findButton.add_Click(new System.EventHandler(
            this.findButton_Click));
        this.label1.set_Location(new System.Drawing.Point(8, 224));
        this.label1.set_Size(new System.Drawing.Size(144, 23));
        this.label1.set_TabIndex(0);
        this.label1.set_Text("Test ComboBox");
        this.set_ClientSize(new System.Drawing.Size(292, 273));
        this.get_Controls().AddRange(new System.Windows.Forms.Control[]
            { this.comboBox1, this.textBox2, this.textBox1,
            this.showSelectedButton, this.findButton, this.addGrandButton,
            this.addButton, this.label1 });
        this.set_Text("ComboBox Sample");
    } //InitializeComponent

    private void addButton_Click(Object sender, System.EventArgs e)
    {
        comboBox1.get_Items().Add(textBox1.get_Text());
    } //addButton_Click

    private void addGrandButton_Click(Object sender, System.EventArgs e)
    {
        comboBox1.BeginUpdate();
        for (int i = 0; i < 1000; i++) {
            comboBox1.get_Items().Add("Item 1" + ((Int32)i).ToString());
        }
        comboBox1.EndUpdate();
    } //addGrandButton_Click

    private void findButton_Click(Object sender, System.EventArgs e)
    {
        int index = comboBox1.FindString(textBox2.get_Text());
        comboBox1.set_SelectedIndex(index);
    } //findButton_Click

    private void showSelectedButton_Click(Object sender, System.EventArgs e)
    {
        int selectedIndex = comboBox1.get_SelectedIndex();
        Object selectedItem = comboBox1.get_SelectedItem();
        MessageBox.Show("Selected Item Text: " + selectedItem.ToString() 
            + "\n" + "Index: " + ((Int32)selectedIndex).ToString());
    } //showSelectedButton_Click

The following code example uses ToolStripControlHost and ComboBox to show a ToolStripDropDown as a TreeView.

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 98, Windows Server 2000 SP4, Windows CE, Windows Millennium Edition, Windows Mobile for Pocket PC, Windows Mobile for Smartphone, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition

The Microsoft .NET Framework 3.0 is supported on Windows Vista, Microsoft Windows XP SP2, and Windows Server 2003 SP1.

.NET Framework

Supported in: 3.0, 2.0, 1.1, 1.0

.NET Compact Framework

Supported in: 2.0, 1.0

Community Additions

ADD
Show:
© 2014 Microsoft