using System.Collections; namespace System.Windows.Forms { public partial class CheckedListBox { public new class ObjectCollection : CollectionBase { private CheckedListBox owner { get; } private IList items { get; } public object this[int index] => ((Item)items[index])?.Value; public new int Count => items.Count; public void Add(object obj, CheckState check) { var item = new Item(owner, obj); items.Add(item); item.SetState(check); owner.ClearCheckedItems(); } public void Add(object obj, bool isChecked) => Add(obj, isChecked ? CheckState.Checked : CheckState.Unchecked); public void Add(object obj) => Add(obj, false); public void AddRange(object[] objects) { foreach (object obj in objects) Add(obj); } public void Insert(int index, object obj) { var item = new Item(owner, obj); items.Insert(index, item); item.SetState(CheckState.Unchecked); owner.ClearCheckedItems(); } public void Remove(object obj) { int index = IndexOf(obj); if (index != -1) RemoveAt(index); } public new void RemoveAt(int index) { items.RemoveAt(index); owner.ClearCheckedItems(); } public new void Clear() { items.Clear(); owner.ClearCheckedItems(); } public int IndexOf(object obj) { for (int i = 0; i < Count; i++) { if (this[i] == obj) return i; } return -1; } public bool Contains(object obj) => IndexOf(obj) != -1; internal int IndexOfItem(object item) => items.IndexOf(item); // the reason for this method is that enumerator must return Item.Value rather than Item itself public new IEnumerator GetEnumerator() { foreach (Item item in items) yield return item.Value; } internal ObjectCollection(CheckedListBox owner, IList items) { this.owner = owner; this.items = items; } } } }