C# 从列表视图c获取所选项目的行号

C# 从列表视图c获取所选项目的行号,c#,C#,如何在c中从列表视图中获取所选项目的行号 private void listView1_DoubleClick(object sender, EventArgs e) { if( (listView1 row numer for selectitem) > 2 ) { int indx = listView1.SelectedItems[0].Index; listView1.Items[indx].Remove(); } } 可能会有帮助 也就

如何在c中从列表视图中获取所选项目的行号

private void listView1_DoubleClick(object sender, EventArgs e)
{
   if( (listView1 row numer for selectitem) > 2 )
   {
      int indx = listView1.SelectedItems[0].Index;
      listView1.Items[indx].Remove();
   }
}
可能会有帮助

也就是说,改为侦听ItemSelectionChanged事件,并对事件数据使用ItemIndex属性

例如,Microsoft示例显示

private void ListView1_ItemSelectionChanged(
    Object sender, ListViewItemSelectionChangedEventArgs e) {

    var i = e.ItemIndex; // got the latest selection
}

请改为尝试SelectedIndexChanged事件。。。索引位置是从零开始的,所以如果你有10个项目,你的索引将是0-9。如果您认为0是第1行,那么只需添加一个。最后,如果未选择项目,则索引为-1

private void ListView1_SelectedIndexChanged(object sender, EventArgs e)
{
    if (ListView1.SelectedIndex > -1)
    {
        // Add 1 so you have 1 - 10 instead of 0 - 9
        int rowNumber = ListView1.SelectedIndex + 1;

        // Your example says you want to delete the selected index
        // so you still would want to use the selected index
        ListView1.Items.RemoveAt(ListView1.SelectedIndex);

        // After you remove the item, this method will fire again
        // but the selected index will be -1 so none of this code will
        // execute again.
    }
}
请记住,此代码一次只支持选择一行


希望这有帮助……

您的意思是问如何获取索引?如果我选择了项目,我需要获取此选择的行号谢谢,但如果您在列表视图中选择了任何项目,我需要此项目的门行号选择尝试更新答案以反映我认为您的问题。很抱歉,语言障碍很高——我不完全理解你的英语,我在猜测你的意图。