C # Implement the drag of the node in the listview

xiaoxiao2021-03-06  81

Using system.drawing; using system.windows.forms;

Public class form4: form {private treeview TreeView1;

Public form4 () {TreeView1 = New TreeView ();

THIS.SUSPENDLAYOUT ();

// Initialize TreeView1. TreeView1.allowdrop = true; TreeView1.dock = DockStyle.Fill;

// add nodes to treeview1. Treenode node; for (int x = 0; x <3; x) {// add a root node to turn1.nodes.add (String.Format) (String.format ("node { 0} ", x * 4)); for (int y = 1; y <4; y) {// add a child node to the prepiously added node. Node = node.nodes.add (string.format) "Node {0}", x * 4 y));}}

// Add event handlers for the required drag events treeView1.ItemDrag = new ItemDragEventHandler (treeView1_ItemDrag);. TreeView1.DragEnter = new DragEventHandler (treeView1_DragEnter); treeView1.DragOver = new DragEventHandler (treeView1_DragOver); treeView1.DragDrop = new DrageventHandler (TreeView1_DragDrop);

// Initialize the form. This.clientsize = new size (292, 273); this.Controls.add (TreeView1);

THIS.ResumeLayout (false);

[Stathread] // static void main () // {// application.run (new form1 ()); //} // private void TreeView1_itemdrag (Object sender, itemdrageventargs e) {//move the dragged node when the left Mouse Button is buy. if (e.button == mousebuttons.left) {DODRAGDROP (E.Item, DragDropeffects.move);

// Copy The Dragged Node when The Right Mouse Button IS Used. Else IF (E.Button == MouseButtons.right) {DODRAGDROP (E.Item, DragDropeffects.copy);}}

// Set the target drop effect to the effect // specified in the ItemDrag event handler private void treeView1_DragEnter (object sender, DragEventArgs e). {E.Effect = e.AllowedEffect;} // Select the node under the mouse pointer to indicate . the // expected drop location private void treeView1_DragOver (object sender, DragEventArgs e) {// Retrieve the client coordinates of the mouse position Point targetPoint = treeView1.PointToClient (new Point (eX, eY)).;

// select the node at the mouse position. TreeView1.selectedNode = trecess (targetpoint);}

Private void TreeView1_Dragdrop (Object Sender, Drageventargs E) {// Retrieve The Client Coordinates of The Drop location. Point targetpoint = TreeView1.PointToclient (New Point (E.x, E.Y));

// Retrieve The Node At The Drop location. Treenode TargetNode = TreeView1.Getnode (targetpoint);

// Retrieve The Node That Was Dragged. Treenode DraggedNode = (Treenode) E.DATA.GETDATA (TREENODE));

// Confirm that the node at the drop location is not // the dragged node or a descendant of the dragged node. If (! DraggedNode.Equals (targetNode) &&! ContainsNode (draggedNode, targetNode)) {// If it is a move operation, remove the node from its current // location and add it to the node at the drop location if (e.Effect == DragDropEffects.Move) {draggedNode.Remove ();. targetNode.Nodes.Add (draggedNode); }

// If it is a copy operation, clone the dragged node // and add it to the node at the drop location. Else if (e.Effect == DragDropEffects.Copy) {targetNode.Nodes.Add ((TreeNode) draggedNode. Clone ());

// Expand the node at the location // to show the dropped node targetNode.Expand ();.}} // Determine whether one node is a parent // or ancestor of a second node private bool ContainsNode (TreeNode node1, TreeNode. Node2) {// check the parent node of the second node. if (node2.parent == null) Return False; if (node2.parent.equals (node1)) Return True;

// if the parent node is not null or equal to the first node, // Call the contactsnode method, }node (node1, node2.parent);

}

转载请注明原文地址:https://www.9cbs.com/read-94538.html

New Post(0)