本文转载:http://dengzebo.blog.163.com/blog/static/18867406201032141932204/
![](https://i-blog.csdnimg.cn/blog_migrate/8f900a89c6347c561fdf2122f13be562.gif)
![ExpandedBlockStart.gif](https://i-blog.csdnimg.cn/blog_migrate/961ddebeb323a10fe0623af514929fc1.gif)
#region "控制树节点移动,向左右下上"
/// <summary>
/// 通过Ctrl+键盘移动选定的树节点
/// </summary>
/// <param name="TreeView1"> 要编辑的TreeView控件 </param>
/// <param name="eKeys"> The <see cref="System.Windows.Forms.KeyEventArgs"/> KeyEventArgs为按键事件提供数据 </param>
public void MoveSelectNode(TreeView TreeView1, KeyEventArgs eKeys)
{
try
{
if (TreeView1.SelectedNode == null)
{
return;
}
try
{
if (eKeys.KeyCode == Keys.Up && eKeys.Control == true)
{
TreeNode TN = new TreeNode();
TN = TreeView1.SelectedNode;
TreeNode TempNode = (TreeNode)TreeView1.SelectedNode.Clone();
if (TreeView1.SelectedNode.PrevNode == null)
{
return;
}
if (TN.Parent == null)
{
TreeView1.Nodes.Insert(TN.Index - 1, TempNode);
}
else
{
TN.Parent.Nodes.Insert(TN.Index - 1, TempNode);
}
TreeView1.SelectedNode.Remove();
TreeView1.SelectedNode = TempNode;
}
else if (eKeys.KeyCode == Keys.Down && eKeys.Control == true)
{
TreeNode TN = new TreeNode();
TN = TreeView1.SelectedNode;
TreeNode TempNode = (TreeNode)TreeView1.SelectedNode.Clone();
if (TreeView1.SelectedNode.NextNode == null)
{
return;
}
if (TN.Parent == null)
{
TreeView1.Nodes.Insert(TN.Index + 2, TempNode);
}
else
{
TN.Parent.Nodes.Insert(TN.Index + 2, TempNode);
}
TreeView1.SelectedNode.Remove();
TreeView1.SelectedNode = TempNode;
}
else if (eKeys.KeyCode == Keys.Left && eKeys.Control == true)
{
TreeNode TN = new TreeNode();
TN = TreeView1.SelectedNode;
TreeNode TempNode = (TreeNode)TreeView1.SelectedNode.Clone();
if (TreeView1.SelectedNode.Parent == null)
{
return;
}
else
{
if (TreeView1.SelectedNode.Parent.Parent == null)
{
TreeView1.Nodes.Add(TempNode);
}
else
{
TN.Parent.Parent.Nodes.Add(TempNode);
}
}
TN.Remove();
TreeView1.SelectedNode = TempNode;
}
else if (eKeys.KeyCode == Keys.Right && eKeys.Control == true)
{
TreeNode TN = new TreeNode();
TN = TreeView1.SelectedNode;
TreeNode TempNode = (TreeNode)TreeView1.SelectedNode.Clone();
if (TreeView1.SelectedNode.NextNode == null)
{
return;
}
TN.NextNode.Nodes.Insert( 0, TempNode);
TN.Remove();
TreeView1.SelectedNode = TempNode;
}
}
catch
{
}
}
catch
{ }
}
#endregion
/// <summary>
/// 通过Ctrl+键盘移动选定的树节点
/// </summary>
/// <param name="TreeView1"> 要编辑的TreeView控件 </param>
/// <param name="eKeys"> The <see cref="System.Windows.Forms.KeyEventArgs"/> KeyEventArgs为按键事件提供数据 </param>
public void MoveSelectNode(TreeView TreeView1, KeyEventArgs eKeys)
{
try
{
if (TreeView1.SelectedNode == null)
{
return;
}
try
{
if (eKeys.KeyCode == Keys.Up && eKeys.Control == true)
{
TreeNode TN = new TreeNode();
TN = TreeView1.SelectedNode;
TreeNode TempNode = (TreeNode)TreeView1.SelectedNode.Clone();
if (TreeView1.SelectedNode.PrevNode == null)
{
return;
}
if (TN.Parent == null)
{
TreeView1.Nodes.Insert(TN.Index - 1, TempNode);
}
else
{
TN.Parent.Nodes.Insert(TN.Index - 1, TempNode);
}
TreeView1.SelectedNode.Remove();
TreeView1.SelectedNode = TempNode;
}
else if (eKeys.KeyCode == Keys.Down && eKeys.Control == true)
{
TreeNode TN = new TreeNode();
TN = TreeView1.SelectedNode;
TreeNode TempNode = (TreeNode)TreeView1.SelectedNode.Clone();
if (TreeView1.SelectedNode.NextNode == null)
{
return;
}
if (TN.Parent == null)
{
TreeView1.Nodes.Insert(TN.Index + 2, TempNode);
}
else
{
TN.Parent.Nodes.Insert(TN.Index + 2, TempNode);
}
TreeView1.SelectedNode.Remove();
TreeView1.SelectedNode = TempNode;
}
else if (eKeys.KeyCode == Keys.Left && eKeys.Control == true)
{
TreeNode TN = new TreeNode();
TN = TreeView1.SelectedNode;
TreeNode TempNode = (TreeNode)TreeView1.SelectedNode.Clone();
if (TreeView1.SelectedNode.Parent == null)
{
return;
}
else
{
if (TreeView1.SelectedNode.Parent.Parent == null)
{
TreeView1.Nodes.Add(TempNode);
}
else
{
TN.Parent.Parent.Nodes.Add(TempNode);
}
}
TN.Remove();
TreeView1.SelectedNode = TempNode;
}
else if (eKeys.KeyCode == Keys.Right && eKeys.Control == true)
{
TreeNode TN = new TreeNode();
TN = TreeView1.SelectedNode;
TreeNode TempNode = (TreeNode)TreeView1.SelectedNode.Clone();
if (TreeView1.SelectedNode.NextNode == null)
{
return;
}
TN.NextNode.Nodes.Insert( 0, TempNode);
TN.Remove();
TreeView1.SelectedNode = TempNode;
}
}
catch
{
}
}
catch
{ }
}
#endregion