You receive an "ArgumentOutOfRangeException" exception if you add items to the CheckedListBox control in the BeginUpdate method and the EndUpdate method with the Sorted property set to true (820630)



The information in this article applies to:

  • Microsoft Visual Studio .NET (2003), Enterprise Architect Edition
  • Microsoft Visual Studio .NET (2003), Enterprise Developer Edition
  • 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("Item1", True)
   CheckedListBox1.EndUpdate()
   CheckedListBox1.Sorted = True
Microsoft Visual C# .NET Code
   checkedListBox1.BeginUpdate(); 
   checkedListBox1.Items.Clear(); 
   checkedListBox1.Items.Add("Item1", 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.

MORE INFORMATION

Steps to Reproduce the Behavior

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

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

    You may receive the exception that is mentioned in the "Symptoms" section of this article.

REFERENCES

For additional information, click the following article number to view the article in the Microsoft Knowledge Base:

327896 BUG: CheckedListBox Control Loses Check Marks When the Visible Property Is Changed to FALSE and Then Back to TRUE


Modification Type:MinorLast Reviewed:2/3/2006
Keywords:kbvs2005swept kbvs2005doesnotapply kbCtrl kbControl kbListBox kbbug KB820630 kbAudDeveloper