Search code examples
c#winformscheckedlistbox

CheckedListBox.RemoveAt(lastItemIndex) causes a display issue in the list


I have a checkedlistbox that's bound to a generic list of custom type. When I remove other items on the list, the display is okay, but when I remove the last item on the list, the list shows up w/ the type name instead of the displayname.

_selResolutions.RemoveAt(selIndex);
cklResolutions.DataSource = null;
cklResolutions.BeginUpdate();
cklResolutions.DataSource = _selResolutions;
cklResolutions.DisplayMember = "LongDesc";
cklResolutions.ValueMember = "LongDesc";
cklResolutions.EndUpdate();
for (var i = 0; i < _selResolutions.Count; i++)
{
    cklResolutions.SetItemChecked(i, _selResolutions[i].Selected);
}

the display looks like this when the last item is removed w/ the above code.

[ ] Resolution
[ ] Resolution
[ ] Resolution
[ ] Resolution
[ ] Resolution

why is this happening? am i missing something here? how do i correct this?


Solution

  • Try using a BindingSource instead. This provides the DataSource a view that can inform the DGV of changes:

    using System;
    using System.Collections.Generic;
    using System.Windows.Forms;
    
    class Form1 : Form
    {
        [STAThread]
        static void Main()
        {
            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);
            Application.Run(new Form1());
        }
    
        List<MyItem> _selResolutions;
        CheckedListBox cklResolutions;
    
        public Form1()
        {
            Controls.Add(cklResolutions = new CheckedListBox { Dock = DockStyle.Fill });
    
            _selResolutions = new List<MyItem>();
            _selResolutions.Add(new MyItem { LongDesc = "One", Selected = true });
            _selResolutions.Add(new MyItem { LongDesc = "Two", Selected = false });
            _selResolutions.Add(new MyItem { LongDesc = "Three", Selected = false });
            _selResolutions.Add(new MyItem { LongDesc = "Four", Selected = true });
    
            cklResolutions.DataSource = new BindingSource(_selResolutions, null);
            cklResolutions.DisplayMember = cklResolutions.ValueMember = "LongDesc";
    
            UpdateCheckBoxes();
    
            cklResolutions.KeyUp += new KeyEventHandler(cklResolutions_KeyUp);
        }
    
        private void UpdateCheckBoxes()
        {
            for (int n = 0; n < _selResolutions.Count; n++)
                cklResolutions.SetItemChecked(n, _selResolutions[n].Selected);
        }
    
        void cklResolutions_KeyUp(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Delete)
            {
                int index = cklResolutions.SelectedIndex;
                if (index >= 0)
                {
                    BindingSource bs = cklResolutions.DataSource as BindingSource;
                    bs.RemoveAt(index);
                    UpdateCheckBoxes();
                }
            }
        }
    }
    
    class MyItem
    {
        public string LongDesc { get; set; }
        public bool Selected { get; set; }
    }