用键盘上下左右键和ctrl键移动treeview的treenode节点

    #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

转载于:https://www.cnblogs.com/zhbsh/archive/2011/05/25/2056230.html

  • 0
    点赞
  • 0
    评论
  • 0
    收藏
  • 一键三连
    一键三连
  • 扫一扫,分享海报

表情包
插入表情
评论将由博主筛选后显示,对所有人可见 | 还能输入1000个字符
©️2021 CSDN 皮肤主题: 编程工作室 设计师:CSDN官方博客 返回首页
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、C币套餐、付费专栏及课程。

余额充值