Create a new form, add a CheckedListBox to it. Add the following code to the
form's Load event:

private void Form1_Load(object sender, System.EventArgs e)
{
this.checkedListBox1.BeginUpdate();
this.checkedListBox1.Sorted = true;
try
{
this.checkedListBox1.Items.Add("ABC");
this.checkedListBox1.Items.Add("def", true);
}
finally
{
this.checkedListBox1.EndUpdate();
}
}

If the CheckedListBox is sorted, and you are between a BeginUpdate and
EndUpdate, then an ArgumentOutOfRangeException will be thrown if you try to
add an item to the list and set it's checked state at the same time. Getting
rid of the BeginUpdate/EndUpdate or Sorted property "fixes" this.

RE: ArgumentOutOfRangeException adding items to a CheckedListBox. by SoorajPM

SoorajPM
Wed Oct 20 02:55:04 CDT 2004

Hi

Try to give the code like this:

private void Form1_Load(object sender, System.EventArgs e)
{
this.checkedListBox1.BeginUpdate();
try
{
this.checkedListBox1.Items.Add("ABC");
this.checkedListBox1.Items.Add("def", true);
}
finally
{
this.checkedListBox1.EndUpdate();
this.checkedListBox1.Sorted = true;
}
}

Regards
Sooraj
Microsoft Community Star

"JLeBert" wrote:

> Create a new form, add a CheckedListBox to it. Add the following code to the
> form's Load event:
>
> private void Form1_Load(object sender, System.EventArgs e)
> {
> this.checkedListBox1.BeginUpdate();
> this.checkedListBox1.Sorted = true;
> try
> {
> this.checkedListBox1.Items.Add("ABC");
> this.checkedListBox1.Items.Add("def", true);
> }
> finally
> {
> this.checkedListBox1.EndUpdate();
> }
> }
>
> If the CheckedListBox is sorted, and you are between a BeginUpdate and
> EndUpdate, then an ArgumentOutOfRangeException will be thrown if you try to
> add an item to the list and set it's checked state at the same time. Getting
> rid of the BeginUpdate/EndUpdate or Sorted property "fixes" this.