ArgumentOutOfRangeException adding items to a CheckedListBox.

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

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.
 
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
 
Back
Top