通过


CheckedListBox.ObjectCollection.Add 方法

定义

向项列表添加项 CheckedListBox

重载

名称 说明
Add(Object, Boolean)

将项添加到项 CheckedListBox列表中,指定要添加的对象以及是否选中它。

Add(Object, CheckState)

将项添加到项 CheckedListBox列表中,指定要添加的对象和初始选中的值。

Add(Object, Boolean)

Source:
CheckedListBox.ObjectCollection.cs
Source:
CheckedListBox.ObjectCollection.cs
Source:
CheckedListBox.ObjectCollection.cs
Source:
CheckedListBox.ObjectCollection.cs
Source:
CheckedListBox.ObjectCollection.cs

将项添加到项 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.

返回

新添加的项的索引。

示例

下面的代码示例演示如何通过设置CheckOnClickSelectionMode控件和ThreeDCheckBoxes属性来初始化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)

Source:
CheckedListBox.ObjectCollection.cs
Source:
CheckedListBox.ObjectCollection.cs
Source:
CheckedListBox.ObjectCollection.cs
Source:
CheckedListBox.ObjectCollection.cs
Source:
CheckedListBox.ObjectCollection.cs

将项添加到项 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

返回

新添加的项的索引。

例外

参数 check 不是有效 CheckState 值之一。

注解

此方法将项添加到选中的列表框中。 对于未排序的列表框,该项将添加到现有项列表的末尾。 对于已排序的列表框,该项将根据其排序位置插入到列表中。 如果没有足够的可用空间来存储新项,则会发生此 SystemException 情况。

适用于