ListView 控件和TreeView控件实现拖拽(转)

 

ListView 控件在运行时不项目支持拖和功能

本页

症状
如果将项目添加到 Microsoft Windows 窗体上的 ListView 控件,您不能对项重新排序通过在运行时拖动 ListView 控件中的项目。...

如果将项目添加到 Microsoft Windows 窗体上的 ListView 控件,您不能对项重新排序通过在运行时拖动 ListView 控件中的项目。

原因
出现此问题的原因 ListView 控件在运行时不项目的支持拖和功能。

出现此问题的原因 ListView 控件在运行时不项目的支持拖和功能。

替代方法
要变通解决此问题,提供 ListView 控件拖和功能如下: 将以下代码添加到 ListView1_ItemDrag 事件处理程序。Microsoft Visu...

要变通解决此问题,提供 ListView 控件拖和功能如下:
  1. 将以下代码添加到 ListView1_ItemDrag 事件处理程序。

    Microsoft Visual Basic.NET 或 Microsoft Visual Basic 2005 代码
     'Begins a drag-and-drop operation in the ListView control.
                                ListView1.DoDragDrop(ListView1.SelectedItems, DragDropEffects.Move)
    ListView1.DoDragDrop (ListView1.SelectedItems) DragDropEffects.move Microsoft Visual C#.NET 或 Microsoft Visual C# 2005 代码 ListView 控件中的
    //Begins a drag-and-drop operation in the ListView control.
                                listView1.DoDragDrop(listView1.SelectedItems, DragDropEffects.Move);
    listView1.DoDragDrop (listView1.SelectedItems,DragDropEffects.Move);
  2. 将以下代码添加到 ListView1_DragEnter 事件处理程序。

    Visual Basic.NET 或 Visual Basic 2005 代码
    Dim i As Integer
                                For i = 0 To e.Data.GetFormats().Length - 1
                                If e.Data.GetFormats()(i).Equals("System.Windows.Forms.ListView+SelectedListViewItemCollection") Then
                                'The data from the drag source is moved to the target.
                                e.Effect = DragDropEffects.Move
                                End If
                                Next
    e.effect = DragDropEffects.move 结束,如果下一个 Visual C#.NET 或 Visual C# 2005 代码
    int len=e.Data.GetFormats().Length-1 ;
                                int i;
                                for (i = 0 ; i<=len ;i++)
                                {
                                if (e.Data.GetFormats()[i].Equals("System.Windows.Forms.ListView+SelectedListViewItemCollection"))
                                {
                                //The data from the drag source is moved to the target.
                                e.Effect = DragDropEffects.Move;
                                }
                                }
                                
  3. 将以下代码添加到 ListView1_DragDrop 事件处理程序。

    Visual Basic.NET 或 Visual Basic 2005 代码
    'Return if the items are not selected in the ListView control.
                                If ListView1.SelectedItems.Count = 0 Then Return
                                'Returns the location of the mouse pointer in the ListView control.
                                Dim p As Point = ListView1.PointToClient(New Point(e.X, e.Y))
                                'Obtain the item that is located at the specified location of the mouse pointer.
                                Dim dragToItem As ListViewItem = ListView1.GetItemAt(p.X, p.Y)
                                If dragToItem Is Nothing Then Return
                                'Obtain the index of the item at the mouse pointer.
                                Dim dragIndex As Integer = dragToItem.Index
                                Dim i As Integer
                                Dim sel(ListView1.SelectedItems.Count) As ListViewItem
                                For i = 0 To ListView1.SelectedItems.Count - 1
                                sel(i) = ListView1.SelectedItems.Item(i)
                                Next
                                For i = 0 To ListView1.SelectedItems.Count - 1
                                'Obtain the ListViewItem to be dragged to the target location.
                                Dim dragItem As ListViewItem = sel(i)
                                Dim itemIndex As Integer = dragIndex
                                If itemIndex = dragItem.Index Then Return
                                If dragItem.Index < itemIndex Then
                                itemIndex = itemIndex + 1
                                Else
                                itemIndex = dragIndex + i
                                End If
                                'Insert the item in the specified location.
                                Dim insertitem As ListViewItem = dragItem.Clone
                                ListView1.Items.Insert(itemIndex, insertitem)
                                'Removes the item from the initial location while
                                'the item is moved to the new location.
                                ListView1.Items.Remove(dragItem)
                                Next
                                
    Visual C#.NET 或 Visual C# 2005 代码
    //Return if the items are not selected in the ListView control.
                                if(listView1.SelectedItems.Count==0)
                                {
                                return;
                                }
                                //Returns the location of the mouse pointer in the ListView control.
                                Point cp = listView1.PointToClient(new Point(e.X, e.Y));
                                //Obtain the item that is located at the specified location of the mouse pointer.
                                ListViewItem dragToItem = listView1.GetItemAt(cp.X, cp.Y);
                                if(dragToItem==null)
                                {
                                return;
                                }
                                //Obtain the index of the item at the mouse pointer.
                                int dragIndex = dragToItem.Index;
                                ListViewItem[] sel=new ListViewItem [listView1.SelectedItems.Count];
                                for(int i=0; i<=listView1.SelectedItems.Count-1;i++)
                                {
                                sel[i]=listView1.SelectedItems[i];
                                }
                                for(int i=0; i<sel.GetLength(0);i++)
                                {
                                //Obtain the ListViewItem to be dragged to the target location.
                                ListViewItem dragItem = sel[i];
                                int itemIndex = dragIndex;
                                if(itemIndex==dragItem.Index)
                                {
                                return;
                                }
                                if(dragItem.Index<itemIndex)
                                itemIndex++;
                                else
                                itemIndex=dragIndex+i;
                                //Insert the item at the mouse pointer.
                                ListViewItem insertItem = (ListViewItem)dragItem.Clone();
                                listView1.Items.Insert(itemIndex, insertItem);
                                //Removes the item from the initial location while
                                //the item is moved to the new location.
                                listView1.Items.Remove(dragItem);
                                }
                                
  4. 生成 菜单上单击 生成解决方案
  5. 调试 菜单中上, 单击 开始

状态
这种现象是设计使然。

这种现象是设计使然。

更多信息
重现该问题的步骤 启动 Microsoft Visual Studio.NET 或 Microsoft Visual Studio 2005。 通过 Visua...

重现该问题的步骤

  1. 启动 Microsoft Visual Studio.NET 或 Microsoft Visual Studio 2005。
  2. 通过 Visual Basic.NET、 Visual Basic 2005、 Visual 的 C# 2005 或 Visual C#.NET 创建一个新的 Windows 应用程序。

    默认,会创建 Form 1。
  3. ListView 控件添加到 Form 1。
  4. 右键单击 ListView1 ,然后单击 属性
  5. 属性 对话框单击 项目 属性,然后单击省略号按钮 ( | )。
  6. ListViewItem 集合编辑器 对话框,单击 添加 四次。
  7. 将第一个 ListViewItem 控件的 Text 属性设置为 Item 1 ,将第二个 ListViewItem 控件的 Text 属性设置为 Item2 、 将第三个 ListViewItem 控件的 Text 属性设置为 Item3 ,并将在第四个的 ListViewItem 控件在 Text 属性设置为 Item4 中。
  8. ListViewItem 集合编辑器 对话框,单击 确定
  9. 属性 对话框中的 ListView1 ,请将 视图 属性设置为 列表 中。
  10. ListView1 AllowDrop 属性设置为 True 中。
  11. 生成 菜单上单击 生成解决方案
  12. 调试 菜单中上, 单击 开始
  13. 尝试将 Item 1 拖动到新位置 ListView 控件中。

    您不能拖动 Item 1

参考
有关详细信息,请访问下面的 Microsoft Web 站点: Control.DoDragDrop 方法http://msdn2.microsoft.com/...

有关详细信息,请访问下面的 Microsoft Web 站点:
Control.DoDragDrop 方法
http://msdn2.microsoft.com/en-us/library/system.windows.forms.control.dodragdrop(vs.71).aspx (http://msdn2.microsoft.com/en-us/library/system.windows.forms.control.dodragdrop(vs.71).aspx)
Control.DragDrop 事件
http://msdn2.microsoft.com/en-us/library/system.windows.forms.control.dragdrop(vs.71).aspx (http://msdn2.microsoft.com/en-us/library/system.windows.forms.control.dragdrop(vs.71).aspx)
有关其他信息,请单击下面的文章编号,以查看 Microsoft 知识库中相应:
822482  (http://support.microsoft.com/kb/822482/ ) HOW TO: 使用 Microsoft Visual Basic.NET 提供了在 Microsoft Windows 应用程序的拖和功能

 
 
 
 
TreeView控件的拖拽实现
This step-by-step article describes how to perform a drag-and-drop operation with tree nodes between two TreeView controls in a Visual C# application.

Requirements

The following list outlines the recommended hardware, software, network infrastructure, and service packs that you need:
  • Visual C#
This article assumes that you are familiar with the following topics:
  • Windows Forms TreeView control
  • Windows Forms event handling

Description of the technique

The TreeView control provides three drag-and-drop events that you must handle:
  • ItemDrag: This event is raised from the source TreeView control as soon as the user starts to drag the tree node. When this occurs, call the DoDragDrop method to initiate the drag-and-drop procedure.
  • DragEnter: After you initiate the drag-and-drop operation, you must handle the DragEnter event in the destination TreeView control. This event occurs when the user drags the TreeNode object from the source TreeView control to a point in the bounds of the destination TreeView control. The DragEnter event enables the destination TreeView control to specify whether the drag operation is valid for this control. The code sample in this article enables only the move operation.
  • DragDrop: The last event to handle is the DragDrop event of the destination TreeView control. This event occurs when the TreeNode object that is dragged has been dropped on the destination TreeView control. To handle this event, retrieve the TreeNode object, and add the object to the destination TreeView control. The code sample uses the Data object to retrieve the data.
The code sample in this article guarantees that a TreeNode object has been dragged to the destination TreeView control. The GetData method of the Data object retrieves the node that is dragged from the source control. The GetNodeAt method determines where this node is dropped on the destination control. After you determine the position, add the source node as a child of the destination node. Because this sample performs a move operation, the last step is to remove the source node from the original TreeView control.

Steps to create the sample

  1. Create a new Windows Application in Visual C#. By default, a form that is named Form1 is created.
  2. Use the toolbox to add two TreeView controls to Form1. By default, TreeView1 and TreeView2 are created.
  3. To allow the TreeView controls to be dragged and dropped, change the AllowDrop property of both TreeView1 and TreeView2 to True in the Properties window.
  4. Double-click Form1 to generate the method handler for Form1's Load event. Add the following code to populate the two TreeView controls with TreeNode objects and to define the event handlers:
    private void Form1_Load(object sender, System.EventArgs e)
                                {
                                TreeNode ParentNode1;
                                TreeNode ParentNode2;
                                ParentNode1 = treeView1.Nodes.Add("tv1");
                                ParentNode1.Nodes.Add("tv1FirstChild");
                                ParentNode1.Nodes.Add("tv1SecondChild");
                                ParentNode1.Nodes.Add("tv1ThirdChild");
                                ParentNode1.Nodes.Add("tv1FourthChild");
                                ParentNode1.Expand();
                                ParentNode2 = treeView2.Nodes.Add("tv2");
                                ParentNode2.Nodes.Add("tv2FirstChild");
                                ParentNode2.Nodes.Add("tv2SecondChild");
                                ParentNode2.Expand();
                                this.treeView1.ItemDrag += new System.Windows.Forms.ItemDragEventHandler(this.treeView_ItemDrag);
                                this.treeView2.ItemDrag += new System.Windows.Forms.ItemDragEventHandler(this.treeView_ItemDrag);
                                this.treeView1.DragEnter += new System.Windows.Forms.DragEventHandler(this.treeView_DragEnter);
                                this.treeView2.DragEnter += new System.Windows.Forms.DragEventHandler(this.treeView_DragEnter);
                                this.treeView1.DragDrop += new System.Windows.Forms.DragEventHandler(this.treeView_DragDrop);
                                this.treeView2.DragDrop += new System.Windows.Forms.DragEventHandler(this.treeView_DragDrop);
                                }
  5. Add the following method handler to handle the ItemDrag event of either TreeView1 or TreeView2, depending on the direction of the drag operation. This code initiates a move operation on the item that is being dragged.
    private void treeView_ItemDrag(object sender,
                                System.Windows.Forms.ItemDragEventArgs e)
                                {
                                DoDragDrop(e.Item, DragDropEffects.Move);
                                }
  6. Add the following method handler to handle the DragEnter event of TreeView1 or TreeView2, depending on the direction of the drag operation:
    private void treeView_DragEnter(object sender,
                                System.Windows.Forms.DragEventArgs e)
                                {
                                e.Effect = DragDropEffects.Move;
                                }
  7. Add the following method to the Form1 class so that it immediately follows the method that you added in the previous step:
    private void treeView_DragDrop(object sender, System.Windows.Forms.DragEventArgs e)
                                {
                                TreeNode NewNode;
                                if(e.Data.GetDataPresent("System.Windows.Forms.TreeNode", false))
                                {
                                Point pt = ((TreeView)sender).PointToClient(new Point(e.X, e.Y));
                                TreeNode DestinationNode = ((TreeView)sender).GetNodeAt(pt);
                                NewNode = (TreeNode)e.Data.GetData("System.Windows.Forms.TreeNode");
                                if(DestinationNode.TreeView != NewNode.TreeView)
                                {
                                DestinationNode.Nodes.Add((TreeNode) NewNode.Clone());
                                DestinationNode.Expand();
                                //Remove Original Node
                                NewNode.Remove();
                                }
                                }
                                }
  8. Build and run the project. Drag nodes from one TreeView control to the other. Note that the node is removed from the source control and is added as a child node in the destination control.

Notes

The sample that is provided in this article is intended for demonstration purposes only. Therefore, the sample only illustrates how to use the TreeView control in a simplified scenario. To keep the sample small, several scenarios are not considered. For example, the code does not let you perform a drag-and-drop operation with nodes in the same TreeView control.

The code also does not handle the scenario in which a user does not drop the object on a particular node in the destination TreeView control. To handle this scenario, test whether the DestinationNode is null; if it is, you can add the object that is being dragged to the root of the TreeView control, and so on.
posted @ 2009-05-15 14:09  坚持·坚持  阅读(3347)  评论(0编辑  收藏  举报