共用方式為


CheckedListBox.ObjectCollection.Add 方法

定義

將項目加入至 CheckedListBox 的項目清單。

多載

Add(Object, Boolean)

加入項目至 CheckedListBox 項目清單,指定所要加入的物件,不論它是否已選取。

Add(Object, CheckState)

加入項目至 CheckedListBox 項目清單,指定所要加入的物件和初始選取值。

Add(Object, Boolean)

加入項目至 CheckedListBox 項目清單,指定所要加入的物件,不論它是否已選取。

public:
 int Add(System::Object ^ item, bool isChecked);
public int Add (object item, bool isChecked);
override this.Add : obj * bool -> int
Public Function Add (item As Object, isChecked As Boolean) As Integer

參數

item
Object

物件,表示要加入至集合中的項目。

isChecked
Boolean

若要選取項目,則為 true,否則為 false

傳回

Int32

新加入項目的索引。

範例

下列程式碼範例示範藉由設定 CheckOnClickSelectionModeThreeDCheckBoxes 屬性來初始化 CheckedListBox 控制項。 此範例會 CheckedListBox 填入 控制項,並將 設定 DisplayMemberControl.Name 控制項的 屬性。

若要執行此範例,請將下列程式碼貼到包含 CheckedListBox 名為 CheckedListBox1 的表單中,並從表單的建構函式或 Load 方法呼叫 InitializeCheckListBox 方法。

   // This method initializes CheckedListBox1 with a list of all 
   // the controls on the form. It sets the selection mode
   // to single selection and allows selection with a single click.
   // It adds itself to the list before adding itself to the form.
internal:
   System::Windows::Forms::CheckedListBox^ CheckedListBox1;

private:
   void InitializeCheckedListBox()
   {
      this->CheckedListBox1 = gcnew CheckedListBox;
      this->CheckedListBox1->Location = System::Drawing::Point( 40, 90 );
      this->CheckedListBox1->CheckOnClick = true;
      this->CheckedListBox1->Name = "CheckedListBox1";
      this->CheckedListBox1->Size = System::Drawing::Size( 120, 94 );
      this->CheckedListBox1->TabIndex = 1;
      this->CheckedListBox1->SelectionMode = SelectionMode::One;
      this->CheckedListBox1->ThreeDCheckBoxes = true;
      System::Collections::IEnumerator^ myEnum = this->Controls->GetEnumerator();
      while ( myEnum->MoveNext() )
      {
         Control^ aControl = safe_cast<Control^>(myEnum->Current);
         this->CheckedListBox1->Items->Add( aControl, false );
      }

      this->CheckedListBox1->DisplayMember = "Name";
      this->CheckedListBox1->Items->Add( CheckedListBox1 );
      this->Controls->Add( this->CheckedListBox1 );
   }
// This method initializes CheckedListBox1 with a list of all 
// the controls on the form. It sets the selection mode
// to single selection and allows selection with a single click.
// It adds itself to the list before adding itself to the form.

internal System.Windows.Forms.CheckedListBox CheckedListBox1;

private void InitializeCheckedListBox()
{
    this.CheckedListBox1 = new CheckedListBox();
    this.CheckedListBox1.Location = new System.Drawing.Point(40, 90);
    this.CheckedListBox1.CheckOnClick = true;
    this.CheckedListBox1.Name = "CheckedListBox1";
    this.CheckedListBox1.Size = new System.Drawing.Size(120, 94);
    this.CheckedListBox1.TabIndex = 1;
    this.CheckedListBox1.SelectionMode = SelectionMode.One;
    this.CheckedListBox1.ThreeDCheckBoxes = true;

    foreach ( Control aControl in this.Controls )
    {
        this.CheckedListBox1.Items.Add(aControl, false);
    }

    this.CheckedListBox1.DisplayMember = "Name";
    this.CheckedListBox1.Items.Add(CheckedListBox1);
    this.Controls.Add(this.CheckedListBox1);
}
' This method initializes CheckedListBox1 with a list of all the controls
' on the form. It sets the selection mode to single selection and
' allows selection with a single click. It adds itself to the list before 
' adding itself to the form.
Friend WithEvents CheckedListBox1 As System.Windows.Forms.CheckedListBox

Private Sub InitializeCheckedListBox()
    Me.CheckedListBox1 = New CheckedListBox
    Me.CheckedListBox1.Location = New System.Drawing.Point(40, 90)
    Me.CheckedListBox1.CheckOnClick = True
    Me.CheckedListBox1.Name = "CheckedListBox1"
    Me.CheckedListBox1.Size = New System.Drawing.Size(120, 94)
    Me.CheckedListBox1.TabIndex = 1
    Me.CheckedListBox1.SelectionMode = SelectionMode.One
    Me.CheckedListBox1.ThreeDCheckBoxes = True

    Dim aControl As Control
    For Each aControl In Me.Controls
        Me.CheckedListBox1.Items.Add(aControl, False)
    Next

    Me.CheckedListBox1.DisplayMember = "Name"
    Me.CheckedListBox1.Items.Add(CheckedListBox1)
    Me.Controls.Add(Me.CheckedListBox1)
End Sub

備註

這個方法會將專案新增至清單。 針對清單,專案會新增至現有專案清單的結尾。 針對已排序的清單方塊,專案會根據其排序位置插入清單中。 SystemException如果儲存新專案的空間不足,就會發生 。

適用於

Add(Object, CheckState)

加入項目至 CheckedListBox 項目清單,指定所要加入的物件和初始選取值。

public:
 int Add(System::Object ^ item, System::Windows::Forms::CheckState check);
public int Add (object item, System.Windows.Forms.CheckState check);
override this.Add : obj * System.Windows.Forms.CheckState -> int
Public Function Add (item As Object, check As CheckState) As Integer

參數

item
Object

物件,表示要加入至集合中的項目。

check
CheckState

項目之已選取部分的初始 CheckState

傳回

Int32

新加入項目的索引。

例外狀況

check 參數不是其中一個有效 CheckState 值。

備註

這個方法會將專案新增至核取的清單方塊。 針對未排序的核取清單方塊,專案會新增至現有專案清單的結尾。 針對已排序的清單方塊,專案會根據其排序位置插入清單中。 SystemException如果儲存新專案的空間不足,就會發生 。

適用於