在 C# Windows 窗体中的同一 ListView 控件中重新排序/移动/拖放 ListViewItems

时间:2023-04-26
本文介绍了在 C# Windows 窗体中的同一 ListView 控件中重新排序/移动/拖放 ListViewItems的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

问题描述

我在 C# 2008 系统 Windows 窗体中的 LargeIcon 视图中有一个 ListView.现在我想将这个 ListView 的一个项目移动到另一个位置的同一个 ListView 中——我们称之为拖放"或项目重新排序"或其他什么.VB 6 能够做到这一点,并在任何 listView 中自动完成.

I have a ListView in LargeIcon View in C# 2008 System Windows Forms. Now I would like to move an item of this ListView within the same ListView on another position - let's call it "drag-and-drop" or an "item reorder" or whatever. VB 6 is capable of this and does this automatically in any listView.

C# 似乎没有此功能,或者需要先对其进行编码.对于编码,我没有经验,我在互联网上的研究中也没有找到答案.我发现只有一个覆盖程序"不起作用.

C# seems not to have this feature or it needed to be coded first. For coding this I have no experience and I have found no answer in my research in the internet. I found only an "override procedure" which didn't worked.

我不需要任何其他 ListView 控件(如 ObjectListView 或其他),也不需要重写过程或制作新的 ListView 控件.我想在 Microsoft 提供的 ListView 控件中处理它.对此有任何想法.我相信代码将不胜感激除非它是一个非常简单的单行,否则我不能自己做.

I do not need any other ListView Controls (like ObjectListView or whatever) and I do not need override procedures or crafting a new ListView Control. I want to handle it within the ListView control given by Microsoft as it is. Any ideas on this. Code would be highly appreciated I believe I can't do it on my own unless it's a very simple one-liner.

PS:如果需要移动项目,我需要移动项目的所有属性(文本、标签、图像键、背景色、前景色、名称、工具提示文本等).我不知道如何做到这一点.我发现了一个提示:删除一个项目(称为 .Remove())并插入称为 .Insert().但是有了这些信息,我仍然无法通过鼠标移动"项目.我认为listView的所有DragEvents都在这里发挥作用,但我不知道如何使用它们以及如何将所选项目(listView1.SelectedItems)复制到右侧职位和需要首先获得这个职位.

PS: If the item needs to be moved I need all properties of the item to be moved (text, tag, imagekey, background-color, foreground-color, name, tooltiptext etc.). I have no idea how this can be accomplished. One hint on this I found: It exists to remove an item (called .Remove()) and insert called .Insert(). But with this information I still can't make the "moving" of items done by mouse. I think that all the DragEvents of the listView play a role here, but I dunno how to use them and how to copy the selected items (listView1.SelectedItems) to the right position and need of gaining this position first.

推荐答案

其实你说的这个功能Winforms不支持,不是C#.C# 与这样的功能无关;它是 UI 技术功能而不是语言功能.但是,为了解决这个问题,我们这里的代码很少.它支持每个 ListViewItem 用于该目的的 Position 属性(在 LargeIcon 视图中).另一个重要的属性是AutoArrange,这应该设置为false 以使Position 生效.代码如下:

In fact the feature you talk about is not supported by Winforms not C#. C# has nothing to do with such a feature; it's a UI technology feature not a language feature. However, to solve this, we have little code here. It supports the Position property for each ListViewItem to use for that purpose (in LargeIcon view). Another important property is AutoArrange, this should be set to false to allow the Position to take effect. Here is the code:

ListViewItem heldDownItem;
Point heldDownPoint;
//MouseDown event handler for your listView1
private void listView1_MouseDown(object sender, MouseEventArgs e)
{            
    //listView1.AutoArrange = false;
    heldDownItem = listView1.GetItemAt(e.X,e.Y);
    if (heldDownItem != null) {
      heldDownPoint = new Point(e.X - heldDownItem.Position.X, 
                                e.Y - heldDownItem.Position.Y);
    }
}
//MouseMove event handler for your listView1
private void listView1_MouseMove(object sender, MouseEventArgs e)
{
    if (heldDownItem != null){
        heldDownItem.Position = new Point(e.Location.X - heldDownPoint.X, 
                                          e.Location.Y - heldDownPoint.Y);
    }
}
//MouseUp event handler for your listView1
private void listView1_MouseUp(object sender, MouseEventArgs e)
{
    heldDownItem = null;
    //listView1.AutoArrange = true;         
}

注意:如您所见,如果您想 reorder 而不是更改,我在 listView1.AutoArrange 处放了 2ListViewItem 位置您可以取消注释这些行.我可以注意到这里有些闪烁(这在处理 winforms ListView 时是正常的),所以你应该使用这段代码(可以放在表单构造函数中)来启用 DoubleBuffered:

NOTE: as you can see, I let 2 commented code lines listView1.AutoArrange there, if you want to reorder instead of changing the ListViewItem position you can uncomment those lines. I can notice some flicker here (this is normal when you deal with a winforms ListView), so you should use this code (can be placed in the form constructor) to enable DoubleBuffered:

typeof(Control).GetProperty("DoubleBuffered", 
                             System.Reflection.BindingFlags.NonPublic |
                             System.Reflection.BindingFlags.Instance)
               .SetValue(listView1, true, null);

这篇关于在 C# Windows 窗体中的同一 ListView 控件中重新排序/移动/拖放 ListViewItems的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

上一篇:C#在ListView中实现拖拽时自动滚动&掉落 下一篇:拖放在 C# Winforms 应用程序中不起作用

相关文章