Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 1023271
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T11:35:28+00:00 2026-05-16T11:35:28+00:00

How do I repesent a single item in a list box in a foreach

  • 0

How do I repesent a single item in a list box in a foreach statement?

I have tried ListBoxItem but System.Windows.Controls is not considered a valid namespace in my .Net framework (version 4).

foreach(ListBoxItem item in listBoxObject.Items)
{
    ...
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-16T11:35:28+00:00Added an answer on May 16, 2026 at 11:35 am

    Typically when someone is looping through listbox items they are looking to determine if they are selected or not. If this is the case, please try using listBoxObject.SelectedItems instead of listBoxObject.Items. This will return only items that have been selected.

    As far as I can tell, there is no ListBoxItem object. You will need to use the Object for each item (which is what seletecteditems and items returns). The Object represents the item’s value, so use it accordingly (meaning, if the object is a string, use it as a string, but if an object is a complex object, use it as such).

    Code Sample:

    foreach (Object listBoxItem in listBoxObject.SelectedItems)
    {
      //Use as object or cast to a more specific type of object.
    }
    

    And if you know what object will ALWAYS be you can cast it in the foreach loop. (Warning: If you’re wrong this will throw an exception). This example is if only Strings are entered into the listbox.

    foreach (String listBoxItem in listBoxObject.SelectedItems)
    {
      //Use as String. It has already been cast. 
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I think I may have stumbled onto a limitation of Flot, but I'm not
I have a bunch of Views in a default folder that represent single static
Say I have an online store wherein each product has a single category (and
Sure, you can have nested lists to represent multidimensional arrays, but that seems costly...
How much should one DataSet represent? Using the example of an ordering system: While
I have a class Parameter, the purpose of which is to represent the possible
My items are listed as follows; This is just a summary of course. But
I have a 'customers' table. Each record contains a customer name, email, address, etc.
From what I understand of REST principles, URLs should represent a single resource, like
I need to represent single precision numbers as text in a way that won't

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.