CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 5 of 5
  1. #1
    Join Date
    Sep 2008
    Posts
    1

    drag and drop from treeview to listview using C#

    Hi...

    Could anyone show me the C# code to Drag and Drop from a Treeview to a Listview?

    Thank You in advance...

  2. #2
    Join Date
    Mar 2008
    Location
    IRAN
    Posts
    811

    Re: drag and drop from treeview to listview using C#

    hopefully i have aleardy implemented such problem:

    here is the code:

    Code:
            private void treeView1_ItemDrag(object sender, ItemDragEventArgs e)
            {
                treeView1.DoDragDrop(e.Item, DragDropEffects.Move);
                string txt;
                txt = null;
                
                txt += e.Item.ToString();
                textBox2.Text = txt;
                
            }
    
            private void listView1_DragEnter(object sender, DragEventArgs e)
            {
                // this code can be in DragOver also
                if (e.Data.GetDataPresent(typeof(TreeNode)))
                    e.Effect = DragDropEffects.Move;
            }
    
            private void listView1_DragDrop(object sender, DragEventArgs e)
            {
             
                if (e.Data.GetDataPresent(typeof(TreeNode)))
                {
                    TreeNode tn = (TreeNode) e.Data.GetData
                    (typeof(TreeNode));
                    listView1.Items.Add(tn.Text, tn.ImageIndex);
                    treeView1.Nodes.Remove(tn);
                }
            }
    the workhorse of the code is in ItemDrag, DragEnter and DragDrop events.
    Please rate my post if it was helpful for you.
    Java, C#, C++, PHP, ASP.NET
    SQL Server, MySQL
    DirectX
    MATH
    Touraj Ebrahimi
    [toraj_e] [at] [yahoo] [dot] [com]

  3. #3
    Join Date
    Sep 2004
    Posts
    8

    Question Re: drag and drop from treeview to listview using C#

    Hello

    Can I have the code for in reverse implementation with duplicate check while adding to tree view.

    Thanks,
    Ranga

  4. #4
    Join Date
    Mar 2008
    Location
    IRAN
    Posts
    811

    Re: drag and drop from treeview to listview using C#

    just reverse the usage of listview and treeview.
    a little twist in the code will do the job for you.
    Please rate my post if it was helpful for you.
    Java, C#, C++, PHP, ASP.NET
    SQL Server, MySQL
    DirectX
    MATH
    Touraj Ebrahimi
    [toraj_e] [at] [yahoo] [dot] [com]

  5. #5
    Join Date
    Mar 2008
    Location
    IRAN
    Posts
    811

    Re: drag and drop from treeview to listview using C#

    i got time and wrote the code for you.
    this time darg from listview to tree with duplicate check:

    Code:
    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Text;
    using System.Windows.Forms;
    
    namespace DragFromListToTree
    {
        public partial class Form1 : Form
        {
            public Form1()
            {
                InitializeComponent();
                //listView1.AllowDrop = true;
                treeView1.AllowDrop = true;
            }
       
            private void listView1_ItemDrag(object sender, ItemDragEventArgs e)
            {
                listView1.DoDragDrop(e.Item, DragDropEffects.Move);
    
            }
    
            private void treeView1_DragEnter(object sender, DragEventArgs e)
            {
                // this code can be in DragOver also
                if (e.Data.GetDataPresent(typeof(ListViewItem)))
                    e.Effect = DragDropEffects.Move;
            }
    
            private void treeView1_DragDrop(object sender, DragEventArgs e)
            {
                if (e.Data.GetDataPresent(typeof(ListViewItem)))
                {
        
                    ListViewItem li = (ListViewItem)e.Data.GetData(typeof(ListViewItem));
    
                    bool dupplicate = false;
    
                    foreach (TreeNode tn in treeView1.Nodes)
                    {
                        if (tn.Text == li.Text) dupplicate = true;
                    }
    
                    if (!dupplicate)
                    {
                        treeView1.Nodes.Add(li.Text);
                        listView1.Items.Remove(li);
                    }
                }
            }
    
        }
    }
    Please rate my post if it was helpful for you.
    Java, C#, C++, PHP, ASP.NET
    SQL Server, MySQL
    DirectX
    MATH
    Touraj Ebrahimi
    [toraj_e] [at] [yahoo] [dot] [com]

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured