locked
how to get the items from listbox RRS feed

  • Question

  • hi there

    i have listBox and there are items in the list , i want to get an item as string and getting the item without selection

    i make loop

    for( int i =1 ; i<= listBox.items.Count;i++)

    {

    ////////// how can i get the item to put it in txtbox

     

    }

    Friday, November 4, 2011 4:37 PM

Answers

  • listBox.Items[i].Text or listBox.Items[i].Value
    • Proposed as answer by MusicDemon Friday, November 4, 2011 6:22 PM
    • Marked as answer by Heba Khojah Friday, November 4, 2011 10:07 PM
    Friday, November 4, 2011 4:40 PM

All replies

  • listBox.Items[i].Text or listBox.Items[i].Value
    • Proposed as answer by MusicDemon Friday, November 4, 2011 6:22 PM
    • Marked as answer by Heba Khojah Friday, November 4, 2011 10:07 PM
    Friday, November 4, 2011 4:40 PM
  • or using Linq:

    //no for loop needed!
    List<string> items = listBox1.Items.Cast<String>().ToList();
    MessageBox.Show(String.Join("\n", items.ToArray()));
    

    \n is a delimiter, you can use comma for example.


    Mitja
    Friday, November 4, 2011 9:46 PM
  • listBox.Items[i].Text or listBox.Items[i].Value.ToString();

    Dont forget the ".ToString()" before putting it in a textbox such that:

    textBox1.Text = listBox.Items[i].Text or listBox.Items[i].Value.ToString();

     

    Regards,

     

    • Proposed as answer by Arvin Granados Friday, November 11, 2011 3:40 AM
    Saturday, November 5, 2011 8:57 AM