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(
	array<Object^>^ items
)

Parameters

items
Type: array<System::Object>

An array of objects to add to the list.

ExceptionCondition
ArgumentNullException

An item in the items parameter was nullptr.

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.

internal:
   // Declare ComboBox1
   System::Windows::Forms::ComboBox^ ComboBox1;

private:
   // Initialize ComboBox1. 
   void InitializeComboBox()
   {
      this->ComboBox1 = gcnew ComboBox;
      this->ComboBox1->Location = System::Drawing::Point( 128, 48 );
      this->ComboBox1->Name = "ComboBox1";
      this->ComboBox1->Size = System::Drawing::Size( 100, 21 );
      this->ComboBox1->TabIndex = 0;
      this->ComboBox1->Text = "Typical";
      array<String^>^ installs = {"Typical","Compact","Custom"};
      ComboBox1->Items->AddRange( installs );
      this->Controls->Add( this->ComboBox1 );

      // Hook up the event handler. 
      this->ComboBox1->DropDown += gcnew System::EventHandler(
         this, &Form1::ComboBox1_DropDown );
   }

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

.NET Framework

Supported in: 4.6, 4.5, 4, 3.5, 3.0, 2.0, 1.1

.NET Framework Client Profile

Supported in: 4, 3.5 SP1
Show:
© 2015 Microsoft