C#listview不允许我删除所选的 [英] C# listview does not let me to remove an selected

查看:79
本文介绍了C#listview不允许我删除所选的的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

尝试从窗体应用程序的列表视图中删除项目。

每次我尝试抛出异常都是如此奇怪。还有我最初使用的代码:



异常说:索引超出范围。必须是非负数且小于集合的大小。参数名称:index



trying to remove item from a listview of a window form application.
it is so weird that it try to throw an exception each time i do it. And there is the code that i use originally :

exception say: Index was out of range.Must be non-negative and less than the size of the collection. Parameter name:index

private void Remove( )
        {
            
            try
            {
                
                      listView1.Items.RemoveAt(listView1.SelectedItems[0].Index);


            }
            catch (Exception ex) { MessageBox.Show(ex.Message); }
        }





我尝试过:



即使我试图输入直接索引0,它仍然会抛出相同的异常





What I have tried:

even though i tried to put in direct index 0 , it still would throw the same exception

ListView.SelectedListViewItemCollection album = this.listView1.SelectedItems;
                if (album.Count > 0)
                    listView1.Items.RemoveAt(0);

推荐答案

您是否检查了 SelectedItems 集合以确保它不是真的?
Did you inspect the SelectedItems collection to make sure it's not empy?


是的,我这样做了,异常被抛出**





yes, i did this , the exception get thrown at **


if (listView1.SelectedItems.Count > 0)
                {
                    listView1.Items.RemoveAt(listView1.SelectedItems[0].Index); **
                    
                    
                }


这是Windows Forms ....



在第一个例子中,你没有检查是否有选定的索引,所以当没有选择时它明显失败。



如果第二个示例失败且具有相同的确切异常,那么问题可能是由代码的位置引起的。如果过早执行代码,则可能尚未创建控件。您可以从 Control.IsHandleCreated调查情况属性(System.Windows.Forms) [ ^ ]



例如,如果从表单构造函数中调用以下代码,得到列表视图尚未创建的消息。此外,如果省略该消息并让代码运行,您可以看到列表视图中有三个项目,因此没有进行删除。



如果您打电话例如,从按钮点击事件,它应该工作正常

Taken this is Windows Forms....

In the first example you don't examine if there is a selected index or not so it clearly fails when there is no selection.

If the second example fails with the same exact exception, then perhaps the problem is caused by the location of your code. If you execute the code too early the control may not yet be created. You can investigate the situation from Control.IsHandleCreated Property (System.Windows.Forms)[^]

For example if you call the following code from the form constructor you get the message that the listview isn't created yet. Also if you omit the message and let the code run, you can see that there are three items in the listview so deletion hasn't taken place.

If you call it for example from a button click event, it should work fine
public void RemoveItems(bool omitCheck=false) {

   if (!omitCheck) {
      if (!this.listView1.IsHandleCreated) {
         MessageBox.Show("Listview not fully created yet");
         return;
      }
   }

   this.listView1.MultiSelect = true;

   // Add a few items
   this.listView1.Items.Add("First");
   this.listView1.Items.Add("Second");
   this.listView1.Items.Add("Third");

   // Delete selected items (none)
   foreach (ListViewItem item in this.listView1.SelectedItems) {
      this.listView1.Items.Remove(item);
   }

   // Select two items
   this.listView1.Items[0].Selected = true;
   this.listView1.Items[2].Selected = true;

   // Delete selected items (two)
   foreach (ListViewItem item in this.listView1.SelectedItems) {
      this.listView1.Items.Remove(item);
   }
}


这篇关于C#listview不允许我删除所选的的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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