Drag and Drop using two TreeViews


E

emferrari

Hi everybody

I have two treeviews, one of them is only to receive items dragged from
the treeview1. I want to know how to drag a full node information to
the treeview2. I know how to do that only in a single node operation.

Here is my current code:

private void trv_Directories_ItemDrag(object sender, ItemDragEventArgs
e)
{
string strItem = e.Item.ToString();

// Start the Drag Operation
DoDragDrop(strItem, DragDropEffects.Copy);
}

private void trv_Directories_DragEnter(object sender, DragEventArgs e)
{
// Determine if we are dragging something, if we are, set the effect
if (e.Data.GetDataPresent(DataFormats.Text))
e.Effect = DragDropEffects.Copy;
else
e.Effect = DragDropEffects.None;
}

private void trv_Selection_DragEnter(object sender, DragEventArgs e)
{
// Handle the Drag effect when the listbox is entered
if (e.Data.GetDataPresent(DataFormats.Text))
e.Effect = DragDropEffects.Copy;
else
e.Effect = DragDropEffects.None;
}

private void trv_Selection_DragDrop(object sender, DragEventArgs e)
{
string dummy = "";
bool itemFound = false;

// Get the string from the data that was dragged
string s = (string)e.Data.GetData(dummy.GetType());
s = s.Substring(s.IndexOf(":") + 1).Trim();
Position.X = e.X;
Position.Y = e.Y;

Position = trv_Selection.PointToClient(Position);

// Checks if the item to be dropped is new for the list
for(int i=0; i<trv_Selection.Nodes.Count; i++)
{
// If finds the item on the list don't do nothing
if(trv_Selection.Nodes.Text == s)
{
itemFound = true;
break;
}
else
itemFound = false;
}

// If the item wasn't found on the list, add it.
if(!itemFound)
// Drop the string in the listbox
trv_Selection.Nodes.Add(s);
}

Any information will be appreciated!

Thanks!

Eduardo
 
Ad

Advertisements

R

Robbe Morris [C# MVP]

http://www.eggheadcafe.com/articles/treeview_databinding.asp

Learn an additional method for databinding a DataSet to a windows forms .NET
TreeView control. Learn how to drag and drop nodes on the same TreeView
control as well as drag and drop nodes across multiple TreeView controls
while maintaining the data bound relationship with the source and target
TreeView's DataSet.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top