C#: How to get the index of the selected item(and the text) in ListBox

旧时模样 提交于 2019-12-23 02:44:23

问题


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.


回答1:


ListBox.SelectedItem Property:

Gets or sets the currently selected item in the ListBox.

Or, naturally, ListBox.SelectedItems Propery:

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

... Remarks

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.

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.




回答2:


The selected index is in the SelectedIndex property.

The selected text is in the Text property.




回答3:


I think you mean ListView (not ListBox) so use SelectedItems and SelectedIndices properties.



来源:https://stackoverflow.com/questions/789364/c-how-to-get-the-index-of-the-selected-itemand-the-text-in-listbox

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!