Export (0) Print
Expand All

ComboBox.ObjectCollection.AddRange Method

This method supports the .NET Framework infrastructure and is not intended to be used directly from your code.

Adds an array of items to the list of items for a ComboBox.

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

public void AddRange (
	Object[] items
)
public void AddRange (
	Object[] items
)
public function AddRange (
	items : Object[]
)

Parameters

items

An array of objects to add to the list.

Exception typeCondition

ArgumentNullException

An item in the items parameter was a null reference (Nothing in Visual Basic).

If the Sorted property of the ComboBox is set to true, the items are inserted into the list alphabetically. Otherwise, the items are inserted in the order they occur within the array. This method is typically passed an array of String objects, but an array of any type of object can be passed to this method. When an object is added to the collection, the method calls the object's ToString method to obtain the string to display in the list. When using this method to add items to the collection, you do not need to call the BeginUpdate and EndUpdate methods to optimize performance.

The following code example demonstrates how to initialize a ComboBox control by setting the text property and using the AddRange method to populate the ComboBox. It also demonstrates handling the DropDown event. To run the example, paste the following code in a form and call the InitializeComboBox method from the form's constructor or Load method.


// Declare ComboBox1.
internal System.Windows.Forms.ComboBox ComboBox1;

// Initialize ComboBox1.
private void InitializeComboBox()
{
    this.ComboBox1 = new ComboBox();
    this.ComboBox1.Location = new System.Drawing.Point(128, 48);
    this.ComboBox1.Name = "ComboBox1";
    this.ComboBox1.Size = new System.Drawing.Size(100, 21);
    this.ComboBox1.TabIndex = 0;
    this.ComboBox1.Text    = "Typical";
    string[] installs = new string[]{"Typical", "Compact", "Custom"};
    ComboBox1.Items.AddRange(installs);
    this.Controls.Add(this.ComboBox1);
    
    // Hook up the event handler.
    this.ComboBox1.DropDown +=  
        new System.EventHandler(ComboBox1_DropDown);
}

// Handles the ComboBox1 DropDown event. If the user expands the  
// drop-down box, a message box will appear, recommending the
// typical installation.
private void ComboBox1_DropDown(object sender, System.EventArgs e)
{
    MessageBox.Show("Typical installation is strongly recommended.", 
    "Install information", MessageBoxButtons.OK, 
        MessageBoxIcon.Information);
}

// Declare comboBox1.
System.Windows.Forms.ComboBox comboBox1;

// Initialize comboBox1.
private void InitializeComboBox()
{
    this.comboBox1 = new ComboBox();
    this.comboBox1.set_Location(new System.Drawing.Point(128, 48));
    this.comboBox1.set_Name("comboBox1");
    this.comboBox1.set_Size(new System.Drawing.Size(100, 21));
    this.comboBox1.set_TabIndex(0);
    this.comboBox1.set_Text("Typical");
    String installs[] = new String[] { "Typical", "Compact", "Custom" };
    comboBox1.get_Items().AddRange(installs);
    this.get_Controls().Add(this.comboBox1);
    // Hook up the event handler.
    this.comboBox1.add_DropDown(new System.EventHandler(
        comboBox1_DropDown));
} //InitializeComboBox

// Handles the comboBox1 DropDown event. If the user expands the  
// drop-down box, a message box will appear, recommending the
// typical installation.
private void comboBox1_DropDown(Object sender, System.EventArgs e)
{
    MessageBox.Show("Typical installation is strongly recommended.", 
        "Install information", MessageBoxButtons.OK, 
        MessageBoxIcon.Information);
} //comboBox1_DropDown

Windows 98, Windows 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 .NET Framework does not support all versions of every platform. For a list of the supported versions, see System Requirements.

.NET Framework

Supported in: 2.0, 1.1, 1.0

Community Additions

ADD
Show:
© 2014 Microsoft