通过键盘方向键控制TreeView节点的移动
本文转载:http://dengzebo.blog.163.com/blog/static/18867406201032141932204/
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.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
作者:阿笨
【官方QQ一群:跟着阿笨一起玩NET(已满)】:422315558
【官方QQ二群:跟着阿笨一起玩C#(已满)】:574187616
【官方QQ三群:跟着阿笨一起玩ASP.NET(已满)】:967920586
【官方QQ四群:Asp.Net Core跨平台技术开发(可加入)】:829227829
【官方QQ五群:.NET Core跨平台开发技术(可加入)】:647639415
【网易云课堂】:https://study.163.com/provider/2544628/index.htm?share=2&shareId=2544628
【腾讯课堂】:https://abennet.ke.qq.com
【51CTO学院】:https://edu.51cto.com/sd/66c64
【微信公众号】:微信搜索:跟着阿笨一起玩NET