Skip to content

Fix CheckedListBox check-state restoration when data binding changes the item count - #15382

Merged
LeafShi1 merged 1 commit into
dotnet:mainfrom
LeafShi1:Fix_15377_CheckedListBox_check-state_restoration
Nov 10, 2026
Merged

LeafShi1 merged 1 commit into
dotnet:mainfrom
LeafShi1:Fix_15377_CheckedListBox_check-state_restoration

Conversation

@LeafShi1

@LeafShi1 LeafShi1 commented Nov 9, 2026 •

Copy link
Copy Markdown
Member

Fixes #15377

Root cause

CheckedListBox.RefreshItems saves check states using the previous item count. The base refresh can repopulate the items from a data source with more entries, but the restoration loop previously used the new item count to index the old state array.

This causes an IndexOutOfRangeException. During DataSource assignment, the exception is caught internally and DisplayMember is cleared.

Proposed changes

  • Limit check-state restoration to the smaller of the saved state length and the refreshed item count.
  • Preserve existing positional check-state restoration, additional items remain unchecked.
  • Add regression tests for initial binding, rebinding, item-count changes, and check-state preservation, with and without a control handle.

Customer Impact

  • Prevents debugger interruptions caused by this first-chance exception during data binding.
  • Keeps the configured DisplayMember intact, so bound items display the selected property rather than their ToString() values.

Regression?

  • No, the same underlying issue can exist in .NET Framework, where a missing saved-state entry causes a NullReferenceException during unboxing. The array-based implementation instead throws an IndexOutOfRangeException

Risk

  • Minimal

Screenshots

Before

WinFormsApp23.zip

Binding a larger data source throws an internal exception. If DisplayMember was assigned first, it can be reset, producing incorrect item labels. The debugger may break even though execution subsequently continues

BeforeChanges.mp4

After

State restoration does not index beyond the saved array. DisplayMember remains configured, retained indices preserve their check states, and additional items remain unchecked.

AfterChanges.mp4

Test methodology

  • Unit test and manual test

Test environment(s)

  • .net 12.0.0-beta.26556.115
Microsoft Reviewers: Open in CodeFlow

@LeafShi1
LeafShi1 requested a review from a team as a code owner November 9, 2026 06:03
@codecov

codecov Bot commented Nov 9, 2026

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 39.79260%. Comparing base (295f067) to head (fdadabd).

Additional details and impacted files
@@              Coverage Diff              @@
##                main      #15382   +/-   ##
=============================================
  Coverage   39.79260%   39.79260%           
=============================================
  Files            246         246           
  Lines           9836        9836           
  Branches        1036        1036           
=============================================
  Hits            3914        3914           
  Misses          5741        5741           
  Partials         181         181           
Flag Coverage Δ
Debug 39.79260% <ø> (ø)
production 42.31695% <ø> (ø)
test 19.92786% <ø> (ø)
unit 42.31695% <ø> (ø)

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.

@LeafShi1
LeafShi1 requested review from SimonZhao888 and ricardobossan and a balanced review from Copilot November 9, 2026 06:49

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.

🟢 Approval recommended

No active review issues block the change to bound check-state restoration when data binding changes the item count.

No open issues 🎉

What changed in this PR

Fixes #15377 by preventing CheckedListBox check-state restoration from indexing beyond saved states when data binding changes the item count, preserving the configured DisplayMember.

Changes:

  • Bound restoration to the smaller of the saved-state length and refreshed item count.
  • Preserve check states by position while leaving additional items unchecked.
  • Add regression tests for binding, rebinding, and item-count changes, with and without a control handle.
File Description
src/​test/​unit/​System.Windows.Forms/​CheckedListBoxTests.cs Adds regression coverage for check-state preservation and bound display text.
src/​System.Windows.Forms/​System/​Windows/​Forms/​Controls/​ListBoxes/​CheckedListBox.cs Limits check-state restoration to valid saved and current indices.

Review effort: Balanced

@SimonZhao888 SimonZhao888 left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

LGTM!

@LeafShi1
LeafShi1 merged commit 8a95231 into dotnet:main Nov 10, 2026
10 checks passed
@LeafShi1
LeafShi1 deleted the Fix_15377_CheckedListBox_check-state_restoration branch November 11, 2026 02:45
@dotnet-milestone-bot dotnet-milestone-bot Bot added this to the 12.0-preview1 milestone Nov 11, 2026
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.

When a CheckedListBox is bound to a List<T>, CheckedListBox.RefreshItems() raises an IndexOutOfRangeException during the DataSource assignment.

4 participants