列表视图中选定索引的问题 [英] Problems with selected indices in listview

查看:22
本文介绍了列表视图中选定索引的问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个包含我自己类的对象的数组列表.我想从数组列表中获取对象,该对象的索引为 listview 的 selectedindex.

I have an arraylist which contain objects of my own class. I want to fetch the object from the array list which has the index = selectedindex of listview.

我试过了:

TrackInformation t=(TrackInformation) SongList[listView1.SelectedIndices[0]];

TrackInformation 是我的类,SongListTrackInformation 类型的 ArrayList.

TrackInformation is my class and SongList is an ArrayList of type TrackInformation.

listview1 不允许选择多个索引,所以我想要 SelectedIndices 集合的第一个元素.

listview1 does not allow multiple indices selection so I want the first element of the SelectedIndices collection.

我收到 ArgumentOutOfRangeException 并且它说 '0' 的值对 'index' 无效.

I am getting ArgumentOutOfRangeException and it says value of '0' is not valid for 'index'.

推荐答案

ListView.SelectedIndexChanged 事件有一个怪癖,它会轰炸您的代码.启动程序时,未选择任何项目.单击一个项目并 SelectedIndexChanged 触发,没问题.现在单击另一个项目,该事件将触发两次.首先让您知道,第一个项目是未选择.然后再次告诉您新项目被选中.第一个事件会让你索引一个空数组,kaboom.RV1987 的代码段阻止了这一点.

The ListView.SelectedIndexChanged event has a quirk that bombs your code. When you start your program, no item is selected. Click an item and SelectedIndexChanged fires, no problem. Now click another item and the event fires twice. First to let you know, unhelpfully, that the first item is unselected. Then again to tell you that the new item is selected. That first event is going to make you index an empty array, kaboom. RV1987's snippet prevents this.

这篇关于列表视图中选定索引的问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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