【问题标题】:C# listview count >0 but has no items how is this possible?C# listview count >0 但没有项目这怎么可能?
【发布时间】:2013-04-10 14:34:45
【问题描述】:

又一次被这个列表视图对象弄糊涂了。当我尝试从 Windows 表单中的列表视图中检索选定的列表视图项时,我的代码会出现“下标越界”错误。使用 VS add watch 命令,我看到以下内容当我查看 listview 对象的手表时,我看到以下内容

this.SearchResults  {System.Windows.Forms.ListView, Items.Count: 52, Items[0]: ListViewItem: {0}}   System.Windows.Forms.ListView

所以我看到 52 的计数是正确的,当程序运行时,我可以从集合中选择一行。例如,假设我从集合中挑选了第 5 个项目。 Watch 将返回以下内容

this.SearchResults.SelectedIndices[0]   5   int

因此,对于索引,我只想将 listviewitem 传递给另一个对象以进行进一步处理。当我尝试这个时,我得到一个运行时错误

An unhandled exception of type 'System.ArgumentOutOfRangeException' occurred in System.Windows.Forms.dll

Additional information: InvalidArgument=Value of '5' is not valid for 'index'.

这怎么可能?我有 52 个项目,代码表现得好像列表视图是空的。我曾尝试对索引进行硬编码,但也没有用。

我的这个表单的构造函数的代码列表视图在下面

public ResultsDisplay(List<MATS_Doc> foundDocs)
    {
        InitializeComponent();

        this.CenterToScreen();
        this.SearchResults.Columns.Add("Title");
        this.SearchResults.Columns.Add("Stuff");
        foreach (MATS_Doc doc in foundDocs)
        {
            // retrieve coresponding document
            // create new ListViewItem
            ListViewItem searchResults = new ListViewItem(doc.Id.ToString());
            searchResults.SubItems.Add(doc.Title);
            searchResults.SubItems.Add(doc.Stuff);
            // add the listviewitem to a new row of the ListView control
            this.SearchResults.Items.Add(searchResults); //show Text1 in column1, Text2 in col2
        }
        foreach (ColumnHeader column in this.SearchResults.Columns)
        {
            column.Width = -2;
        }
        this.Show();
    }

更新

下面是抛出异常的代码。列表视图的形式相同

 if (scoredListing == null)
        {
            DocumentView showdoc = new DocumentView(this.SearchResults.SelectedItems[this.SearchResults.SelectedIndices[0]]);
            showdoc.ShowDialog();
        }

【问题讨论】:

  • 你能给我们更多的代码来看看这个 List 是如何初始化的,或者 Property 的样子吗?
  • 我不明白你说你选择第 5 项的那一行...看起来你试图选择第 5 个 selected 项,而不是项。跨度>
  • 我很想知道您“粘贴”列表视图项的代码
  • 请发布引发异常的代码。单独的异常并不能帮助我们检测到任何东西。或者,GlaDOS 会怎么说:它不能帮助我们,帮助你,帮助我们,帮助我们所有人 ;)
  • 我用违规行更新了帖子。我正在启动另一个表单,并尝试使用 SelectedItems 字段传递 listviewitem。

标签: c# listview runtime-error listviewitem


【解决方案1】:
this.SearchResults.SelectedIndices[0]

每次没有选择时都会抛出,因为SelectedIndices 是空的,所以没有第一个元素。 this.SearchResults.SelectedIndices.Count 的值是多少?您的索引必须介于 0 和该值 - 1 之间。

编辑:好的,在MSDN docs 找到这个:

谨防从未显示的列表

如果您的 ListView 从未使用过,则无法信任 Selected 属性 被绘制(例如,它在一个 TabControl 中,在一个没有 尚未被选中)。在这种情况下,SelectedItems 和 父 ListView 的 SelectedIndices 未正确更新和 仍然是空的。

EDIT2:这很有趣但不相关。正如@nolonar 所指出的,SelectedIndices 包含引用所有项目的索引,而不是选定的项目,因此值 5 不是指第五个选择项目(因为可能只有一个),而是总体上的第五个项目。

【讨论】:

  • 我认为他正在选择第 5 项,然后引发异常。虽然我不确定。这个问题非常令人困惑,tbh...
  • 你说得对,我也不确定,如图所示语法不清楚。
  • 我还没有添加任何错误捕获,但我 100% 肯定我已经选择了一些东西。当我使用 VS Watch 时,我看到它对我选择的对应行进行了正确的评估。我的问题是我不能在 selecteditems 字段中使用该 int 值来提取 listviewitem。我希望这能解决问题。
  • SelectedItems.Count 的值是多少?
【解决方案2】:

问题出在:

this.SearchResults.SelectedItems[this.SearchResults.SelectedIndices[0]]

应该是这样的:

this.SearchResults.Items[this.SearchResults.SelectedIndices[0]]

如果您只选择了 1 个项目,SelectedItems 将只保留 1 个元素,如果您尝试访问除 0 以外的索引,则会引发异常

【讨论】:

    【解决方案3】:

    你可能需要使用

    this.SearchResults.Items[4]
    

    选择第 5 项。

    当您使用列表的SelectedItems 时,它只返回选定的项目。在选定的项目集合中,当您调用它时,第 5 项可能不存在。因为可能没有任何选择,或者只能选择一项。

    【讨论】:

    • 我不知道为什么,但这有效。我很困惑为什么 .SelectedItems 会抛出异常,但 .Items 在这种情况下无论如何都不会,感谢您的建议。吹了一个多小时。
    • 查看@nolonar 的回答,这是对问题的正确解释
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-06-21
    • 1970-01-01
    • 2013-10-15
    • 2022-11-03
    • 1970-01-01
    • 2018-10-31
    相关资源
    最近更新 更多