So I'm working on this CheckListBox and would like to override the Add() method which adds a new item to the checklistbox, but, the method is not a CheckListBox method, it's a method in the CheckListBox's ObjectCollection.

So, two questions:

  1. Can this method be overriden from within a control that inherits directly from CheckListBox?
  2. Is it possible to completely override the collection the CheckListBox accepts so that I can handle the adding and removing from the list directly. As in, instead of my custom control using CheckListBox.ObjectCollection for items, can it use MyCheckListBox.MyCustomCollection class that inherits from ObjectCollection? If so, I'm not sure how to change that in the control.

I would really appreciate any suggestions on this. I am attempting to add values to the items that depend on the state of the item's check value, and instead of maintaining multiple dictionaries/lists, it would be nice to just use one, each item stands alone with each value stored in its respective class object MyCustomItem or something that would go in the MyCustomCollection.

Any ideas?

  1. You could create your own Add method that passes through to the collection:

     public void Add(object item)
     {
       this.Items.Add(item);
     }
    
  2. The CheckListBox.ObjectCollection that is returned by the Items property is a public non-sealed class so in theory you can extend it:

    public class MyCheckListBox : CheckListBox
    {
      public class MyCustomCollection : CheckListBox.ObjectCollection // possibly needs to extend CheckListBox..::..ObjectCollection
      {
         // override whatever collection methods you need to here
      }
    
      // override the Items property of the CheckListBox to return your new collection type
      public new MyCheckListBox.MyCustomCollection Items { get; }
    }
    

but is that necessary?

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.