ListView中的错误:InvalidArgument =值'0'对'index'无效 [英] Error in ListView: InvalidArgument = Value of '0' is not valid for 'index'

查看:260
本文介绍了ListView中的错误:InvalidArgument =值'0'对'index'无效的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我第二次在ListView中选择某些项目时,会出现此错误.我尝试对其进行调试,并且当我第二次选择某个项目时,list_answers.SelectedItems.Count为0.为什么? 这是我的代码:

This error appears when I select some item in ListView for the second time. I tried to debug it, and when I select some item for the second time, the list_answers.SelectedItems.Count is 0. Why? Here is my code:

private void list_answers_SelectedIndexChanged(object sender, EventArgs e)
        {
            ListViewItem t = list_answers.SelectedItems[0];
            for (int i = 0; i < tasks.Count; i++)
            {
                if (t.Text == "Question №" + (i + 1))
                {
                    this.ShowOnePanel(i);
                    iter = i;
                    break;
                }
            }
        }

推荐答案

更改选择时,ListView将首先取消选择当前行,然后选择新行,因此您将有一个调用,其中SelectedItems将为空.

When changing selection, the ListView will first deselect current row and then select new one, so you will have a call where SelectedItems will be empty.

您可以通过添加

if(list_answers.SelectedIndex == -1)
    return;

or

if(list_answers.SelectedItems.Count == 0)
    return;

这篇关于ListView中的错误:InvalidArgument =值'0'对'index'无效的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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