Need a combobox with typed items

A

active

I need a combobox where the items are typed as string - not object.

And another where the items are from a class I generated.

Can one make such comboboxes from System.Windows.Forms.ComboBox?
If that is not practical I appreciate you telling me that?



Thanks in advance
 
J

John B

active said:
I need a combobox where the items are typed as string - not object.

And another where the items are from a class I generated.

Can one make such comboboxes from System.Windows.Forms.ComboBox?
If that is not practical I appreciate you telling me that?



Thanks in advance
It would be possible to create a generic control to do this but...
1) is a cast acceptable at run time from the loosely typed items collection.
2) not sure how much luck you'd have validating item addition so you
might have to roll your own combo, not a trivial task i'd imagine
3) I dont think you have any way of creating generic controls with the
visual designer.

Experiment with creating a class such as:

public class ComboEx<T> : System.Windows.Forms.ComboBox{
}

and go from there.

JB
 
A

active

John B said:
It would be possible to create a generic control to do this but...
1) is a cast acceptable at run time from the loosely typed items
collection.
If I understand: That's what I want to get away from with the
Forms.ComboBox
2) not sure how much luck you'd have validating item addition so you might
have to roll your own combo, not a trivial task i'd imagine

You think if I did what you said below and added AddItem and AddItemRange
with typed arguments (and ignored the Item.Add and Item.AddRange) - sound
like it would work.

I just don't have much insight into this - which is why I'm asking.


Thanks
 
J

John B

active said:
It would be possible to create a generic control to do this but...
1) is a cast acceptable at run time from the loosely typed items
collection.
If I understand: That's what I want to get away from with the
Forms.ComboBox
[/QUOTE]
I agree but you are limited in the fact that you need to add the items
to the base combo's collection so it displays them.
You could keep another collection (strong typed) and just return that as
the items property but you'd need to ensure it was readonly or you'd
have to be notified when items are added.
You think if I did what you said below and added AddItem and AddItemRange
with typed arguments (and ignored the Item.Add and Item.AddRange) - sound
like it would work.
It would work yes, but it depends on how much validation you want.
You would have to determine whether you wanted to absolutely prevent
adding to the items collection (by casting to combobox) or just
accept/hope that it would not happen.
There is a SetItemCore and SetItemsCore method but it appears they only
get called when you set a datasource.
JB
<...>
 
R

RobinS

The combobox items are always objects. Everything in .Net is an object,
that's the cornerstone of the whole building.

Why not create a list and bind the combobox to the list? You can use
List(Of String) or List(Of Customers) to be specific.

Robin S.
 
A

active

Per your last statement, I had searched the internet for "strongly typed"
ComboBox and got many hit - all seem to relate to datasource



Thanks for the help



Thanks
 
A

active

RobinS said:
The combobox items are always objects. Everything in .Net is an object,
that's the cornerstone of the whole building.

I should have used "Object" instead of "object"
Why not create a list and bind the combobox to the list? You can use
List(Of String) or List(Of Customers) to be specific.
I'm going to look into this

thanks
 

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