Skip to content

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

Closed

Description

@ricardobossan

Related to feedback 11170483

.NET version

.NET 10

Did it work in .NET Framework?

Yes

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

No response

Issue description

When a CheckedListBox is bound to a List<T>, CheckedListBox.RefreshItems() raises an IndexOutOfRangeException during the DataSource assignment. The exception is caught internally, so the application continues and the items are populated. However, if DisplayMember was assigned before DataSource, the setter's catch path clears DisplayMember, and the list displays each item's ToString() value instead of the selected property.

The relevant code is in CheckedListBox.RefreshItems, ListControl.DataSource, and ListBox.OnDataSourceChanged. Issue 6707 is related because it concerns this method and a reported .NET 6 NullReferenceException.

Steps to reproduce

  1. Create a WinForms application targeting .NET 9 or .NET 10.
  2. Add a CheckedListBox to a form and assign DisplayMember before assigning its DataSource.
  3. Bind it to a List<TextList> containing three items, where TextList has a Name property:
using System.Collections.Generic;
using System.Windows.Forms;

var form = new Form();
var checkedListBox = new CheckedListBox();
form.Controls.Add(checkedListBox);
checkedListBox.DisplayMember = nameof(TextList.Name);
checkedListBox.DataSource = new List<TextList>
{
    new() { Name = "First" },
    new() { Name = "Second" },
    new() { Name = "Third" }
};

sealed class TextList
{
    public string Name { get; set; } = "";
}
  1. Observe the first chance exception during the DataSource assignment. Repeating the binding with a list of a different item count also triggers it.

Expected behavior

The data source should be bound without an exception, and the assigned DisplayMember should remain in effect.

Actual behavior

CheckedListBox.RefreshItems() indexes the saved check state using the newly populated item count, although the saved state was sized using the previous Items.Count. This raises an IndexOutOfRangeException. The exception is swallowed, the application continues, and the items are populated, but DisplayMember is reset to an empty value when it was set before DataSource. Setting DisplayMember after DataSource displays the selected property correctly.

One possible fix is to limit the state restoration loop to the smaller of the saved state length and the current item count.

Image

Activity

  1. added
    untriaged
    on Nov 7, 2026
  2. self-assigned this
    on Nov 9, 2026
  3. removed
    untriaged
    on Nov 10, 2026
  4. Olina-Zhang commented on Nov 12, 2026

    @Olina-Zhang
    Member

    Verified it in the latest .NET 12 build: 12.0.100-alpha.1.26560.109 + dlls built from Winforms repo, this issue was fixed:

    GH15377_Verify.mp4
  5. added this to the 12.0-preview1 milestone on Nov 12, 2026
  6. siggemannen commented on Nov 12, 2026

    @siggemannen

    Hi, i was the original reporter of the issue. I have one question, should one actually restore the selected check in all cases?
    Let's say you have a form with a button that goes something like this:

     public Form1()
     {
         InitializeComponent();
         changeItemsButton.Click += (a, b) => {
             var tl = new List<TextList>();
    
             tl.Add(new TextList() { ID = 4, Name = "Test4" });
             tl.Add(new TextList() { ID = 5, Name = "Test5" });
             tl.Add(new TextList() { ID = 6, Name = "Test6" });
             objectList.DataSource = tl;
         };
         var tl = new List<TextList>();
    
         tl.Add(new TextList() { ID = 1, Name = "Test" });
         tl.Add(new TextList() { ID = 2, Name = "Test2" });
         tl.Add(new TextList() { ID = 3, Name = "Test3" });
         objectList.DataSource = tl;
         objectList.DisplayMember = "Name";
     }

    In this case, the items added are nothing like the previous items. Should one really restore their checked state, just based on the previously selected index?

  7. ricardobossan commented on Nov 13, 2026

    @ricardobossan
    MemberAuthor

    Thanks for asking, @siggemannen. CheckedListBox has preserved check states by index in RefreshItems since the first open-source commit of System.Windows.Forms in 2018. PR #15382 didn't add that behavior; it only limits restoration to indexes present in both lists, preventing the out-of-range exception and the DisplayMember reset when rebinding to a longer list.

    Starting a new data source with no checks would change existing behavior, and some applications may rely on checks staying selected when they rebind the same or refreshed data. That falls outside this fix. If you'd prefer new sources to start unchecked, would you consider opening a separate suggestion issue? As a workaround, you can clear the checks after assigning the new data source:

    objectList.DataSource = tl;
    for (int i = 0; i < objectList.Items.Count; i++)
        objectList.SetItemChecked(i, false);

    @LeafShi1, could you confirm or add anything I've missed?

  8. LeafShi1 commented on Nov 16, 2026

    @LeafShi1
    Member

    could you confirm or add anything I've missed?

    Thanks, Ricardo. You’re right, the current fix only addresses the IndexOutOfRangeException and does not alter the original behavior.

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