Hi Michael,
This problem is more complex than it first looks like.
Combobox.Items property only returns the ObjectCollection without much
logic, so Combobox.Items property is not where you want to customize the
logic. The correct place is ObjectCollection.Add/Insert methods.
However, ComboBox.ObjectCollection.Add/Insert methods are both not
overridable. So we have to use "new" C# keyword to hide its base version
while creating a new one ourselves. In these 2 methods, you may add the
logic of searching the existing items list(through base[index]) and return
the existing item index or the new added item index(if none is found).
In Combobox, you still need to replace the default implementation of
Combobox.Items property with our new created ComboBox.ObjectCollection so
that the new version of "Add/Insert" methods logic can be used. The problem
is ComboBox.Items property is also not overridable. We still have to use
"new" C# keyword to hide its base version and use the new collection. Below
code snippet demonstrates the basic logic:
namespace CustomizeCombobox
{
public partial class MyComboBox : ComboBox
{
public MyComboBox()
{
}
private MyObjectCollection myitemsCollection = null;
new public MyObjectCollection Items
{
get
{
if (myitemsCollection == null)
{
myitemsCollection = new MyObjectCollection(this);
}
return myitemsCollection;
}
}
public class MyObjectCollection : ComboBox.ObjectCollection
{
public MyObjectCollection(ComboBox owner): base(owner)
{
}
new public int Add(object item)
{
//lookup object collection for existing items.
//return the existing item index or new added item index
}
new public void Insert(int index, object item)
{
//lookup object collection for existing items.
//return the existing item index or new added item index
}
}
}
}
Note: this is only the basic logic. There may be some other problems. For
example, Combobox.Items property has a customized "EditorAttribute" to
provide the design-time editor feature, like below:
[Editor("System.Windows.Forms.Design.ListControlStr ingCollectionEditor,
System.Design, Version=2.0.0.0, Culture=neutral,
PublicKeyToken=b03f5f7f11d50a3a", typeof(UITypeEditor)),
MergableProperty(false), SRCategory("CatData"),
DesignerSerializationVisibility(DesignerSerializat ionVisibility.Content),
Localizable(true), SRDescription("ComboBoxItemsDescr")]
public ObjectCollection Items
{
get
{
if (this.itemsCollection == null)
{
this.itemsCollection = new ObjectCollection(this);
}
return this.itemsCollection;
}
}
So if your new Items property want to get design-time feature, you need
more effort to adding EditorAttribute.
Hope this helps.
Best regards,
Jeffrey Tan
Microsoft Online Community Support
==================================================
Get notification to my posts through email? Please refer to
http://msdn.microsoft.com/subscripti...ult.aspx#notif
ications.
Note: The MSDN Managed Newsgroup support offering is for non-urgent issues
where an initial response from the community or a Microsoft Support
Engineer within 1 business day is acceptable. Please note that each follow
up response may take approximately 2 business days as the support
professional working with you may need further investigation to reach the
most efficient resolution. The offering is not appropriate for situations
that require urgent, real-time or phone-based interactions or complex
project analysis and dump analysis issues. Issues of this nature are best
handled working with a dedicated Microsoft Support Engineer by contacting
Microsoft Customer Support Services (CSS) at
http://msdn.microsoft.com/subscripti...t/default.aspx.
==================================================
This posting is provided "AS IS" with no warranties, and confers no rights.