从列表视图中选择指数 [英] Select index from listview
问题描述
我有一些问题让选定行的索引列表视图。我不知道为什么这个代码不工作?我得到的SelectedIndex下方的红线
私人无效lvRegAnimals_SelectedIndexChanged(对象发件人,EventArgs五)
{
INT指数= lvRegAnimals.SelectedIndex;
串specialData = motelManager.GetInfoFromList(指数);
UppdateSpecialData(specialData);
}
帮助是preciated。 !谢谢
编辑:
由于一些奇怪的原因,我得到两个消息时,我点击线路之一在ListView!?首先,我得对上号,然后最后单击的行数。可能是什么问题?
私人无效lvRegAnimals_SelectedIndexChanged(对象发件人,EventArgs五)
{
INT指数= lvRegAnimals.FocusedItem.Index;
MessageBox.Show(Convert.ToString(指数));
}
当我说像这样的检查它的工作现在:
如果(lvRegAnimals.SelectedIndices.Count大于0)
由于的 ListView控件不包含任何的SelectedIndex
,而不是存在的一个属性将selectedIndices 。code>
VAR指数= lvRegAnimals.SelectedIndices;
//指数[0],你可以用它来访问一个选择的索引
< A HREF =http://msdn.microsoft.com/en-us/library/system.windows.forms.listview.selectedindices.aspx> ListView.SelectedIndices
当MultiSelect属性设置为true,则该属性返回包含在
ListView中选定的所有项目的索引的
集合。对于单选择ListView控件,该属性返回包含只包含ListView中所选项目的
的索引单个元素的
集合。
块引用>I'm having some problem to get the index of the selected row in a listview. I wonder why this code isn't working? I get a red line below the SelectedIndex
private void lvRegAnimals_SelectedIndexChanged(object sender, EventArgs e) { int index = lvRegAnimals.SelectedIndex; string specialData = motelManager.GetInfoFromList(index); UppdateSpecialData(specialData); }
Help is preciated. Thanks!
EDIT:
For some strange reason I get two messages when I click on one of the lines in the listView!? First I get the previous number and then the number for the last clicked line. What could be wrong?
private void lvRegAnimals_SelectedIndexChanged(object sender, EventArgs e) { int index = lvRegAnimals.FocusedItem.Index; MessageBox.Show(Convert.ToString(index)); }
It's working now when I added a check like this:
if(lvRegAnimals.SelectedIndices.Count > 0)
解决方案Because ListView doesn't contain any
SelectedIndex
, instead there is a property ofSelectedIndices
.var indices = lvRegAnimals.SelectedIndices; //indices[0] you can use that to access the first selected index
When the MultiSelect property is set to true, this property returns a collection containing the indexes of all items that are selected in the ListView. For a single-selection ListView, this property returns a collection containing a single element containing the index of the only selected item in the ListView.
这篇关于从列表视图中选择指数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!