Repository navigation
Fix CheckedListBox check-state restoration when data binding changes the item count - #15382
Merged
LeafShi1 merged 1 commit intoNov 10, 2026
Conversation
Codecov Report✅ All modified and coverable lines are covered by tests. 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
Flags with carried forward coverage won't be shown. Click here to find out more. 🚀 New features to boost your workflow:
|
LeafShi1
requested review from
SimonZhao888 and
ricardobossan
and
a balanced review from Copilot
November 9, 2026 06:49
Contributor
There was a problem hiding this comment.
🟢 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
Shyam-Gupta
approved these changes
Nov 9, 2026
LeafShi1
deleted the
Fix_15377_CheckedListBox_check-state_restoration
branch
November 11, 2026 02:45
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Fixes #15377
Root cause
CheckedListBox.RefreshItemssaves 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. DuringDataSourceassignment, the exception is caught internally andDisplayMemberis cleared.Proposed changes
Customer Impact
DisplayMemberintact, so bound items display the selected property rather than theirToString()values.Regression?
NullReferenceExceptionduring unboxing. The array-based implementation instead throws anIndexOutOfRangeExceptionRisk
Screenshots
Before
WinFormsApp23.zip
Binding a larger data source throws an internal exception. If
DisplayMemberwas assigned first, it can be reset, producing incorrect item labels. The debugger may break even though execution subsequently continuesBeforeChanges.mp4
After
State restoration does not index beyond the saved array.
DisplayMemberremains configured, retained indices preserve their check states, and additional items remain unchecked.AfterChanges.mp4
Test methodology
Test environment(s)
Microsoft Reviewers: Open in CodeFlow