Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/286.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 读取ListView属性后看到的奇怪行为_C#_Winforms_Listview_Properties - Fatal编程技术网

C# 读取ListView属性后看到的奇怪行为

C# 读取ListView属性后看到的奇怪行为,c#,winforms,listview,properties,C#,Winforms,Listview,Properties,因此,我试图克隆我的列表视图,这样我就得到了一个新的列表视图,它具有旧视图的属性,但没有旧视图的项目(同时避免引用旧视图) 上面的方法工作得非常好,但是当我回到原始的ListView并执行以下操作时: string[] customerToAdd = { "To IBT", customerName, contactNum, date, SKU, itemDescription, emp, emp, emp

因此,我试图克隆我的
列表视图
,这样我就得到了一个新的
列表视图
,它具有旧视图的属性,但没有旧视图的项目(同时避免引用旧视图)

上面的方法工作得非常好,但是当我回到原始的
ListView
并执行以下操作时:

string[] customerToAdd = { "To IBT", customerName, contactNum, date, SKU, itemDescription,
                                             emp, emp, emp, emp, emp};
var listViewItem = new ListViewItem(customerToAdd);
listViewItem.Font = new Font(listViewItem.Font, FontStyle.Regular);
TableDisplay.Items.Insert(0, listViewItem);
我得到一个错误:

异常:引发:“InvalidArgument=值'-1'对'index'无效。”(System.ArgumentOutOfRangeException) 引发System.ArgumentOutOfRangeException:“InvalidArgument=值'-1'对'index'无效。”

因此,我创建了一个方法来比较原始
ListView
在复制其属性之前和之后的属性,唯一的区别是
CanSelect
created
Visible
已从True更改为False


你知道它为什么会这样吗?

为了解决这个问题,我只是改变了方法,使每个属性都是手动设置的

private ListView generateEmptyClone(ListView toClone)
{
    ListView newCopy = new ListView();

    newCopy.Alignment = ListViewAlignment.Top;
    newCopy.BorderStyle = BorderStyle.Fixed3D;
    newCopy.BackgroundImageTiled = false;
    newCopy.Font = new Font("Mircosoft Sans Serif", 9F, FontStyle.Bold, GraphicsUnit.Point, (byte)(0));
    newCopy.ForeColor = SystemColors.MenuText;
    // etc...


    foreach (ColumnHeader head in toClone.Columns)
    {
        newCopy.Columns.Add((ColumnHeader)head.Clone());
    }

    return newCopy;
}

事实证明,只有在我稍后在应用程序中处理了新副本时,错误才会出现。因此,我认为,通过像我在原始问题中那样迭代属性,必须存在一个引用回原始
ListView
的属性,因此当副本被释放时,原始
ListView
的某些元素也被释放,因此出现了问题。

你能提供用于创建
myListViewItem
的代码吗?@jaredbaszler我已经修改了帖子,我在这里没有看到任何不对劲的地方。也许可以检查一下,在克隆后,listview中的列数是否与您克隆的列数相同。此外,-1通常在找不到任何内容时由某种find函数返回。例如,如果您在列表框或数组中搜索某个项,但它没有找到它。如果您正在查找某个内容,然后尝试将其插入到索引-1中,则该操作无效。检查整个索引,确保它们都有效。这很容易调试。@jaredbaszler我已经检查并调试了一切。这肯定与属性的复制方式有关,特别是第一个Foreach循环。
private ListView generateEmptyClone(ListView toClone)
{
    ListView newCopy = new ListView();

    newCopy.Alignment = ListViewAlignment.Top;
    newCopy.BorderStyle = BorderStyle.Fixed3D;
    newCopy.BackgroundImageTiled = false;
    newCopy.Font = new Font("Mircosoft Sans Serif", 9F, FontStyle.Bold, GraphicsUnit.Point, (byte)(0));
    newCopy.ForeColor = SystemColors.MenuText;
    // etc...


    foreach (ColumnHeader head in toClone.Columns)
    {
        newCopy.Columns.Add((ColumnHeader)head.Clone());
    }

    return newCopy;
}