C#實現樹型結構TreeView節點拖拽的簡易功能)

來源:互聯網
上載者:User

本文摘抄部落格園裡面的牛人吉日嘎啦。http://www.cnblogs.com/jirigala

 

 例子程式運行效果如何下:

  

  當然在節點拖拽時,需要注意幾個事情:

   1:拖拽時總需要有提示資訊比較好,防止誤操作後找不到被托摘到哪裡去了。

   2:父親節點總不能拖拽到自己的子節點上,那不是死迴圈或者亂了輩份了不是?

  為了讓TreeView支援拖拽功能,需要注意以下幾個屬性設定及相應的事件代碼。

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

  

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 代碼實現參考如下:

       private void tvOrganize_ItemDrag(object sender, ItemDragEventArgs e)
        {
            if (this.permissionEdit)
            {
                // 開始進行拖放操作,並將拖放的效果設定成移動。
                this.DoDragDrop(e.Item, DragDropEffects.Move);
            }
        }

        private void tvOrganize_DragEnter(object sender, DragEventArgs e)
        {
            // 拖動效果設成移動
            e.Effect = DragDropEffects.Move;
        }

        private void tvOrganize_DragDrop(object sender, DragEventArgs e)
        {
            // 定義一個中間變數
            TreeNode treeNode;
            //判斷拖動的是否為TreeNode類型,不是的話不予處理
            if (e.Data.GetDataPresent("System.Windows.Forms.TreeNode", false))
            {
                // 拖放的目標節點
                TreeNode targetTreeNode;
                // 擷取當前游標所處的座標
                // 定義一個位置點的變數,儲存當前游標所處的座標點
                Point point = ((TreeView)sender).PointToClient(new Point(e.X, e.Y));
                // 根據座標點取得處於座標點位置的節點
                targetTreeNode = ((TreeView)sender).GetNodeAt(point);
                // 擷取被拖動的節點
                treeNode = (TreeNode)e.Data.GetData("System.Windows.Forms.TreeNode");
                // 判斷拖動的節點與目標節點是否是同一個,同一個不予處理
                if (BaseInterfaceLogic.TreeNodeCanMoveTo(treeNode, targetTreeNode))
                {
                    if (BaseSystemInfo.ShowInformation)
                    {
                        // 是否移動部門
                        if (MessageBox.Show(AppMessage.Format(AppMessage.MSG0038, treeNode.Text, targetTreeNode.Text), AppMessage.MSG0000, MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.No)
                        {
                            return;
                        }
                    }
                    ServiceManager.Instance.OrganizeService.MoveTo(UserInfo, treeNode.Tag.ToString(), targetTreeNode.Tag.ToString());
                    // 往目標節點中加入被拖動節點的一份複製
                    targetTreeNode.Nodes.Add((TreeNode)treeNode.Clone());
                    // 將被拖動的節點移除
                    treeNode.Remove();
                }
            }
        }

 

 

 

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.