C#:如何在列表框中获取所选项目(和文本)的索引 [英] C#: How to get the index of the selected item(and the text) in ListBox

查看:708
本文介绍了C#:如何在列表框中获取所选项目(和文本)的索引的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个列表框(详细视图).如何获取所选项目的索引?

I have a listbox (detailed view). How do I get the index of the selected item?

我最终也希望在索引处获得文本,但这应该很容易.

I also eventually want to get the text at the index, but that should be easy.

推荐答案

ListBox.SelectedItem 属性:

获取或设置列表框中当前选定的项目.

Gets or sets the currently selected item in the ListBox.

或者,自然地, ListBox.SelectedItems 属性:

获取一个包含ListBox中当前选定项目的集合.

Gets a collection containing the currently selected items in the ListBox.

...备注

对于多选ListBox,此属性返回一个集合,其中包含在ListBox中选择的所有项目.对于单选ListBox,此属性返回一个包含单个元素的集合,该元素包含ListBox中唯一选定的项目.有关如何操作集合项的更多信息,请参见ListBox .. ::.SelectedObjectCollection.

For a multiple-selection ListBox, this property returns a collection containing all items that are selected in the ListBox. For a single-selection ListBox, this property returns a collection containing a single element containing the only selected item in the ListBox. For more information about how to manipulate the items of the collection, see ListBox..::.SelectedObjectCollection.

ListBox类提供了多种引用所选项目的方法.可以使用SelectedItem属性,而不是使用SelectedItems属性在单选ListBox中获取当前选定的项目.如果要获取当前在列表框中选择的项目的索引位置,而不是项目本身,请使用SelectedIndex属性.此外,如果要获取多选ListBox中所有选定项的索引位置,则可以使用SelectedIndices属性.

The ListBox class provides a number of ways to reference selected items. Instead of using the SelectedItems property to obtain the currently selected item in a single-selection ListBox, you can use the SelectedItem property. If you want to obtain the index position of an item that is currently selected in the ListBox, instead of the item itself, use the SelectedIndex property. In addition, you can use the SelectedIndices property if you want to obtain the index positions of all selected items in a multiple-selection ListBox.

这篇关于C#:如何在列表框中获取所选项目(和文本)的索引的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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