简体   繁体   中英

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

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].ToString

But this does not return the desired result.

Does anyone know how to do this?

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;

使用此listBox.Items[index].Text

要通过索引从ListBox的项目中获取项目,请使用此方法

string item = listBox1.Items[0];

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM