Say cbRoadwayType is my combobox:
for (int i = 0; i < cbRoadwayType.Items.Count; i++)
{
cbRoadwayType.SelectedIndex = i;
int val = (int)cbRoadwayType.SelectedItem // INVALID EXCEPTION cast
if (val != FILTER_ALL && val != FILTER_NOT_SET)
{
roadwayList.Add(val);
}
}
But, I get an invalid exception cast. How do I just get the value of the
combobox item, based on an index?!
Quimbly - 12 Jan 2007 23:58 GMT
Oops, sorry, my original code was:
for (int i = 0; i < cbRoadwayType.Items.Count; i++)
{
int val = (int)cbRoadwayType.Items[i];
if (val != FILTER_ALL && val != FILTER_NOT_SET)
{
roadwayList.Add(val);
}
}
> Say cbRoadwayType is my combobox:
>
[quoted text clipped - 11 lines]
> But, I get an invalid exception cast. How do I just get the value of the
> combobox item, based on an index?!
Peter Thornqvist - 13 Jan 2007 14:32 GMT
Are the combox items ints to begin with?

Signature
Regards, Peter
Chad Z. Hower - 14 Jan 2007 10:22 GMT
> for (int i = 0; i < cbRoadwayType.Items.Count; i++)
> {
[quoted text clipped - 5 lines]
> }
> }
We dont know what type is in your combobox. What did you add?
Furthermore, you should use a foreach instead of a normal for loop in
this situation.

Signature
Chad Z. Hower
Microsoft Regional Director
"Programming is an art form that fights back"
http://www.KudzuWorld.com/
Need a professional technical speaker at your event?
http://www.woo-hoo.net
IKEAS - 14 Jan 2007 17:52 GMT
maby you must use foreach
foreach(object o in combo.items)
{
try
{
int i = (int)o;
.... somes codes
}
catch
{
Console.WritLine("error with object type " + o.GetType().ToString())
}
}
Quimbly a écrit :
> Say cbRoadwayType is my combobox:
>
[quoted text clipped - 11 lines]
> But, I get an invalid exception cast. How do I just get the value of the
> combobox item, based on an index?!
Peter Thornqvist - 14 Jan 2007 21:00 GMT
> maby you must use foreach
It should work either way. I suspect the OP didn't store ints in the
combobox to start with. That's why he gets the invalid cast error

Signature
Regards, Peter