Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / Windows Forms / WinForm General / January 2007

Tip: Looking for answers? Try searching our database.

How to iterate over a combobox's items?

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Quimbly - 12 Jan 2007 23:46 GMT
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


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.