domingo, 31 de julio de 2011

drag and drop from treeview to listview using C#

 
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);
}
}
}

}
}

0 comentarios:

Publicar un comentario