重新排序/移动/的DragDrop在C#Windows窗体在同一ListView控件中ListViewItems [英] Reorder / move / dragdrop ListViewItems within the same ListView Control in C# Windows Forms

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

问题描述

我在LargeIcon视图中的的ListView在C#2008系统的Windows窗体
现在,我想在另一个位置相同的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#似乎没有这个功能,或者需要为codeD第一。编码这种我没有经验,我发现在我的互联网研究没有回答。我发现它没有工作只是一个覆盖的过程。

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控件。我想微软给出,因为它是ListView控件内处理。在这个任何想法。 code将是非常美联社preciated我相信
我不能做我自己,除非它是一个非常简单的一行。

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:如果该项目需要移动我所需要的项目的所有属性被移动(文字,标记,imagek​​ey,背景颜色,前景颜色,名称的ToolTipText等)。我不知道这是如何实现的。
在此,我发现一个提示:它的存在是为了删除一个项目(名为上卸下摆臂()),并插入名为 .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#有无关,与这样的功能;这是一个用户界面技术的特征不是一种语言功能。然而,为了解决这个问题,我们在这里有一点code。它支持位置属性每个 ListViewItem的用于这一目的(在 LargeIcon 视图)。另一个重要的特性是 AutoArrange ,这应设置为允许位置生效。这里是code:

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;         
}

注意:你可以看到,我让2评论code线 listView1.AutoArrange 在那里,如果你想重新排序而不是修改 ListViewItem的位置,你可以取消这些行。我可以在这里看到一些闪烁(这是正常的,当你处理一个WinForms ListView的),所以你应该使用code(可放置在形式的构造函数),使 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);

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

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