欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

Csharp: TreeList Drag and Drop

程序员文章站 2022-03-31 18:01:52
/// /// https://www.codeproject.com/articles/3225/treelistview /// https://www.codeproject.com/Tips/572728/TreeListViewEx-Tree-List-and-Drag-and-Drop ... ......
  /// <summary>
    /// https://www.codeproject.com/articles/3225/treelistview
    /// https://www.codeproject.com/tips/572728/treelistviewex-tree-list-and-drag-and-drop     
    /// https://sourceforge.net/projects/objectlistview/
    /// 涂聚文 20190704
    /// geovin du
    /// </summary>
    public partial class trytreelistview : form
    {
        list<int> selectindex = new list<int>();//
  
        treelistviewitem draggednode=new treelistviewitem();
        /// <summary>
        /// 
        /// </summary>
        public string draggtext
        {
            set;
            get;
        }
        /// <summary>
        /// 
        /// </summary>
        string beforeedit = "";
        /// <summary>
        /// 
        /// </summary>
        string beforeeditname = "";
        /// <summary>
        /// 
        /// </summary>
        string afteredit = "";
        string nodechildid = "";
        string nodeparentid = "";
        string nodechild = "";
        string nodeparent = "";
        string nodetitle = "";
        /// <summary>
        /// 
        /// </summary>
        list<treelistviewitem> getlist = new list<treelistviewitem>();
        /// <summary>
        /// 
        /// </summary>
        /// <returns></returns>
       datatable setlist()
       {

            datatable dt = new datatable();
            dt.columns.add("id", typeof(int));
            dt.columns.add("name", typeof(string));
            dt.columns.add("parentid", typeof(int));
            dt.columns.add("level", typeof(int));
            dt.rows.add(1, "六福集团", 0,0);
            dt.rows.add(2, "广州六福", 1,1);
            dt.rows.add(3, "北京六福", 1,1);
            dt.rows.add(4, "上海六福", 1,1);
            dt.rows.add(5, "深圳六福", 1,1);
            dt.rows.add(6, "人事部", 5,2);
            dt.rows.add(7, "科技部", 5,2);
            dt.rows.add(8, "恒大集团", 0, 0);
            dt.rows.add(9, "恒大广州", 8, 1);
            dt.rows.add(10, "恒大北京", 8, 1);
            dt.rows.add(11, "恒大上海", 8, 1);
            dt.rows.add(12, "恒大深圳", 8, 1);
            dt.rows.add(13, "人事部", 11, 2);
            dt.rows.add(14, "科技部", 11, 2);


            return dt;
       }
       /// <summary>
       /// 
       /// </summary>
       /// <param name="tr1"></param>
       /// <param name="parentid"></param>
       private void settreeview(treeview tr1, int parentid)
       {

           datarow[] ds = setlist().select("parentid=" + parentid.tostring());
           if (ds.length > 0)
           {
               int pid = -1;
               foreach (datarow row in ds)
               {
                   treenode node = new treenode();
                   node.text = row["name"].tostring();
                   node.tag = (int)row["id"];
                   pid = (int)row["parentid"];
                   if (pid == 0)
                   {
                       //添加根节点
                       tr1.nodes.add(node);
                   }
                   else
                   {
                       //添加根节点之外的其他节点
                       refreshchildnode(tr1, node, pid);
                   }
                   //查找以node为父节点的子节点
                   settreeview(tr1, (int)node.tag);

               }
           }
                  
       }
        /// <summary>
        /// 
        /// </summary>
        public void bind()
       {

           columnheader head=new columnheader();
           head.text = "level";        
           treelistview1.columns.add(head);
           datarow[] ds = setlist().select("parentid=0");
           
           

           foreach (datarow row in ds)
           {

               //第一层
               treelistviewitem itema = new treelistviewitem(row["name"].tostring(), 0);
               itema.expand();
               itema.subitems.add(row["id"].tostring());
               itema.subitems.add(row["parentid"].tostring());
               itema.subitems.add(row["level"].tostring());
               itema.tooltiptext = row["parentid"].tostring();
               treelistview1.items.add(itema);
               datarow[] ds1 = setlist().select("parentid=" + row["id"].tostring());
               if (ds1.length > 0)
               {
                   //第二层
                   foreach (datarow row1 in ds1)
                   {
                       if (row["id"].tostring() == row1["parentid"].tostring())
                       {
                           treelistviewitem item = new treelistviewitem(row1["name"].tostring(), 1);
                           item.subitems.add(row1["id"].tostring());
                           item.subitems.add(row1["parentid"].tostring());
                           item.subitems.add(row1["level"].tostring());
                           item.tooltiptext = row1["parentid"].tostring();
                           itema.items.add(item);

                           datarow[] ds2 = setlist().select("parentid=" + row1["id"].tostring());
                           if (ds2.length > 0)
                           {
                               foreach (datarow row2 in ds2)
                               {
                                   //第三层
                                   if (row1["id"].tostring() == row2["parentid"].tostring())
                                   {
                                       treelistviewitem item2 = new treelistviewitem(row2["name"].tostring(), 2);
                                       item2.subitems.add(row2["id"].tostring());
                                       item2.subitems.add(row2["parentid"].tostring());
                                       item2.subitems.add(row2["level"].tostring());
                                       item2.tooltiptext = row2["parentid"].tostring();
                                       item.items.add(item2);
                                   }


                               }
                           }
                       }    
                   }
               }



           }

       }
       /// <summary>
       /// 处理根节点的子节点
       /// </summary>
       /// <param name="tr1"></param>
       /// <param name="treenode"></param>
       /// <param name="parentid"></param>
       private void refreshchildnode(treeview tr1, treenode treenode, int parentid)
       {
           foreach (treenode node in tr1.nodes)
           {
               if ((int)node.tag == parentid)
               {
                   node.nodes.add(treenode);
                   return;
               }
               else if (node.nodes.count > 0)
               {
                   findchildnode(node, treenode, parentid);
               }
           }
       }
       /// <summary>
       /// 处理根节点的子节点的子节点
       /// </summary>
       /// <param name="tnode"></param>
       /// <param name="treenode"></param>
       /// <param name="parentid"></param>
       private void findchildnode(treenode tnode, treenode treenode, int parentid)
       {

           foreach (treenode node in tnode.nodes)
           {
               if ((int)node.tag == parentid)
               {
                   node.nodes.add(treenode);
                   return;
               }
               else if (node.nodes.count > 0)
               {
                   findchildnode(node, treenode, parentid);

               }

           }

       }


        /// <summary>
        /// 
        /// </summary>
        public trytreelistview()
        {
            initializecomponent();
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void trytreelistview_load(object sender, eventargs e)
        {
            settreeview(this.treeview1, 0);
            this.treeview1.expandall();
            bind();
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void treelistview1_itemcheck(object sender, itemcheckeventargs e)
        {

            treelistviewitem item = new treelistviewitem();
            //messagebox.show(e.index.tostring());
            // treelistviewitem
            //messagebox.show(treelistview1.selecteditems.count.tostring());
            // messagebox.show(treelistview1.items[0].subitems[0].text);
            //messagebox.show(treelistview1.gettreelistviewitemfromindex(e.index).text);
            if (treelistview1.gettreelistviewitemfromindex(e.index).checked == false)
            {

                //messagebox.show(treelistview1.gettreelistviewitemfromindex(e.index).subitems[0].text);  //从子节点开始
                item = treelistview1.gettreelistviewitemfromindex(e.index);
                getlist.add(item);
            }

            //取消
            //if (treelistview1.gettreelistviewitemfromindex(e.index).checked == true)
            //{
            //    messagebox.show(treelistview1.gettreelistviewitemfromindex(e.index).subitems[0].text);
            //}

            //messagebox.show(e.index.tostring());
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void treelistview1_itemactivate(object sender, eventargs e)
        {
            
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void button1_click(object sender, eventargs e)
        {
            this.textbox1.text=getlist.count.tostring();
            string s = getlist[0].fullpath; //节点路径
            string l=getlist[0].level.tostring();
            string ss = getlist[0].parent.text;
            string sb = getlist[0].parent.subitems[0].text;
            form1 frm = new form1();
            frm.fullpath = getlist[0].fullpath; //节点路径
            frm.editname = getlist[0].text;
            frm.id = getlist[0].subitems[1].text;
            if (frm.showdialog() == dialogresult.ok)
            {

            }
             getlist = new list<treelistviewitem>();

          

            
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void treelistview1_itemmousehover(object sender, listviewitemmousehovereventargs e)
        {
            //messagebox.show(e.item.text);
        }
        /// <summary>
        /// 编辑前的值
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void treelistview1_beforelabeledit(object sender, treelistviewbeforelabelediteventargs e)
        {
            messagebox.show(e.item.text);
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void treelistview1_afterlabeledit(object sender, treelistviewlabelediteventargs e)
        {
            messagebox.show(e.item.text);
        }
        /// <summary>
        /// 拖放完成发生
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void treelistview1_dragdrop(object sender, drageventargs e)
        {
            try
            {

                // retrieve the client coordinates of the drop location.
                point targetpoint = treelistview1.pointtoclient(new point(e.x, e.y));
                //所要放置的位置
                // retrieve the node at the drop location.
                treelistviewitem targetnode = treelistview1.getitemat(targetpoint);
                treelistviewitem fullnode = treelistview1.getitematfullrow(targetpoint);
                // draggednode = (treelistviewitem)e.data.getdata(typeof(treelistviewitem));
                //string s = e.data.getdata(typeof(treelistviewitem)).tostring();
                system.windows.forms.treelistview.dragitemdata data = (system.windows.forms.treelistview.dragitemdata)e.data.getdata(typeof(system.windows.forms.treelistview.dragitemdata));
                draggednode = (treelistviewitem)data.dragitems[0];

                if (!draggednode.equals(targetnode) && !containsnode(draggednode, targetnode))
                {
                    // if it is a move operation, remove the node from its current 
                    // location and add it to the node at the drop location.
                    if (e.effect == dragdropeffects.move)
                    {


                        //draggednode.remove();
                        // int indx=targetnode.index;
                        // targetnode.items.insert(draggednode.index,draggednode);
                        //treelistview1.items.insert(indx, targetnode);

                        draggednode.remove();
                        targetnode.items.add(draggednode);

                        // targetnode.items.insert(indx, draggednode); //父节点,添加移动的子节点

                        nodechildid = draggednode.subitems[1].text;//子节点
                        nodeparentid = targetnode.subitems[1].text;//父节点
                        nodechild = draggednode.text;//子节点
                        nodeparent = targetnode.text;//父节点
                        nodetitle = draggednode.tooltiptext;

                        //bookkindlistinfo.bookkindid = int.parse(nodechildid);
                        //bookkindlistinfo.bookkindparent = int.parse(nodeparentid);
                        //bookkindlistinfo.bookkindname = nodechild;
                        //bookkindlistinfo.bookkindcode = nodetitle;
                        //this.textbox1.text = nodeparent;
                        //this.textbox2.text = nodechild;
                        //this.textbox3.text = nodetitle;
                        //formoperatingstringinfo.nodemoveupdated = true;

                        //button1.text = "移動區域節點";

                        //administrativearealistinfo.areaid = int.parse(nodechildid);                  
                        //administrativearealistinfo.areaparentid = int.parse(nodeparentid);
                        //administrativearealistinfo.areaname = nodechild;
                        //int k = bookkindlistbll.updatebookkindlist(bookkindlistinfo);
                        //if (k >= 1)
                        //{
                        //    messagebox.show("移動书目录成功!");
                        //    initdatatable();
                        //    bindroot();
                        //    treeview1.expandall(); //全部展开
                        //}
                        //else
                        //{
                        //    messagebox.show("移動书目录不成功!");
                        //}


                        /// messagebox.show("子节点:" + nodechild + ",父节点:" + nodeparent, "拖动节点", messageboxbuttons.ok, messageboxicon.information);
                    }

                    // if it is a copy operation, clone the dragged node 
                    // and add it to the node at the drop location.
                    else if (e.effect == dragdropeffects.copy)
                    {
                        targetnode.items.add((treelistviewitem)draggednode.clone());
                    }

                    // expand the node at the location 
                    // to show the dropped node.
                    targetnode.expand();
                }
            }
            catch (exception ex)
            {
                ex.message.tostring();
            }
        }

        /// <summary>
        ///determine whether one node is a parent 
        ///or ancestor of a second node.
        /// </summary>
        /// <param name="node1"></param>
        /// <param name="node2"></param>
        /// <returns></returns>
        private bool containsnode(treelistviewitem node1, treelistviewitem node2)
        {
            // check the parent node of the second node.
            if (node2.parent == null) return false;
            if (node2.parent.equals(node1)) return true;

            // if the parent node is not null or equal to the first node, 
            // call the containsnode method recursively using the parent of 
            // the second node.
            return containsnode(node1, node2.parent);
        }

        /// <summary>
        /// 施放离开边界时发生
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void treelistview1_dragleave(object sender, eventargs e)
        {
            
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void treelistview1_dragenter(object sender, drageventargs e)
        {
            e.effect = e.allowedeffect;
            
            
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void treelistview1_itemdrag(object sender, itemdrageventargs e)
        {
            // move the dragged node when the left mouse button is used.
            if (e.button == mousebuttons.left)
            {
                dodragdrop(e.item, dragdropeffects.move);
                draggednode = (treelistviewitem)e.item;

            }

            // copy the dragged node when the right mouse button is used.
            else if (e.button == mousebuttons.right)
            {
                dodragdrop(e.item, dragdropeffects.copy);
                draggednode = (treelistviewitem)e.item;
            }
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void treelistview1_dragover(object sender, drageventargs e)
        {
            //draggednode = (treelistviewitem)e.data.getdata(typeof(treelistviewitem));

                //point draggedpoint = treelistview1.pointtoclient(new point(e.x, e.y));
                //draggednode = treelistview1.getitemat(draggedpoint);         


        }
        /// <summary>
        /// 双控件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void treelistview1_doubleclick(object sender, eventargs e)
        {
           string selectedname=treelistview1.selecteditems[0].text;
           form1 frm = new form1();
           frm.fullpath = treelistview1.selecteditems[0].fullpath; //节点路径
           frm.editname = treelistview1.selecteditems[0].text;
           frm.id = treelistview1.selecteditems[0].subitems[1].text;
           if (frm.showdialog() == dialogresult.ok)
           {

           }
           this.textbox1.text = selectedname;
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void treelistview1_leave(object sender, eventargs e)
        {
            if (treelistview1.selecteditems!= null)
            {
                //让选中项背景色呈现红色
                treelistview1.selecteditems[0].backcolor = color.red;
                //前景色为白色
                treelistview1.selecteditems[0].forecolor = color.white;
            }
        }
        /// <summary>
        /// 添加子节点
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void addtoolstripmenuitem_click(object sender, eventargs e)
        {

        }
        /// <summary>
        /// 编辑当前节点
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void edittoolstripmenuitem_click(object sender, eventargs e)
        {

        }
        /// <summary>
        /// 删除当前节点
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void deltoolstripmenuitem_click(object sender, eventargs e)
        {

        }
        /// <summary>
        /// 添加同层节点
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void addleveltoolstripmenuitem_click(object sender, eventargs e)
        {

        }


    }

  Csharp: TreeList Drag and Drop