【问题标题】:ListView Move Item with Up and Down Button .NET FrameworkListView 使用向上和向下按钮移动项目 .NET Framework
【发布时间】:2019-12-18 18:50:34
【问题描述】:

我有问题!

我有一个列表视图,其中包含随机数量的项目(根据文档的页面)。但我需要选择重新排序这些页面(它们是列表视图项)。我做了一个在两个按钮(向上和向下)上使用的功能,但它没有重新排序。例如:我有两个项目(第 1 页和第 2 页),我想将第 2 页移动到位置 0,它移动但在列表视图中它仍然显示以前的顺序。在我用更多页面制作的其他示例中。 下面是代码和他的电话。

private void moveImgList(int direction)
    {
        if (list_image.SelectedItems == null) { return; }

        ListViewItem item = list_image.SelectedItems[0];

        int newIndex = item.Index + direction;

        if (newIndex < 0 || newIndex >= list_image.Items.Count) { return; }

        list_image.Items.Remove(item);
        list_image.Items.Insert(newIndex, item);

    }

方法调用

private void btnLeft_Click(object sender, EventArgs e)
    {
        this.moveImgList(-1);
    }

    private void btnRight_Click(object sender, EventArgs e)
    {
        this.moveImgList(1);
    }

应用程序 Windows 窗体 .NET 框架

My code

原来是这样!

private void moveItem(int direction)
    {
        if (listView1.SelectedItems.Count == 0) { return; }

        ListViewItem item = listView1.SelectedItems[0];

        int newIndex = item.Index + direction;

        if (newIndex < 0 || newIndex >= listView1.Items.Count) { return; }

        var currentView = listView1.View;
        listView1.BeginUpdate();
        listView1.View = View.Details;
        listView1.Items.Remove(item);
        listView1.Items.Insert(newIndex, item);
        listView1.EnsureVisible(newIndex);
        listView1.View = currentView;
        listView1.EndUpdate();
    }

【问题讨论】:

  • 您的代码按预期工作。在BeginUpdate()EndUpdate() 序列中执行索引更改只是消除了闪烁。删除Refresh(),不是必须的。
  • 您可以在 moveImgList() 调用之后的 Buttons Click 事件中添加 list_image.Focus();(为了更好地查看索引更改:所选项目将保持可见选中状态)。
  • 我输入了list_image.Focus ();,但它也不起作用。我还是有同样的问题
  • 这是your code at work,我刚刚添加了list_image.EnsureVisible(newIndex);。它有什么问题?
  • link 这是我在这里工作的代码

标签: c# winforms listview .net-4.0


【解决方案1】:

尝试在列表视图对象上调用BeginUpdate()EndUpdate() 方法。这会强制 UI 重新绘制控件。

 list_image.BeginUpdate();
 list_image.Items.Remove(item);
 list_image.Items.Insert(newIndex, item);
 list_image.EndUpdate();

【讨论】:

  • 还增加了BeginUpdate()和EndUpdate()。不幸的是,没有成功。
【解决方案2】:

当您删除一个项目时,索引会发生变化,您可能会插入错误的位置,如果它是最后一个项目,甚至会出现异常。我认为交换 2 个项目更好:

private void moveImgList(int direction)
{
    if (list_image.SelectedItems == null) return; 
    int index = list_image.SelectedIndex;
    if((direction == -1 && index == 0) || (index == list_image.Items.Count - 1 && direction =1)
        return;
    int newIndex = index + direction;
    tmp = list_image.Items[index].Clone();
    list_image.Items[index] = (ListViewItem)list_image.Items[newIndex].Clone();
    list_image.Items[newIndex] = (ListViewItem)tmp;
}

然后这会起作用:

private void btnLeft_Click(object sender, EventArgs e)
{
    moveImgList(-1);
}

private void btnRight_Click(object sender, EventArgs e)
{
    moveImgList(1);
}

【讨论】:

  • 错误:System.ArgumentException:'不能在多个位置添加或插入项目'第1页'。首先,您需要将其从当前位置移除或克隆。
  • 你确定吗?因为这段代码既不添加也不插入
  • 你正在使用moveImgList(-1);moveImgList(1);
  • 我不得不对编译代码进行一些修改,但是在执行的时候给出了我提到的错误
  • if (list_image.SelectedItems == null) return; ListViewItem item = list_image.SelectedItems[0]; int index = item.Index; if ((direction == -1 &amp;&amp; index == 0) || (index == list_image.Items.Count - 1 &amp;&amp; direction == 1)) return; int newIndex = index + direction; ListViewItem tmp = list_image.Items[index]; list_image.Items[index] = list_image.Items[newIndex]; list_image.Items[newIndex] = tmp;
猜你喜欢
  • 2020-04-06
  • 1970-01-01
  • 1970-01-01
  • 2021-07-18
  • 2021-10-08
  • 2011-02-26
  • 1970-01-01
  • 1970-01-01
  • 2023-03-18
相关资源
最近更新 更多