Skip to content

Stale selected-item count cache causes IndexOutOfRangeException in ListBox.RefreshItems #15360

Closed

Description

@SimonZhao888

.NET version

11.0.0, 10.0.12

Did it work in .NET Framework?

No

Did it work in any of the earlier releases of .NET Core or .NET 5+?

Not tested/verified

Issue description

The customer's application encounters System.IndexOutOfRangeException while loading a form that contains a CheckedListBox. After the list's items, selection, and checked states have been set, enabling the horizontal scrollbar in the form's Load handler triggers the exception and interrupts the affected control's initialization. The customer reports that the issue occurs with some item counts and checked states but not others.

Screen.Recording.2026-11-04.140939.mp4

Steps to reproduce

  1. Open the desired project in Visual Studio, build it, and start debugging.
  2. During initialization in the constructor, before the control's native handle is created, it adds "A", sets SelectedIndex=0, and reads SelectedItems.Count.
  3. It adds "B" and then sets HorizontalScrollbar=true.
  4. Observe System.IndexOutOfRangeException at the HorizontalScrollbar assignment in the form's constructor, before the form is displayed.

The reproduction code in the form's constructor is:

public ReproForm()
{
    CheckedListBox list = new CheckedListBox { Dock = DockStyle.Fill };
    Controls.Add(list);

    list.Items.Add("A");
    list.SelectedIndex = 0;
    _ = list.SelectedItems.Count;
    list.Items.Add("B");
    list.HorizontalScrollbar = true;
}

Activity

  1. self-assigned this
    on Nov 4, 2026
  2. added
    untriaged
    on Nov 4, 2026
  3. removed
    untriaged
    on Nov 6, 2026
  4. added this to the 12.0-preview1 milestone on Nov 12, 2026
  5. Zheng-Li01 commented on Nov 12, 2026

    @Zheng-Li01
    Member

    Verified the issue with .NET 12 12.0.100-alpha.1.26560.109 build, the issue has been fixed as below screenshot.

    image
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

No labels
No labels

Type

No type

Projects

No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions