ComboBox.ObjectCollection.AddRange 方法

此方法支持 .NET 基础结构,但不适合在代码中直接使用。

ComboBox 的项列表添加项的数组。

**命名空间:**System.Windows.Forms
**程序集:**System.Windows.Forms(在 system.windows.forms.dll 中)

语法

声明
Public Sub AddRange ( _
    items As Object() _
)
用法
Dim instance As ObjectCollection
Dim items As Object()

instance.AddRange(items)
public void AddRange (
    Object[] items
)
public:
void AddRange (
    array<Object^>^ items
)
public void AddRange (
    Object[] items
)
public function AddRange (
    items : Object[]
)

参数

  • items
    要向列表添加的对象的数组。

异常

异常类型 条件

ArgumentNullException

items 参数中的一项为 空引用(在 Visual Basic 中为 Nothing)。

备注

如果 ComboBoxSorted 属性设置为真,则按字母顺序将项插入到列表中。否则按项在数组中出现的顺序插入项。通常向此方法传递 String 对象的数组,但可以向此方法传递任何类型的对象的数组。当向集合添加一个对象时,该方法调用此对象的 ToString 方法来获得要在列表中显示的字符串。使用此方法向集合添加项时,不需要调用 BeginUpdateEndUpdate 方法来优化性能。

示例

下面的代码示例演示如何通过设置文本属性并使用 AddRange 方法填充 ComboBox 来初始化 ComboBox 控件。它还演示了如何处理 DropDown 事件。若要运行该示例,请将下面的代码粘贴到一个窗体中,然后在该窗体的构造函数或 Load 方法中调用 InitializeComboBox 方法。

' Declare ComboBox1.
Friend WithEvents ComboBox1 As System.Windows.Forms.ComboBox

' Initialize ComboBox1.
Private Sub InitializeComboBox()
    Me.ComboBox1 = New ComboBox
    Me.ComboBox1.Location = New System.Drawing.Point(128, 48)
    Me.ComboBox1.Name = "ComboBox1"
    Me.ComboBox1.Size = New System.Drawing.Size(100, 21)
    Me.ComboBox1.TabIndex = 0
    Me.ComboBox1.Text = "Typical"
    Dim installs() As String = New String() _
        {"Typical", "Compact", "Custom"}
    ComboBox1.Items.AddRange(installs)
    Me.Controls.Add(Me.ComboBox1)
End Sub

' Handles the ComboBox1 DropDown event. If the user expands the  
' drop-down box, a message box will appear, recommending the
' typical installation.
Private Sub ComboBox1_DropDown _ 
    (ByVal sender As Object, ByVal e As System.EventArgs) _ 
    Handles ComboBox1.DropDown
    MessageBox.Show("Typical installation is strongly recommended.", _
    "Install information", MessageBoxButtons.OK, MessageBoxIcon.Information)
End Sub
// 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);
}
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 );
   }
// 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

.NET Framework 并不是对每个平台的所有版本都提供支持。有关受支持版本的列表,请参见系统要求

版本信息

.NET Framework

受以下版本支持:2.0、1.1、1.0

请参见

参考

ComboBox.ObjectCollection 类
ComboBox.ObjectCollection 成员
System.Windows.Forms 命名空间