C # Move the Treeview Tree node up and down

Source: Internet
Author: User
/// <Summary> /// move up /// </Summary> /// <Param name = "sender"> </param> /// <Param name = "E"> </param> private void tsbmoveup_click (Object sender, eventargs e) {If (TV _tree.selectednode = NULL) {MessageBox. show ("select the node to be moved! "); This. focus (); return;} folder model = new folder (); treenode node = TV _tree.selectednode; // obtain the value model of the selected node. folderid = node. tag. tostring (); Model. theorder = node. name. tostring (); If (node. prevnode = NULL) // if the previous node of the selected node is null, {return;} folder upmodel = new folder (); treenode upnode = node is returned. prevnode; // obtain the upmodel of the previous node of the selected node. folderid = upnode. tag. tostring (); upmodel. theorder = upnode. name; bool Flag = folderoperation. moveup (model, upmodel); // swaps the order numbers of the two nodes if (FLAG) // if the execution is successful {string theorder = node. name; node. name = upnode. name; upnode. name = theorder; treenode newnode = (treenode) node. clone (); // clone the selected node if (node. parent = NULL) {TV _tree.nodes.insert (upnode. index, newnode); // Insert the current node to the previous node of the selected node} else {node. parent. nodes. insert (upnode. index, newnode);} node. remove (); TV _tree.selectedn Ode = newnode;} else {return ;}} /// <summary> /// move down /// </Summary> /// <Param name = "sender"> </param> /// <Param name =" E "> </param> private void tsbmovedown_click (Object sender, eventargs e) {If (TV _tree.selectednode = NULL) {MessageBox. show ("select the node to be moved! "); This. focus (); return;} folder model = new folder (); treenode node = TV _tree.selectednode; // selected node model. folderid = node. tag. tostring (); Model. theorder = node. name. tostring (); If (node. nextnode = NULL) // if the next node is null, {return;} folder downmodel = new folder (); treenode downode = node is returned. nextnode; downmodel. folderid = downode. tag. tostring (); downmodel. theorder = downode. name; bool flag = folderoperation. moveup (model, downmodel); If (FLAG) {string theorder = node. name; node. name = downode. name; downode. name = theorder; treenode newnode = (treenode) downode. clone (); // clone the next node and all its sub-nodes if (node. parent = NULL) {TV _tree.nodes.insert (node. index, newnode); // Insert the value of the next node to the original node} else {node. parent. nodes. insert (node. index, newnode);} downode. remove (); // Delete the next node TV _tree.selectednode = node; // re-select} else {return;} string SQL = string. format ("Update folder set theorder = '{0}' Where folderid = '{1}'", model. theorder, upmodel. folderid); string sql1 = string. format ("Update folder set theorder = '{0}' Where folderid = '{1}'", upmodel. theorder, model. folderid );
/// <Summary> /// expand and collapse nodes /// </Summary> /// <Param name = "sender"> </param> // <param name = "E"> </param> private void tvdocinfo_afterexpand (Object sender, treevieweventargs e) {If (E. node. parent = NULL) {Treeview tree = (Treeview) sender; tree. aftercheck-= new treevieweventhandler (this. tvdocinfo_afterexpand); // Fold other peer nodes treenode parentnode = E. node. parent; If (parentnode! = NULL) {foreach (treenode tn in parentnode. nodes) {If (TN! = E. node) tn. Collapse () ;}} else {foreach (treenode tn in tree. nodes) {If (TN! = E. node) tn. Collapse ();}}}}

Drag a node. You must set the allowdrop attribute of Treeview to true;

Private point position = new point (0, 0); Private void treeview1_itemdrag (Object sender, itemdrageventargs e) {dodragdrop (E. item, dragdropeffects. move);} private void treeviewincludragenter (Object sender, drageventargs e) {If (E. data. getdatapresent (typeof (treenode) E. effect = dragdropeffects. move; else E. effect = dragdropeffects. none;} private void treeviewincludragdrop (Object sender, drageventargs E) {treenode mynode = NULL; If (E. data. getdatapresent (typeof (treenode) {mynode = (treenode) (E. data. getdata (typeof (treenode);} else {MessageBox. show ("error");} position. X = E. x; position. y = E. y; position = tvtype. pointtoclient (position); treenode dropnode = This. tvtype. getnodeat (position); If (functionsui. createquestiondialog ("whether to add" + mynode. text + "mobile" + dropnode. text + "under? ") = Dialogresult. OK) {// 1. The target node is not empty. 2. The target node is not the byte point of the dragged contact. 3. If (dropnode! = NULL & dropnode. parent! = Mynode & dropnode! = Mynode) {treenode dragnode = mynode; // The dragged node is deleted from the original position. Mynode. remove (); // Add the dropnode to the target node. nodes. add (dragnode);} // if the target node does not exist, that is, if the drag position does not exist, place the dragged node under the root node if (dropnode = NULL) {treenode dragnode = mynode; mynode. remove (); tvtype. nodes. add (dragnode );}}}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.