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

How much should one DataSet represent? Using the example of an ordering system: While
Most of time we represent concepts which can never be less than 0. For
How do i represent CRLF using Hex in C#?
How to represent the start and end times for one day? Using October 23,
How can I represent the following in XSD. <price-update> <![CDATA[ arbitrary data goes here
I need to represent inheritance like Person/Student. How do you do that in LInqToSql?
or how can i represent an address to country mapping where an address has
How would you represent a temporal many-to-many relation in SQL? Under non-temporal circumstances one
What is the best way to graphically represent page flow, as applicable to an
Let's say I want to represent \q (or any other particular backslash-escaped character). That

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.