???? ID: 307968 - ????? ???????: 04 ?????? 2010 - ??????: 2.0

?? ????? C# ????????? ??? ?????-????-????? ??????????? ?? TreeView ????? ?? ??? ???? ????

?????? ??????This article applies to a different operating system than the one you are using. Article content that may not be relevant to you is disabled.
?? ???? ?? Microsoft Visual Basic .NET ??????? ?? ???, ?????307967  (http://support.microsoft.com/kb/307967/ ) .
?? Microsoft Visual C++ .NET ??????? ?? ??? ?? ???? ??, ?????815675  (http://support.microsoft.com/kb/815675/ ) .

?? ????? ??

??? ?? ??????? ???? | ??? ?? ??????? ????

??????

This step-by-step article describes how to perform a drag-and-drop operation with tree nodes between twoTreeViewcontrols in a Visual C# application.

??????????

????? ???? outlines ???????? ?????????, ??????????, ??????? ??????, ?? ???? ???? ???:
  • ????? C#
?? ???? ????? ?? ?? ?? ????? ???? ?? ?????? ???:
  • Windows FormsTreeViewcontrol
  • Windows Forms event handling

????? ?? ?????

TheTreeViewcontrol provides three drag-and-drop events that you must handle:
  • ItemDrag: This event is raised from the sourceTreeViewcontrol as soon as the user starts to drag the tree node. When this occurs, call theDoDragDropmethod to initiate the drag-and-drop procedure.
  • DragEnter: After you initiate the drag-and-drop operation, you must handle theDragEnterevent in the destinationTreeView???????? ??? This event occurs when the user drags theTreeNodeobject from the sourceTreeViewcontrol to a point in the bounds of the destinationTreeView???????? ??? TheDragEnterevent enables the destinationTreeViewcontrol 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 theDragDropevent of the destinationTreeView???????? ??? This event occurs when theTreeNodeobject that is dragged has been dropped on the destinationTreeView???????? ??? To handle this event, retrieve theTreeNodeobject, and add the object to the destinationTreeView???????? ??? The code sample uses the????:object to retrieve the data.
The code sample in this article guarantees that aTreeNodeobject has been dragged to the destinationTreeView???????? ??? TheGetData?? ??? ????????:object retrieves the node that is dragged from the source control. TheGetNodeAtmethod 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 originalTreeView???????? ???

Steps to create the sample

  1. Create a new Windows Application in Visual C#. ???????? ??? ??, Form1 ??? ???? ??????? ?? ???? ???
  2. Use the toolbox to add twoTreeViewForm1 ?? ??? ???????? ??? ???????? ??? ??,TreeView1, ??TreeView2???? ???? ????
  3. ?????? ???? ?? ???TreeView???????? ?? ????? ?? ???? ?? ????? ???? ?????, ?? ??? ????????? ????AllowDrop????? ?? ???TreeView1, ??TreeView2???? ?? ???True??? ????? ????
  4. ???-????? ????Form1Form1 ?? ??? ????? ?? ??? ???? ?????? ????? ????? ?? ????????? ???? ?? ??? ????? ??? ??????TreeView?? ??? ????????TreeNode???????? ?? ????? 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. ????? ???? ?? ??? ????? ???? ?????? ??????ItemDrag?? ?? ?? ?????TreeView1??,TreeView2, ?????? ???????? ?? ???? ?? ???? ??? ?? ??? ?? ???? ?? ????? ?? ??? ?? ?? ???? ?? ???? ???????? ?? ??????? ???? ???
    private void treeView_ItemDrag(object sender,
    			System.Windows.Forms.ItemDragEventArgs e)
    		{
    			DoDragDrop(e.Item, DragDropEffects.Move);
    		}
  6. ????? ???? ?? ??? ????? ???? ?????? ??????DragEnter?? ?????TreeView1??,TreeView2, ?????? ???????? ?? ???? ?? ???? ??:
    private void treeView_DragEnter(object sender,
    			System.Windows.Forms.DragEventArgs e)
    		{
    			e.Effect = DragDropEffects.Move;
    		}
  7. Form1 ???? ?? ??? ????? ???? ?????? ????? ??? ???????? ????? ???? ?? ???? ???? ??? ??? ????? ???:
    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. ?????, ?? ????????? ?? ?????? ?? ?? ????? ?? ??????TreeView????? ????????? ????? ??? ???? ?? ??? ????? ???????? ?? ????? ???? ??? ?? ?? ?????? ?? ???????? ??? ??? ?????? ??? ?? ??? ??? ????? ??? ???

???

?? ???? ??? ??? ?? ????? ?? ???? ???????? ?????????? ?? ??? ?????? ??? ?????, ????? ???? illustrates ?? ????? ???? ????TreeView??? ??????? ???????? ??? ????????? ????? ????? ???? ????, ?? ???????? ??? ???? ????? ?????? ?? ???, ??? ???? ???? ??? ?? ????? ?? ??? ?? ?????-????-????? ???????? ?? ???? ???TreeView???????? ???

??? ?? ???????? ?? ?????? ???? ?????????? ???? ???????? ?? ???? ?????? ??? ?? ????? ??? ?? ????? ????TreeView???????? ??? ?? ???????? ?? ????? ???? ?? ??? ???? ???? ?? ????DestinationNode??; ?? ??? ?? ??, ?? ?? ?? ??? ?? ????? ?? ??? ?? ?? ???????? ??? ???? ???TreeView????????, ?? ??? ????

??????

???? ??????? ?? ???, ????? Microsoft ?????? ??????? (MSDN) ??? ???? ?????:
DragEnter ?????
(VS.71) http://MSDN.Microsoft.com/en-us/library/SYSTEM.Windows.forms.control.dragenter .aspx (http://msdn.microsoft.com/en-us/library/system.windows.forms.control.dragenter(VS.71).aspx)

DragDrop ?????
(vs.71) http://msdn2.Microsoft.com/en-us/library/SYSTEM.Windows.forms.control.dragdrop .aspx (http://msdn2.microsoft.com/en-us/library/system.windows.forms.control.dragdrop(vs.71).aspx)

???? ???? ???? ??:
  • Microsoft Visual C# 2008 Express Edition
  • Microsoft Visual C# 2005
  • Microsoft Visual C# .NET 2002 Standard Edition
??????: 
kbsweptvs2008 kbhowtomaster kbsample kbmt KB307968 KbMthi
???? ?????? ???????????? ?????? ????????
??????????: ?? ???? ?? ???? ??????? ?? ????? ?? Microsoft ????-?????? ?????????? ?????? ?????? ???? ??? ??. Microsoft ???? ??? ????-???????? ?? ????-???????? ????? ?????? ?? ???? ???????? ???? ?? ???? ????? ????? ??? ?? ??? ?????? ?? ???? ???? ???? ??? ????? ??. ???????, ????-???????? ???? ????? ???? ???? ???? ???. ?????, ????????, ?????-???? ?? ??????? ?? ???????? ?? ???? ???, ???? ?? ??? ?????? ???? ???? ??? ????? ??? ?? ???? ??. Microsoft ??????? ??? ???? ?? ?????? ?? ??????????, ????????? ?? ??? ?????? ?? ???? ????? ?? ???? ???????? ?? ??? ???? ????? ?? ??? ????????? ???? ??. Microsoft ????-?????? ?????????? ?? ????? ?????? ?? ?? ??? ??.
?????????? ?? ??????? ????????? ??????? ??:307968  (http://support.microsoft.com/kb/307968/en-us/ )