Olvasás angol nyelven Szerkesztés

Megosztás a következőn keresztül:


CheckedListBox.ObjectCollection.Add Method

Definition

Adds an item to the list of items for a CheckedListBox.

Overloads

Add(Object, Boolean)

Adds an item to the list of items for a CheckedListBox, specifying the object to add and whether it is checked.

Add(Object, CheckState)

Adds an item to the list of items for a CheckedListBox, specifying the object to add and the initial checked value.

Add(Object, Boolean)

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

Adds an item to the list of items for a CheckedListBox, specifying the object to add and whether it is checked.

C#
public int Add (object item, bool isChecked);

Parameters

item
Object

An object representing the item to add to the collection.

isChecked
Boolean

true to check the item; otherwise, false.

Returns

The index of the newly added item.

Examples

The following code example demonstrates initializing a CheckedListBox control by setting the CheckOnClick, SelectionMode, and ThreeDCheckBoxes properties. The example populates the CheckedListBox with controls and sets the DisplayMember to the Control.Name property of the control.

To run the example, paste the following code in a form containing a CheckedListBox named CheckedListBox1 and call the InitializeCheckListBox method from the form's constructor or Load method.

C#
// 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);
}

Remarks

This method adds an item to the list. For a list, the item is added to the end of the existing list of items. For a sorted checked list box, the item is inserted into the list according to its sorted position. A SystemException occurs if there is insufficient space available to store the new item.

Applies to

.NET Framework 4.8.1 és más verziók
Termék Verziók
.NET Framework 1.1, 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
Windows Desktop 3.0, 3.1, 5, 6, 7, 8, 9

Add(Object, CheckState)

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

Adds an item to the list of items for a CheckedListBox, specifying the object to add and the initial checked value.

C#
public int Add (object item, System.Windows.Forms.CheckState check);

Parameters

item
Object

An object representing the item to add to the collection.

check
CheckState

The initial CheckState for the checked portion of the item.

Returns

The index of the newly added item.

Exceptions

The check parameter is not one of the valid CheckState values.

Remarks

This method adds an item to the checked list box. For an unsorted checked list box, the item is added to the end of the existing list of items. For a sorted checked list box, the item is inserted into the list according to its sorted position. A SystemException occurs if there is insufficient space available to store the new item.

Applies to

.NET Framework 4.8.1 és más verziók
Termék Verziók
.NET Framework 1.1, 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
Windows Desktop 3.0, 3.1, 5, 6, 7, 8, 9