Move the treenode node of the treeview by pressing the left and right buttons on the keyboard and pressing the ctrl key.

Source: Internet
Author: User

# Region move a node with the keyboard up/down key and ctrl key

/// <Summary>
/// Move the node by pressing the left and right buttons on the keyboard and pressing the ctrl key.
/// Idea: Copy two selected nodes. 1. To remember the node location selected for the first time, 2. during the migration process, a specified position will be inserted,
/// Then remove the selected node
/// </Summary>
/// <Param name = "treeView"> treeview </param>
/// <Param name = "keys"> keyboard buttons </param>
Public void MoveTreeNodeByKeyWord (TreeView treeView, KeyEventArgs keys)
{
If (treeView. SelectedNode! = Null)
{
// Move up
If (keys. KeyCode = Keys. Up & keys. Control = true)
{

TreeNode tn = new TreeNode ();
Tn = treeView. SelectedNode;
TreeNode tempnode = (TreeNode) treeView. SelectedNode. Clone ();
If (treeView. SelectedNode. PrevNode = null)
{
Return;
}

If (tn. Parent = null)
{
TreeView. Nodes. Insert (tn. Index-1, tempnode );

}
Else
{
Tn. Parent. Nodes. Insert (tn. Index-1, tempnode );
}
TreeView. SelectedNode. Remove ();
TreeView. SelectedNode = tempnode;
}
// Move down
Else if (keys. KeyCode = Keys. Down & keys. Control = true)
{
TreeNode tn = new TreeNode ();
Tn = treeView. SelectedNode;
TreeNode tempnode = (TreeNode) treeView. SelectedNode. Clone ();
If (treeView. SelectedNode. NextNode = null)
{
Return;
}

If (tn. Parent = null)
{
TreeView. Nodes. Insert (tn. Index + 2, tempnode );

}
Else
{
Tn. Parent. Nodes. Insert (tn. Index + 2, tempnode );
}
TreeView. SelectedNode. Remove ();
TreeView. SelectedNode = tempnode;
}
// Left
Else if (keys. KeyCode = Keys. Left & keys. Control = true)
{
TreeNode tn = new TreeNode ();
Tn = treeView. SelectedNode;
TreeNode tempnode = (TreeNode) treeView. SelectedNode. Clone ();
If (treeView. SelectedNode. Parent = null)
{
Return;
}

If (treeView. SelectedNode. Parent. Parent = null)
{
TreeView. Nodes. Add (tempnode );

}
Else
{
Tn. Parent. Parent. Nodes. Add (tempnode );
}
Tn. Remove ();
// TreeView. SelectedNode. Remove ();
TreeView. SelectedNode = tempnode;
}
// Right
Else if (keys. KeyCode = Keys. Right & keys. Control = true)
{
TreeNode tn = new TreeNode ();
Tn = treeView. SelectedNode;
TreeNode tempnode = (TreeNode) treeView. SelectedNode. Clone ();
If (treeView. SelectedNode. NextNode = null)
{
Return;
}

TreeView. SelectedNode. NextNode. Nodes. Insert (0, tempnode );
Tn. Remove ();


TreeView. SelectedNode = tempnode;
}

 


}

}

# Endregion

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.