Customizing a ComboBox

G

Guest

I want to create a customized ComboBox where the methods ComboBox.Items.Add
and ComboBox.Items.Insert will behave thusly:
-- If the item is not present, add it.
-- If the item is present, set the selected index to the item (without
adding a duplicate).

I know the basics of inheriting from user controls, so if this was merely
overriding a method of ComboBox I could do it. But how does one go about
overriding a method of the Items collection of ComboBox? I reviewed the MSDN
documentation to no avail.
 
B

bob

Hi Michael,
Suggest an enumerator that iterates over the comboBox Items
collection.
hth
Bob
IEnumerator en = this.comboBox1.Items.GetEnumerator();// combobox with
some stTuples in it
stTuple a = new stTuple();
bool lglFoundIt=false;
a.m = "kvalue";
a.n = "zvalue";
a.o = "xvalue";
while (en.MoveNext())
if (((stTuple)en.Current).Equals(a))
{
this.comboBox1.SelectedItem = en.Current;
lglFoundIt = true;
}

if (!lglFoundIt)
this.comboBox1.Items.Add(a);//Have wait until the
iteration is finished.
 
G

Guest

That is not what I am seeking, Bob. As I stated in my original question, I
want to subclass ComboBox and then customize it to do a lookup, rather than
just invoke a helper method to work with the ComboBox as you have illustrated
(which I already have in place).
 
J

Jeffrey Tan[MSFT]

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.ListControlStringCollectionEditor,
System.Design, Version=2.0.0.0, Culture=neutral,
PublicKeyToken=b03f5f7f11d50a3a", typeof(UITypeEditor)),
MergableProperty(false), SRCategory("CatData"),
DesignerSerializationVisibility(DesignerSerializationVisibility.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/subscriptions/managednewsgroups/default.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/subscriptions/support/default.aspx.
==================================================
This posting is provided "AS IS" with no warranties, and confers no rights.
 
G

Guest

Thank you for the information. I agree it is certainly more complicated than
I expected and/or hoped. I do not think I could justify the effort to design
it thoroughly at this point so I think I shall just live with the slightly
less elegant--but substantially less code--solution of checking before an
insert.
 
J

Jeffrey Tan[MSFT]

Hi Michael,

Yes, I understand that. If you need further help, please feel free to post,
thanks.

Best regards,
Jeffrey Tan
Microsoft Online Community Support
==================================================
Get notification to my posts through email? Please refer to
http://msdn.microsoft.com/subscriptions/managednewsgroups/default.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/subscriptions/support/default.aspx.
==================================================
This posting is provided "AS IS" with no warranties, and confers no rights.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top