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

后端 未结 3 998
一向
一向 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 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;
    

提交回复
热议问题