Repository navigation
Fix issue 15360: Stale selected-item count cache causes IndexOutOfRangeException in ListBox.RefreshItems - #15361
Conversation
…geException in ListBox.RefreshItems
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## main #15361 +/- ##
=============================================
Coverage 39.33306% 39.33306%
=============================================
Files 246 246
Lines 9806 9806
Branches 1032 1032
=============================================
Hits 3857 3857
Misses 5769 5769
Partials 180 180
Flags with carried forward coverage won't be shown. Click here to find out more. 🚀 New features to boost your workflow:
|
There was a problem hiding this comment.
🟡 Changes recommended
The count cache retains a replaced backing array after items are cleared, keeping removed items alive potentially for the control’s lifetime.
1 open issue
What changed in this PR
Addresses #15360 by making the shared ListBox selected-item count cache distinguish backing arrays, preventing stale counts during item refresh in ListBox and CheckedListBox.
Changes:
- Tracks both the
ItemArrayinstance and its version when caching the selected-item count. - Adds a CheckedListBox regression test for selection preservation when enabling the horizontal scrollbar before handle creation.
| File | Description |
|---|---|
| src/test/unit/System.Windows.Forms/CheckedListBoxTests.cs | Adds the scrollbar-triggered refresh regression test. |
| src/System.Windows.Forms/System/Windows/Forms/Controls/ListBoxes/ListBox.SelectedObjectCollection.cs | Adds backing-array identity to count-cache validation. |
Review effort: Balanced
…geException in ListBox.RefreshItems (dotnet#15361) * Fix issue 15360: Stale selected-item count cache causes IndexOutOfRangeException in ListBox.RefreshItems * Handle feedback * Add comment and update test case --------- Co-authored-by: Simon Zhao (BEYONDSOFT CONSULTING INC) <[email protected]>
|
@LeafShi1: We need to backport this across the board - but I have a bad feeling to do that, since this is allegedly not a regression? @Olina: How far back can you test this on NetFx (so <=4.8.1) without much effort? Let's talk about this tomorrow - maybe @Olina-Zhang you might know something about this until then? |
|
@KlausLoeffelmann tested in .NET framework 2.0 version, that issue still can be reproduced: |


Fixes #15360
Root cause
SelectedObjectCollection.Count cached its value using only ItemArray.Version. When RefreshItems() replaced the backing ItemArray, the new array could have the same version as the old one, causing a stale selected-item count and an IndexOutOfRangeException
Proposed changes
Track both the ItemArray instance and its version when caching the selected-item count. Add a regression test covering selection restoration when enabling HorizontalScrollbar.
Customer Impact
Prevents ListBox and CheckedListBox from crashing during item refresh when selection information has been accessed beforehand. Existing behavior and APIs remain unchanged.
Regression?
Risk
Screenshots
Before
Screen.Recording.2026-11-04.140939.mp4
After
Screen.Recording.2026-11-04.140227.mp4
Test methodology
Test environment(s)
Microsoft Reviewers: Open in CodeFlow