如何在列表视图控件中获取所选项目的索引
问题描述:
大家好,
我有一个列表视图控件,其中充满活力.现在,每当我单击任何项目时,我都应该能够获取该项目的索引..我该怎么做..
thnx,
hi all ,
i have a listview control which populates dyanamically .Now whenever I click on any item i should be able to get the index of the item .. how can i do it ..
thnx ,
答
在SelectedIndexChanged
中使用SelectedIndex
参考: http://msdn.microsoft.com/zh-我们/library/system.web.ui.webcontrols.listview.selectedindexchanged.aspx [您是否已阅读文档(ListView.SelectItems [Did you read the documentation (ListView.SelectItems[^])?
处理ListBox的SelectedIndexChanged事件:
Handle the SelectedIndexChanged event of your ListBox:
private void myListBox_SelectedIndexChanged(object sender, EventArgs e) { ListBox lb = sender as ListBox; if (lb != null) { int index = lb.SelectedIndex; Console.WriteLine(index); } }
仅在SelectionMode为One的情况下有效:对于多选列表框,您需要查看SelectedIndexes列表.
This will only work if the SelectionMode is One: for Multi selection listboxes you need to look at the SelectedIndexes list instead.