CheckedListBox.ObjectCollection.Add Method (Object, Boolean)

 
System_CAPS_noteNote

The .NET API Reference documentation has a new home. Visit the .NET API Browser on docs.microsoft.com to see the new experience.

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

Namespace:   System.Windows.Forms
Assembly:  System.Windows.Forms (in System.Windows.Forms.dll)

public int Add(
	object item,
	bool isChecked
)

Parameters

item
Type: System.Object

An object representing the item to add to the collection.

isChecked
Type: System.Boolean

true to check the item; otherwise, false.

Return Value

Type: System.Int32

The index of the newly added item.

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.

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.

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

.NET Framework
Available since 1.1
Return to top
Show: