Microsoft KB Archive/820630

= You receive an &quot;ArgumentOutOfRangeException&quot; exception if you add items to the CheckedListBox control in the BeginUpdate method and the EndUpdate method with the Sorted property set to true =

Article ID: 820630

Article Last Modified on 2/3/2006

-

APPLIES TO


 * Microsoft Visual Studio .NET 2003 Enterprise Architect
 * Microsoft Visual Studio .NET 2003 Enterprise Developer
 * Microsoft Visual Studio .NET 2003 Professional Edition
 * Microsoft Visual Studio .NET 2003 Academic Edition

-



SYMPTOMS
If you add items with the check parameter or the isChecked parameter to the CheckedListBox control in the BeginUpdate method and the EndUpdate method to maintain performance, and you set the Sorted property to True before you add the items, then you may receive the following exception:

An unhandled exception of type 'System.ArgumentOutOfRangeException' occurred in system.windows.forms.dll.

Additional information: Specified argument was out of the range of valid values.



RESOLUTION
To work around this bug, set the Sorted property to True after you add all the items to the CheckedListBox control, as in the following examples.

Microsoft Visual Basic .NET Code CheckedListBox1.BeginUpdate CheckedListBox1.Items.Clear CheckedListBox1.Items.Add(&quot;Item1&quot;, True) CheckedListBox1.EndUpdate CheckedListBox1.Sorted = True Microsoft Visual C# .NET Code checkedListBox1.BeginUpdate; checkedListBox1.Items.Clear; checkedListBox1.Items.Add(&quot;Item1&quot;, true); checkedListBox1.EndUpdate; checkedListBox1.Sorted=true;



STATUS
Microsoft has confirmed that this is a bug in the Microsoft products that are listed at the beginning of this article.



Steps to Reproduce the Behavior
 Create a new Windows application by using Visual Basic .NET or Visual C# .NET. Drag a CheckedListBox control to Form1. Double-click Form1 to open the Form1_Load event.  Add the following code in the Form1_Load event:

Visual Basic .NET Code CheckedListBox1.Sorted = True CheckedListBox1.BeginUpdate CheckedListBox1.Items.Clear CheckedListBox1.Items.Add(&quot;Test&quot;, True) CheckedListBox1.EndUpdate Visual C# .NET Code checkedListBox1.Sorted=true; checkedListBox1.BeginUpdate; checkedListBox1.Items.Clear; checkedListBox1.Items.Add(&quot;Test&quot;, true); checkedListBox1.EndUpdate;  On the Debug menu, click Star.

You may receive the exception that is mentioned in the &quot;Symptoms&quot; section of this article.

<div class="references_section">