473,398 Members | 2,404 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,398 software developers and data experts.

Customizing a ComboBox

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.
Mar 27 '07 #1
5 6770
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.
On Tue, 27 Mar 2007 15:56:47 -0700, michael sorens
<mi***********@discussions.microsoft.comwrote:
>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.
Mar 28 '07 #2
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).

Mar 28 '07 #3
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.
Apr 13 '07 #4
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.
Apr 16 '07 #5
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/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.
Apr 17 '07 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Peter Schwalm | last post by:
I'd like to modify the python search path depending on the source directory of the script being started. The reason is: I use a version control system, and the python scripts and modules are...
6
by: Jason | last post by:
Sorry for the post here but could not find an Exchange newgroup. I developed an application a year or 2 ago that sends SMTP email. The application allows the user to customize the text from...
1
by: Job Lot | last post by:
How can embed company logo in title section of setup dialog box?
2
by: Job Lot | last post by:
I am customizing DataGridTextBoxColumn. How can I access properties in DataGrid to which custom DataGridTextBoxColumn will be added. Say for instance, how I would access SelectionBackColor...
1
by: kjcox | last post by:
I've been using DocBook and XML for quite some time now. It works great for making simple HTML pages, http://gnuware.com/icecast/. I normally use it on RHEL and do not customize anything other than...
4
by: VR | last post by:
I am trying to embed a check box into a FlexGrid's cell, but having a problem when I start scrolling the grid. Here is my MyCheckBox class... class MyCheckBox : CheckBox { void Init (...
1
by: Bui Trung Nguyen | last post by:
How can I customize Datagrid in ,NET such as DataGrid in VB 6.0 (I can add column with combobox button) thanks
2
by: Peter Stojkovic | last post by:
I want do something like in MS-Outlook 2002 There is the text for the main menues AND on the right side there is a input-Field / ComboBox for searching help . the text that appears is "type a...
1
by: =?Utf-8?B?bWljaGFlbCBzb3JlbnM=?= | last post by:
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...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.