如何通过索引从列表框中获取文本? [英] How do you get the text from a listbox by index?

查看:23
本文介绍了如何通过索引从列表框中获取文本?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图通过索引从我的 winForms ListBox 中的条目中获取文本,但我似乎被难住了.我能想到的唯一合乎逻辑的事情是:

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.

有人知道怎么做吗?

推荐答案

你的列表框中有什么?

如果列表框中有字符串值,除了缺少大括号外,您的代码是正确的:

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

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

如果列表框中的事物是某种对象,您可能需要重写 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.

示例:

MyClass my = (MyClass)listBox.Items[index];
string value = my.SomePropertyOfMyClass;

这篇关于如何通过索引从列表框中获取文本?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆