• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    公众号

c#winformTreeView与ListView的项互相拖动的应用[转载]

原作者: [db:作者] 来自: [db:来源] 收藏 邀请

转载modede

很久没写教程了,停顿了有两年之久了。今天我们来讨论讨论 winform 中TreeView与ListView,通观两者很相似,只是一个是树形结构,一个是列表结构。

今天自己创建的群里(2175832)有个朋友问到ListView的项如何拖动到TreeView中,并生成子节点,签于麻烦,我回答比较麻烦,用BUTTON实现吧,但他觉得拖动比较方便,那当然哦!无奈帮人帮到底,送佛送到西,于是立即创建本地WINFORM 测试项目,给了点代码给他(其实他本身已经完成了一半代码量了,只是在ListView拖动项到控区域外遇到了不解之疑)

好了,废话不多说了,直接转正题吧。今天我帖了代码是两个功能的结合:
1、ListView项拖动到TreeView
2、TreeView节点项拖动到ListView中

(效果图)

 

主要代码实现如下:

       #region 将LISTVIEW项拖动TREEVIEW中

        /// <summary>
        /// 对选中项指定Move的“开始拖动操作”
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void listView1_ItemDrag(object sender, System.Windows.Forms.ItemDragEventArgs e)
        {
            this.DoDragDrop(e.Item,DragDropEffects.Move);
        }

        private void listView1_DragEnter(object sender, System.Windows.Forms.DragEventArgs e)
        {
            e.Effect=DragDropEffects.Move;
        }

        private void treeView1_DragEnter(object sender, System.Windows.Forms.DragEventArgs e)
        {
            //判定是否现在拖动的数据是LISTVIEW项
            ListViewItem lvi = (ListViewItem)e.Data.GetData(typeof(ListViewItem));
            if (lvi != null)
            {
                e.Effect = DragDropEffects.Move;
            }
            else
                Cursor = Cursors.No;

        }
        private void treeView1_DragDrop(object sender, System.Windows.Forms.DragEventArgs e)
        {
            //获得拖放中的项
            ListViewItem lvi = (ListViewItem)e.Data.GetData(typeof(ListViewItem));

            //根据鼠标坐标确定要移动到的目标节点
            Point pt;
            TreeNode targeNode;
            pt = ((TreeView)(sender)).PointToClient(new Point(e.X, e.Y));
            targeNode = this.treeView1.GetNodeAt(pt);
            TreeNode NewMoveNode  =new TreeNode(lvi.Text);
            if(targeNode!=null)//如果目标节点未选中,则添加到一级节点
            {
                targeNode.Nodes.Add(NewMoveNode);
                //更新当前拖动的节点选择
                treeView1.SelectedNode = NewMoveNode;
                //展开目标节点,便于显示拖放效果
                targeNode.Expand();
                treeView1.Refresh();
            }
            else
            {
                this.treeView1.Nodes.Add(NewMoveNode);
                //更新当前拖动的节点选择
                treeView1.SelectedNode = NewMoveNode;
                //展开目标节点,便于显示拖放效果
                treeView1.ExpandAll();
                treeView1.Refresh();
            }
           
        }
        #endregion
       #region 将treeView的项拖动到ListView

        //定义被选择的项
        private TreeNode _selectednode;

        /// <summary>
        /// 选择treeView的项
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void treeView2_AfterSelect(object sender, System.Windows.Forms.TreeViewEventArgs e)
        {
            try
            {
                _selectednode = e.Node;
            }
            catch { }
        }
        /// <summary>
        /// 对选中项指定Move的“开始拖动操作”
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void treeView2_ItemDrag(object sender, System.Windows.Forms.ItemDragEventArgs e)
        {
            this.DoDragDrop(_selectednode,DragDropEffects.Move);
        }

        private void treeView2_DragEnter(object sender, System.Windows.Forms.DragEventArgs e)
        {
            e.Effect=DragDropEffects.Move;
        }

        /// <summary>
        /// 添加MouseDown是因为,MouseDown先执行AfterSelect,所以在没有AfterSelect时,MouseDown要先选中被选取项
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void treeView2_MouseDown(object sender, System.Windows.Forms.MouseEventArgs e)
        {
            try
            {
                if ((e.Button & MouseButtons.Left) == MouseButtons.Left)
                {
                    _selectednode = treeView2.GetNodeAt(e.X,e.Y);
                    treeView2.SelectedNode = _selectednode;
                }
            }
            catch { }

        }

        private void FillListView(TreeNodeCollection nodes)
        {
            for (int i = 0; i < nodes.Count; i++)
            {
                ListViewItem lvwItem = new ListViewItem(nodes[i].Text, nodes[i].ImageIndex);
                lvwItem.Tag = nodes[i].Tag;
                listView2.Items.Add(lvwItem);
            }
        }

        //当选中项移到listView的工作区时,指定被拖动的目标的移动方式
        private void listView2_DragEnter(object sender, System.Windows.Forms.DragEventArgs e)
        {
            try
            {
                TreeNode node = (TreeNode)e.Data.GetData(typeof(TreeNode));
                if (node != null)
                {
                    e.Effect = DragDropEffects.Move;
                }
                else
                    Cursor = Cursors.No;
            }
            catch { }
            finally { Cursor = Cursors.Default; }

        }

        //当选中项在listView在随SHU标移动时,指定被拖动的目标的移动方式
        private void listView2_MouseMove(object sender, System.Windows.Forms.MouseEventArgs e)
        {
            try
            {
                if (_selectednode != null & (e.Button & MouseButtons.Left) == MouseButtons.Left)
                {
                    DragDropEffects dropEffect = listView2.DoDragDrop(_selectednode, DragDropEffects.Move);
                }
            }
            catch { }

        }

        //当被放入控件时
        private void listView2_DragDrop(object sender, System.Windows.Forms.DragEventArgs e)
        {
            try
            {
                TreeNode node = (TreeNode)e.Data.GetData(typeof(TreeNode));
                if (node != null)
                    listView2.Items.Add(node.Text, node.ImageIndex);
            }
            catch { }
            finally { Cursor = Cursors.Default; }
        }

        #endregion

http://blog.csdn.net/modede/archive/2008/05/27/2486663.aspx


鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
上一篇:
C#for循环嵌套发布时间:2022-07-13
下一篇:
c#TreeView实现三种选中状态发布时间:2022-07-13
热门推荐
热门话题
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap