#region 用键盘上下键和ctrl键移动节点
/// <summary>
/// 用键盘上下左右键和ctrl键移动节点
/// 思路:现复制2个选中的节点。1.为了记住第一次选中节点位置,2在上移过程中现将令一个插入指定位置,
/// 然后将选中节点移除
/// </summary>
/// <param name="treeView">treeview</param>
/// <param name="keys">键盘的按键</param>
public void MoveTreeNodeByKeyWord(TreeView treeView, KeyEventArgs keys)
{
if (treeView.SelectedNode != null)
{
//向上移动
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;
}
//向下移动
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;
}
//向左
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;
}
//向右
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