本文转载:http://dengzebo.blog.163.com/blog/static/18867406201032141932204/
///<summary>///通过Ctrl+键盘移动选定的树节点
///</summary>///<paramname="TreeView1">要编辑的TreeView控件</param>///<paramname="eKeys">The<seecref="System.Windows.Forms.KeyEventArgs"/>KeyEventArgs为按键事件提供数据</param>publicvoidMoveSelectNode(TreeViewTreeView1,KeyEventArgseKeys)
{
try
{
if(TreeView1.SelectedNode==null)
{
return;
}
try
{
if(eKeys.KeyCode==Keys.Up&&eKeys.Control==true)
{
TreeNodeTN=newTreeNode();
TN=TreeView1.SelectedNode;
TreeNodeTempNode=(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;
}
elseif(eKeys.KeyCode==Keys.Down&&eKeys.Control==true)
{
TreeNodeTN=newTreeNode();
TN=TreeView1.SelectedNode;
TreeNodeTempNode=(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;
}
elseif(eKeys.KeyCode==Keys.Left&&eKeys.Control==true)
{
TreeNodeTN=newTreeNode();
TN=TreeView1.SelectedNode;
TreeNodeTempNode=(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;
}
elseif(eKeys.KeyCode==Keys.Right&&eKeys.Control==true)
{
TreeNodeTN=newTreeNode();
TN=TreeView1.SelectedNode;
TreeNodeTempNode=(TreeNode)TreeView1.SelectedNode.Clone();
if(TreeView1.SelectedNode.NextNode==null)
{
return;
}
TN.NextNode.Nodes.Insert(0,TempNode);
TN.Remove();
TreeView1.SelectedNode=TempNode;
}
}
catch
{
}
}
catch
{}
}
#endregion