ComboBox.ObjectCollection.AddRange(Object[]) 方法
定義
重要
部分資訊涉及發行前產品,在發行之前可能會有大幅修改。 Microsoft 對此處提供的資訊,不做任何明確或隱含的瑕疵擔保。
將項目陣列加入至 ComboBox 項目清單中。
public:
void AddRange(cli::array <System::Object ^> ^ items);
public:
void AddRange(... cli::array <System::Object ^> ^ items);
public void AddRange (object[] items);
public void AddRange (params object[] items);
member this.AddRange : obj[] -> unit
Public Sub AddRange (items As Object())
Public Sub AddRange (ParamArray items As Object())
參數
- items
- Object[]
要加入至清單中的物件陣列。
例外狀況
items
參數中的項目為 null
。
範例
下列程式碼範例示範如何藉由設定 text 屬性,以及使用 AddRange 方法來填入 ComboBox 來初始化 ComboBox 控制項。 它也會示範如何處理 DropDown 事件。 若要執行此範例,請將下列程式碼貼到表單中,並從表單的建構函式或 Load
方法呼叫 InitializeComboBox
方法。
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.
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.
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
備註
如果 的 SortedComboBox 屬性設定為 true,專案會依字母順序插入清單。 否則,專案會以陣列內發生的順序插入。 這個方法通常會傳遞 物件的陣列 String ,但任何類型的物件陣列可以傳遞至這個方法。 當物件加入至集合時,方法會呼叫 物件的 ToString 方法,以取得要顯示在清單中的字串。 使用這個方法將專案新增至集合時,您不需要呼叫 BeginUpdate 和 EndUpdate 方法來將效能優化。