Export (0) Print
Expand All

ComboBox.ObjectCollection.AddRange Method

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
)

Parameters

items
Type: System.Object[]

An array of objects to add to the list.

ExceptionCondition
ArgumentNullException

An item in the items parameter was null.

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

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

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

Community Additions

ADD
Show:
© 2014 Microsoft