How do you get the text from a listbox by index?

后端 未结 3 993
一向
一向 2021-01-22 17:40

I am trying to get text from an entry in my winForms ListBox by index, but I seem to be stumped. The only logical thing I can think of is:

listBox.Items[index].T         


        
相关标签:
3条回答
  • 2021-01-22 17:52

    use this listBox.Items[index].Text

    0 讨论(0)
  • 2021-01-22 18:11

    To get the item from a ListBox's items by index please use this way

    string item = listBox1.Items[0];
    
    0 讨论(0)
  • 2021-01-22 18:12

    What do you have in your Listbox?

    If there are string values in the listbox, your code is correct except for missing braces:

    string value = listBox.Items[index].ToString();
    

    If the things in the listbox are some sort of object, you may need to override ToString() to get the desired result, or cast the thing you get out of the listbox to the desired type and then access an appropriate property.

    Example:

    MyClass my = (MyClass)listBox.Items[index];
    string value = my.SomePropertyOfMyClass;
    
    0 讨论(0)
提交回复
热议问题