ybt
asked on
C# drag and drop treeview item
In C# I'm trying to implement drag and drop functionality
I used code:
But when I press mouse button on a third level node, I got a root node value and if I use event:
user has to click twice, first to select and second to drag, how to do it in a right way?
I used code:
private void treeView1_MouseDown(object sender, MouseEventArgs e)
{
treeView1.Select();
}
private void treeView1_ItemDrag(object sender, ItemDragEventArgs e)
{
if (treeView1.SelectedNode.FullPath.Substring(treeView1.SelectedNode.FullPath.Length - 4, 4).ToLower() == ".pdf")
{
ls_selected_node = treeView1.SelectedNode.FullPath;
}
if(string.IsNullOrEmpty(ls_selected_node) == true)
{
MessageBox.Show("Select a valid File.", "Warning",MessageBoxButtons.OK,MessageBoxIcon.Warning);
return;
}
if (e.Button == MouseButtons.Left)
{
string file = dirpath1_t + ls_policy_no + "\\" + ls_selected_node;
string[] files = new string[1];
files[0] = @file;
DataObject data = new DataObject(DataFormats.FileDrop, files);
DoDragDrop(data, DragDropEffects.Copy);
ls_selected_node = "";
}
}
But when I press mouse button on a third level node, I got a root node value and if I use event:
private void treeView1_AfterSelect(object sender, TreeViewEventArgs e)
{
treeView1.Select();
}
user has to click twice, first to select and second to drag, how to do it in a right way?
ASKER
You probably did not realize what I need I have a treeview and need to drag pdf item and drop it to outlook email treeview image attached
treeview.JPG
treeview.JPG
The example solution shows how you can drag node's from one location to another in the same tree and even across tree views.
-saige-
-saige-
ASKER
In my case this part of code:
aNode.ImageKey = GetImageKey(child.FullName , child.Extension);
children = child.GetDirectories();
is not working
children.Length always 0
aNode.ImageKey = GetImageKey(child.FullName
children = child.GetDirectories();
is not working
children.Length always 0
Most likely there are not any child directories or you do not have access to them. You can change the path that the methods look in; in the PopulateTreeView methods, just change the line:
-saige-
DirectoryInfo info = new DirectoryInfo(@"put whatever path you want here");
-saige-
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Form1.cs -
Open in new window
Form1.Designer.cs -Open in new window
Which produces the following output --saige-