ListView.SelectedItems [0]选择错误

时间:2015-03-15 01:55:59

标签: c# listview foreach

为什么我的程序会返回以下错误?

请特别感谢。

PS:无论出于何种原因listview中只有一个项目可以使用。

  

错误: InvalidArgument =' 1'的值对于' index'无效。参数名称:index

代码:

if(e.Control == true)
            {
                try
                {
                    string s = "";
                    string sCheck = "";
                    int select = 0;
                    int i = 0;
                    int position = 0;

                    if (i == 1)
                    {
                        i--;
                    }

                    if(select >= 1)
                    {
                        do
                        {
                            select--;
                        } while (select >= 1);
                    }

                    if (position >= 1)
                    {
                        do
                        {
                            position--;
                        } while (position >= 1);
                    }

                    foreach (object item1 in listView1.Items)
                    {
                        if (item1 == listView1.SelectedItems[select])
                        {
                            s = listView1.SelectedItems[select].Text;
                            sCheck = item1.ToString();
                            MessageBox.Show(item1.ToString());

                        }
                        else
                        {
                            select++;
                        }
                    }
                    string s1 = "";


                    foreach (object item in listView1.Items)
                    {
                        if (sCheck == item.ToString())
                        {

                            MessageBox.Show(item.ToString());
                            i++;
                            position++;

                        }
                        else
                        {
                            position++;
                        }
                    }

                    string s2 = listView1.Items.Count.ToString();
                    s1 = position.ToString();

                    if (i == 1)
                    {
                        string result = "Item: " + s + " || Position: " + s1 + " || Total Items: " + s2;
                        MessageBox.Show(result, "ListView Information", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    }
                }
                catch (Exception ex)
                {
                    MessageBox.Show("Select a Item");
                    MessageBox.Show(ex.Message);

                }

1 个答案:

答案 0 :(得分:0)

如果没有选定的项目,代码将在if (item1 == listView1.SelectedItems[select])处抛出异常,因为集合中的[0]没有元素。

另外,我相信以下代码与您的结果相同:

try
{
    foreach (var item in listView1.SelectedItems) 
    {
        MessageBox.Show(item.ToString());
    }

    if (listView1.SelectedItems.Count == 1)
    {
        var result = string.Format("Item: {0} || Position: {1} || Total Items: {2}", 
            listView1.SelectedItems[0], listView1.SelectedItems.Count, listView1.Items.Count);

        MessageBox.Show(result, "ListView Information", MessageBoxButtons.OK, MessageBoxIcon.Information);
    }
}
catch (Exception ex)
{
    MessageBox.Show("Select a Item");
    MessageBox.Show(ex.Message);
}

前三个if语句是冗余的,因为它们在分配变量后直接出现(即,如果您刚刚将其初始化为0,i如何等于1?)

两个foreach循环显示相同的内容(显示包含所选项目的消息框)。

每个选定项目的位置都会增加,因此它总是等于所选项目总数。

如果您提供有关您正在尝试实现的具体内容的更多详细信息,我会尽力提供帮助。