Hi,
I'm developing a CheckedComboBox inherited from ComboBox and it is nearly finished but the problem is that it's underlying Items property is an ObjectCollection. How can I convert it to a CheckedItemsCollections like the one CheckedListBox has If I convert it to some other kind of custom Collection of my own, how the DataSource property of the base ComboBox of my control affected When the DataSource property is changed, what really goes beneath Too many questions, I know, but all for a nice Control, doesn't worth ;)

Custom Item Property for ComboBox
Sumesh Babu
The property is not virtual and therefore you can't change its definition. You could use the new keyword to hide it but that is not likely to work in most cases and would be a design flaw in my opinion. CheckedListBox doesn't hide it either. What it does is define a new property that exposes the items in a more type-safe manner. Nothing prevents users from using the original Items property however. What is different is that the base ListBox class is designed for extensibility and therefore exposes a virtual method to create the underlying Items collection. The CheckedListBox overrides this to create its own custom collection so it can better track the checked state. Nevertheless both properties return the same underlying collection and therefore will remain in sync.
The problem you are facing is that ComboBox is not extensible in this area. It will always create its own collection. Therefore you are stuck with using the collection it uses internally. However you can subscribe to its events and do whatever you want when the underlying collection changes. You could therefore create a parallel collection that maintains whatever additional information you need. You return your custom collection when users use your custom property. You return (or at least the base class will) the real collection whenever users use the Items property. You'll need to override the various methods that are called when the list changes (AddItemCore, SetItemCore, SetItemsCore). You'll also want to override the method called when the data source changes so you can reset your internal collection (OnDataSourceChanged). Finally you might need to override RefreshItem and RefreshItems. Note that some of the methods are obsolete and I don't believe they have replacements. You'll need to override the methods and then run your control through its paces to ensure that you are getting notified correctly so you can update your internal collection properly.
Good luck,
Michael Taylor - 8/17/06
barkingdog
I thinks and am afraid you are right because that will be a long work but I will wait for somedays before going on with these steps. Maybe, someone can give another easier way to accomplish it. Thanks for your help and long and detailed information.