Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/317.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 - Fatal编程技术网

C# ListView移动项目

C# ListView移动项目,c#,winforms,listview,C#,Winforms,Listview,ListView还有一个问题:(现在我需要在组中移动项目(向上、向下、到开头、到结尾),但是ListView总是在末尾显示移动的项目 以下是将项目移到开头的示例代码: if (1 == listView1.SelectedItems.Count) { ListViewItem item = listView1.SelectedItems[0]; ListViewGroup gp = item.Group; int index;

ListView还有一个问题:(现在我需要在组中移动项目(向上、向下、到开头、到结尾),但是ListView总是在末尾显示移动的项目

以下是将项目移到开头的示例代码:

   if (1 == listView1.SelectedItems.Count)
    {

        ListViewItem item = listView1.SelectedItems[0];
        ListViewGroup gp = item.Group;

        int index;
        index = item.Index;

        if (index < listView1.Items.Count)
        {

            index = 0;

            listView1.Items.Remove(item);

            item.Group = gp;

            listView1.Items.Insert(index, item);
        }
    }
if(1==listView1.SelectedItems.Count)
{
ListViewItem项=listView1。选择编辑项[0];
ListViewGroup gp=项目组;
整数指数;
索引=项目索引;
if(索引
我试图通过谷歌找到一些解决方案,但我发现其他人()也有和我一样的问题,但他的解决方案不起作用:(


我考虑过使用ObjectListView,但我已经修改了ListView,它现在支持WinAmp效果的拖放、onScroll事件、滚动同步等。我不想丢失这些东西:(

我对这个问题的自动回答是,如果你说

listView1.Items.Count-1


因为列表的索引为零,所以计数比上一个索引大1)

看看代码,它有一些问题让我好奇。看起来可能的问题是初始的if语句。如果itemlist中只有一个项,那么删除它并重新添加它将不会有任何作用,无论您将其粘贴到何处。我相信您想要尝试以下方法:

/// <summary>
/// Move the given item to the given index in the given group
/// </summary>
/// <remarks>The item and group must belong to the same ListView</remarks>
public void MoveToGroup(ListViewItem lvi, ListViewGroup group, int indexInGroup) {
    group.ListView.BeginUpdate();
    ListViewItem[] items = new ListViewItem[group.Items.Count + 1];
    group.Items.CopyTo(items, 0);
    Array.Copy(items, indexInGroup, items, indexInGroup + 1, group.Items.Count - indexInGroup);
    items[indexInGroup] = lvi;
    for (int i = 0; i < items.Length; i++)
        items[i].Group = null;
    for (int i = 0; i < items.Length; i++) 
        group.Items.Add(items[i]);
    group.ListView.EndUpdate();
}
//
///将给定项移动到给定组中的给定索引
/// 
///项目和组必须属于同一ListView
public void MoveToGroup(ListViewItem lvi、ListViewGroup组、int IndexGroup){
group.ListView.BeginUpdate();
ListViewItem[]items=新ListViewItem[group.items.Count+1];
group.Items.CopyTo(Items,0);
复制(items,indexgroup,items,indexgroup+1,group.items.Count-indexgroup);
项目[索引组]=lvi;
对于(int i=0;i
First if表示如果ListView中只有1个选定项,而不是1个项:-),并且索引为0,则您是对的…我更正了它,但它没有解决问题。哦!选定项。我误读了。我猜问题出在listView1.items.Insert(索引,项);我会在那里弹出一条调试语句,检查所有变量,确保它们都正常,然后看看是否可以进入其中,看看它是否会在其他地方指向用户定义的代码。我认为这可能不是问题。例如:具有4个项的Listview的第一个索引为0,最后一个索引为3。if(index