ListView选定值C#

ListView选定值C#,c#,listview,C#,Listview,我的代码解释了一切 if (listView1.SelectedItems[0].Text == "") { MessageBox.Show(listView1.SelectedItems[0].Text); MessageBox.Show("Please Select Value First", "Information", MessageBoxButtons.OK, MessageBoxIcon.Error);

我的代码解释了一切

        if (listView1.SelectedItems[0].Text == "")
        {
            MessageBox.Show(listView1.SelectedItems[0].Text);
            MessageBox.Show("Please Select Value First", "Information", MessageBoxButtons.OK, MessageBoxIcon.Error);
        }
        else
        {
        }
但是我在图片中解释了这个错误

如果没有任何选定的项目,则您不能请求第一个项目(
listView1.SelectedItems[0]
)。换句话说,
SelectedItems
为空

看起来你正试图做这样的事情。使用
SelectedItems.Count
检查集合中是否有任何内容:

// if there aren't any selected items
if (listView1.SelectedItems.Count <= 0)
{
   // then give an error
   MessageBox.Show("Please Select Value First", "Information", MessageBoxButtons.OK, MessageBoxIcon.Error);
   return;
}
// otherwise proceed
//如果没有任何选定项

如果(listView1.SelectedItems.Count,则在尝试检索之前,必须在ListView上设置至少1个选定项。
没有什么魔法能帮你做到这一点。

你的代码并不能解释每一件事,你是在使用Multi-select?还是只选择了一项?用户只会选择一项