C# TreeView樹節點上下移動

來源:互聯網
上載者:User
  /// <summary>        /// 上移        /// </summary>        /// <param name="sender"></param>        /// <param name="e"></param>        private void tsbMoveUp_Click(object sender, EventArgs e)        {            if (Tv_tree.SelectedNode == null)            {                MessageBox.Show("請選擇需要移動的的節點!");                this.Focus();                return;            }            folder  model = new folder();            TreeNode node = Tv_tree.SelectedNode;     //獲得選中節點的值            model.folderId = node.Tag.ToString();            model.TheOrder = node.Name.ToString();            if (node.PrevNode == null)      //選中節點的上一個節點為Null則返回            {                return;            }            folder upModel = new folder();            TreeNode upNode = node.PrevNode;            //獲得選中節點的上一個節點            upModel.folderId = upNode.Tag.ToString();            upModel.TheOrder = upNode.Name;            bool flag = folderOperation.MoveUp(model, upModel);       //將兩個節點的排序編號互換            if (flag)       //如果執行成功            {                string theOrder = node.Name;                node.Name = upNode.Name;                upNode.Name = theOrder;                TreeNode newNode = (TreeNode)node.Clone();      //複製選中的節點                if (node.Parent == null)                {                    Tv_tree.Nodes.Insert(upNode.Index, newNode);      //在選中節點的上一個節點的地方插入本節點                }                else                {                    node.Parent.Nodes.Insert(upNode.Index, newNode);                }                node.Remove();                Tv_tree.SelectedNode = newNode;            }            else            {                return;            }        }        /// <summary>        /// 下移        /// </summary>        /// <param name="sender"></param>        /// <param name="e"></param>        private void tsbMoveDown_Click(object sender, EventArgs e)        {            if (Tv_tree.SelectedNode == null)            {               MessageBox.Show("請選擇需要移動的的節點!");                this.Focus();                return;            }            folder model = new folder();            TreeNode node = Tv_tree.SelectedNode;     //選中的節點            model.folderId = node.Tag.ToString();            model.TheOrder = node.Name.ToString();            if (node.NextNode == null)      //下一個節點為Null時返回            {                return;            }            folder downModel = new folder();            TreeNode dowNode = node.NextNode;            downModel.folderId = dowNode.Tag.ToString();            downModel.TheOrder = dowNode.Name;            bool flag = folderOperation.MoveUp(model, downModel);            if (flag)            {                string theOrder = node.Name;                node.Name = dowNode.Name;                dowNode.Name = theOrder;                TreeNode newNode = (TreeNode)dowNode.Clone();       //複製下一個節點及其所有的子節點                if (node.Parent == null)                {                    Tv_tree.Nodes.Insert(node.Index, newNode);    //在原節點的位置插入下一個節點的值                }                else                {                    node.Parent.Nodes.Insert(node.Index, newNode);                }                dowNode.Remove();           //刪除掉選中節點的下一個節點                Tv_tree.SelectedNode = node;  //重新選中            }            else            {                return;            }        }  string sql = string.Format("update folder set theorder = '{0}' where folderid = '{1}'", model.TheOrder, upModel.folderId);            string sql1 = string.Format("update folder set theorder = '{0}' where folderid = '{1}'", upModel.TheOrder, model.folderId);
     /// <summary>        /// 節點的展開和摺疊        /// </summary>        /// <param name="sender"></param>        /// <param name="e"></param>        private void tvDocInfo_AfterExpand(object sender, TreeViewEventArgs e)        {            if (e.Node.Parent == null)            {                TreeView tree = (TreeView)sender;                tree.AfterCheck -= new TreeViewEventHandler(this.tvDocInfo_AfterExpand);                //摺疊其他同級節點                   TreeNode parentNode = e.Node.Parent;                if (parentNode != null)                {                    foreach (TreeNode tn in parentNode.Nodes)                    {                        if (tn != e.Node)                            tn.Collapse();                    }                }                else                {                    foreach (TreeNode tn in tree.Nodes)                    {                        if (tn != e.Node)                            tn.Collapse();                    }                }            }        }

節點拖拽,必須設定TreeView的AllowDrop屬性為True;

        private Point Position = new Point(0, 0);        private void treeView1_ItemDrag(object sender, ItemDragEventArgs e)        {            DoDragDrop(e.Item, DragDropEffects.Move);        }        private void treeView1_DragEnter(object sender, DragEventArgs e)        {            if (e.Data.GetDataPresent(typeof(TreeNode)))                e.Effect = DragDropEffects.Move;            else                e.Effect = DragDropEffects.None;        }        private void treeView1_DragDrop(object sender, DragEventArgs e)        {            TreeNode myNode = null;            if (e.Data.GetDataPresent(typeof(TreeNode)))            {                myNode = (TreeNode)(e.Data.GetData(typeof(TreeNode)));            }            else            {                MessageBox.Show("error");            }            Position.X = e.X;            Position.Y = e.Y;            Position = tvType.PointToClient(Position);            TreeNode DropNode = this.tvType.GetNodeAt(Position);            if (FunctionsUI.CreateQuestionDialog("是否要將"+myNode.Text+"移動"+DropNode.Text+"類別下?") == DialogResult.OK)            {                // 1.目標節點不是空。2.目標節點不是被拖拽接點的位元組點。3.目標節點不是被拖拽節點本身                if (DropNode != null && DropNode.Parent != myNode && DropNode != myNode)                {                    TreeNode DragNode = myNode;                    // 將被拖拽節點從原來位置刪除。                    myNode.Remove();                    // 在目標節點下增加被拖拽節點                    DropNode.Nodes.Add(DragNode);                }                // 如果目標節點不存在,即拖拽的位置不存在節點,那麼就將被拖拽節點放在根節點之下                if (DropNode == null)                {                    TreeNode DragNode = myNode;                    myNode.Remove();                    tvType.Nodes.Add(DragNode);                }            }        }

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.