Skip to content

Fix issue 15360: Stale selected-item count cache causes IndexOutOfRangeException in ListBox.RefreshItems - #15361

Merged
SimonZhao888 merged 3 commits into
dotnet:mainfrom
SimonZhao888:Fix_Issue_15360
Nov 6, 2026
Merged

SimonZhao888 merged 3 commits into
dotnet:mainfrom
SimonZhao888:Fix_Issue_15360

Conversation

@SimonZhao888

@SimonZhao888 SimonZhao888 commented Nov 4, 2026 •

Copy link
Copy Markdown
Member

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?

  • No

Risk

  • Mini

Screenshots

Before

Screen.Recording.2026-11-04.140939.mp4

After

Screen.Recording.2026-11-04.140227.mp4

Test methodology

  • Test case
  • Manually

Test environment(s)

  • 11.0.0-rc.3.26522.148
Microsoft Reviewers: Open in CodeFlow

@codecov

codecov Bot commented Nov 4, 2026 •

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 39.33306%. Comparing base (374e7cd) to head (7c90b50).
⚠️ Report is 1 commits behind head on main.

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           
Flag Coverage Δ
Debug 39.33306% <ø> (ø)
production 41.80752% <ø> (ø)
test 19.92786% <ø> (ø)
unit 41.80752% <ø> (ø)

Flags with carried forward coverage won't be shown. Click here to find out more.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟡 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 ItemArray instance 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

@SimonZhao888
SimonZhao888 merged commit cc81572 into dotnet:main Nov 6, 2026
8 checks passed
@SimonZhao888
SimonZhao888 deleted the Fix_Issue_15360 branch November 6, 2026 00:51
@dotnet-milestone-bot dotnet-milestone-bot Bot added this to the 12.0-preview1 milestone Nov 6, 2026
KlausLoeffelmann pushed a commit to KlausLoeffelmann/winforms that referenced this pull request Nov 10, 2026
…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]>
@KlausLoeffelmann

Copy link
Copy Markdown
Member

@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?
Does this repo in .NET Framework 2.0? It's kind of hard to believe...

Let's talk about this tomorrow - maybe @Olina-Zhang you might know something about this until then?

@Olina-Zhang

Copy link
Copy Markdown
Member

@KlausLoeffelmann tested in .NET framework 2.0 version, that issue still can be reproduced:
image

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

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

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

6 participants