ArgumentOutOfRangeException adding items to a CheckedListBox.

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.
 
G

Guest

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top