Hi all,
I have read a lot of postings and web pages about drag and drop
and treeviews, but could not find an answer to my problem. Sorry, if I
missed something.
I am trying to drag and drop treenodes defined by myself and
don't understand, how to get the nodes data in the dragdrop event.
I'm not very experienced in .NET and for sure there is a simple solution.
I've written a short sample program, based on well known examples,
so explanation is easier. If I made no copy error, interested readers
can copy and run the samples in a few seconds.
The first listing is the program, with normal treenodes. This version works
fine.
At first a few nodes are attached to the tree automatically. Next you can
select some nodes and childs are attached to the selected nodes. Now we are
ready to drag and drop.
Listing 2 is nearly the same, but now the nodes are defined by myself. This
version is not working. Seems to me that I don't understand, how to get
the data in the dragdrop event or it simply doesn't work this way. More
comments are in listing 2.
Listing 3 is a workaround. The last selected node is saved and in the
dragdrop event stored to the new node and deleted at the old node. But I
have to click twice on the node to move. First to select and than to move.
Any hints or pointers to working examples are welcome. Thanks for your
efforts.
Norbert
=============== =============== ==========
Listing 1 (working):
using System;
using System.Drawing;
using System.Collecti ons;
using System.Componen tModel;
using System.Windows. Forms;
using System.Data;
namespace WindowsApplicat ion1
{
public class Form1 : System.Windows. Forms.Form
{
private System.Windows. Forms.TreeView treeView1;
private System.Componen tModel.Containe r components = null;
int count = 0;
public Form1()
{
InitializeCompo nent();
this.treeView1. ItemDrag += new
System.Windows. Forms.ItemDragE ventHandler(thi s.treeView_Item Drag);
this.treeView1. DragEnter += new
System.Windows. Forms.DragEvent Handler(this.tr eeView_DragEnte r);
this.treeView1. DragDrop += new
System.Windows. Forms.DragEvent Handler(this.tr eeView_DragDrop );
treeView1.Nodes .Add("Root");
for(int i = 0; i <= 5; i++)
{
treeView1.Nodes .Add(count.ToSt ring());
count++;
}
treeView1.Expan dAll();
}
protected override void Dispose( bool disposing )
{
if( disposing )
{
if (components != null)
{
components.Disp ose();
}
}
base.Dispose( disposing );
}
private void InitializeCompo nent()
{
this.treeView1 = new System.Windows. Forms.TreeView( );
this.SuspendLay out();
//
// treeView1
//
this.treeView1. AllowDrop = true;
this.treeView1. HideSelection = false;
this.treeView1. ImageIndex = -1;
this.treeView1. Location = new System.Drawing. Point(40, 16);
this.treeView1. Name = "treeView1" ;
this.treeView1. SelectedImageIn dex = -1;
this.treeView1. Size = new System.Drawing. Size(208, 224);
this.treeView1. TabIndex = 0;
this.treeView1. AfterSelect += new
System.Windows. Forms.TreeViewE ventHandler(thi s.treeView1_Aft erSelect);
//
// Form1
//
this.AutoScaleB aseSize = new System.Drawing. Size(5, 13);
this.ClientSize = new System.Drawing. Size(292, 266);
this.Controls.A dd(this.treeVie w1);
this.Name = "Form1";
this.Text = "Form1";
this.ResumeLayo ut(false);
}
[STAThread]
static void Main()
{
Application.Run (new Form1());
}
private void treeView1_After Select(object sender,
System.Windows. Forms.TreeViewE ventArgs e)
{
if (count <= 10)
{
treeView1.Selec tedNode.Nodes.A dd(count.ToStri ng());
treeView1.Expan dAll();
count++;
}
}
private void treeView_ItemDr ag(object sender,
System.Windows. Forms.ItemDragE ventArgs e)
{
DoDragDrop(e.It em, DragDropEffects .Move);
}
private void treeView_DragEn ter(object sender,
System.Windows. Forms.DragEvent Args e)
{
e.Effect = DragDropEffects .Move;
}
private void treeView_DragDr op(object sender,
System.Windows. Forms.DragEvent Args e)
{
TreeNode NewNode;
if(e.Data.GetDa taPresent("Syst em.Windows.Form s.TreeNode", false))
{
Point pt = ((TreeView)send er).PointToClie nt(new Point(e.X, e.Y));
TreeNode DestinationNode = ((TreeView)send er).GetNodeAt(p t);
NewNode = (TreeNode)e.Dat a.GetData("Syst em.Windows.Form s.TreeNode");
DestinationNode .Nodes.Add((Tre eNode) NewNode.Clone() );
DestinationNode .Expand();
//Remove Original Node
NewNode.Remove( );
}
}
}
}
=============== =============== =============== =
Listing 2 (not working):
using System;
using System.Drawing;
using System.Collecti ons;
using System.Componen tModel;
using System.Windows. Forms;
using System.Data;
namespace WindowsApplicat ion1
{
public class Form1 : System.Windows. Forms.Form
{
private System.Windows. Forms.TreeView treeView1;
private System.Componen tModel.Containe r components = null;
int count = 0;
public Form1()
{
InitializeCompo nent();
this.treeView1. ItemDrag += new
System.Windows. Forms.ItemDragE ventHandler(thi s.treeView_Item Drag);
this.treeView1. DragEnter += new
System.Windows. Forms.DragEvent Handler(this.tr eeView_DragEnte r);
this.treeView1. DragDrop += new
System.Windows. Forms.DragEvent Handler(this.tr eeView_DragDrop );
treeView1.Nodes .Add("Root");
for(int i = 0; i <= 5; i++)
{
treeView1.Nodes .Add(new myTreeNode( "in1 " + count.ToString( ), "in2 "
+ count.ToString( )));
count++;
}
treeView1.Expan dAll();
}
protected override void Dispose( bool disposing )
{
if( disposing )
{
if (components != null)
{
components.Disp ose();
}
}
base.Dispose( disposing );
}
private void InitializeCompo nent()
{
this.treeView1 = new System.Windows. Forms.TreeView( );
this.SuspendLay out();
//
// treeView1
//
this.treeView1. AllowDrop = true;
this.treeView1. HideSelection = false;
this.treeView1. ImageIndex = -1;
this.treeView1. Location = new System.Drawing. Point(40, 16);
this.treeView1. Name = "treeView1" ;
this.treeView1. SelectedImageIn dex = -1;
this.treeView1. Size = new System.Drawing. Size(208, 224);
this.treeView1. TabIndex = 0;
this.treeView1. AfterSelect += new
System.Windows. Forms.TreeViewE ventHandler(thi s.treeView1_Aft erSelect);
//
// Form1
//
this.AutoScaleB aseSize = new System.Drawing. Size(5, 13);
this.ClientSize = new System.Drawing. Size(292, 266);
this.Controls.A dd(this.treeVie w1);
this.Name = "Form1";
this.Text = "Form1";
this.ResumeLayo ut(false);
}
[STAThread]
static void Main()
{
Application.Run (new Form1());
}
private void treeView1_After Select(object sender,
System.Windows. Forms.TreeViewE ventArgs e)
{
myTreeNode tn = new myTreeNode("tes t1", "test2");
if (count <= 10)
{
treeView1.Selec tedNode.Nodes.A dd(new myTreeNode( "in1 " +
count.ToString( ), "in2 " + count.ToString( )));
treeView1.Expan dAll();
count++;
}
else
{
myTreeNode myNode = (myTreeNode)e.N ode;
MessageBox.Show ("SelectNode selected is " + myNode.Text + " " +
myNode.text1);
}
}
private void treeView_ItemDr ag(object sender,
System.Windows. Forms.ItemDragE ventArgs e)
{
DoDragDrop(e.It em, DragDropEffects .Move);
}
private void treeView_DragEn ter(object sender,
System.Windows. Forms.DragEvent Args e)
{
e.Effect = DragDropEffects .Move;
}
private void treeView_DragDr op(object sender,
System.Windows. Forms.DragEvent Args e)
{
TreeNode NewNode;
myTreeNode NewConvNode;
// the following if statemeent always returns false.
// I've made some tests with System.Windows. Forms.TreeView instead of
myTreeNode
// and so on, but can't get it working.
//if(e.Data.GetDa taPresent("myTr eeNode", false))
//{
Point pt = ((TreeView)send er).PointToClie nt(new Point(e.X, e.Y));
myTreeNode DestinationNode =
(myTreeNode)((T reeView)sender) .GetNodeAt(pt);
// destination node is correct as is shown by the messagebox
MessageBox.Show ("Target Node selected is " + DestinationNode .Text + " "
+ DestinationNode .text1);
// here is the problem, this statement down't answer. Even with
myTreeNode,
// when NewNode is declared as myTreeNode
NewNode = (TreeNode)e.Dat a.GetData("Syst em.Windows.Form s.TreeView");
// this messagebox doesn't pop up. I've checked different version of
these
// statement, but it doesn't work
MessageBox.Show (NewNode.Text);
//NewConvNode = (myTreeNode)New Node;
//MessageBox.Show ("Source Node selected is " + NewConvNode.Tex t + " " +
NewConvNode.tex t1);
DestinationNode .Nodes.Add((myT reeNode) NewNode.Clone() );
DestinationNode .Expand();
//Remove Original Node
NewNode.Remove( );
//}
}
public class myTreeNode: TreeNode
{
public string text1;
public myTreeNode(stri ng in1, string in2)
{
text1 = in1;
this.Text = in2;
}
}
}
}
=============== =============== =============== ========
Listing 3 (workaround):
using System;
using System.Drawing;
using System.Collecti ons;
using System.Componen tModel;
using System.Windows. Forms;
using System.Data;
namespace WindowsApplicat ion1
{
public class Form1 : System.Windows. Forms.Form
{
private System.Windows. Forms.TreeView treeView1;
private System.Componen tModel.Containe r components = null;
int count = 0;
myTreeNode dragNode;
public Form1()
{
InitializeCompo nent();
this.treeView1. ItemDrag += new
System.Windows. Forms.ItemDragE ventHandler(thi s.treeView_Item Drag);
this.treeView1. DragEnter += new
System.Windows. Forms.DragEvent Handler(this.tr eeView_DragEnte r);
this.treeView1. DragDrop += new
System.Windows. Forms.DragEvent Handler(this.tr eeView_DragDrop );
treeView1.Nodes .Add("Root");
for(int i = 0; i <= 5; i++)
{
treeView1.Nodes .Add(new myTreeNode( "in1 " + count.ToString( ), "in2 "
+ count.ToString( )));
count++;
}
treeView1.Expan dAll();
}
protected override void Dispose( bool disposing )
{
if( disposing )
{
if (components != null)
{
components.Disp ose();
}
}
base.Dispose( disposing );
}
#region Vom Windows Form-Designer generierter Code
/// <summary>
/// Erforderliche Methode für die Designerunterst ützung.
/// Der Inhalt der Methode darf nicht mit dem Code-Editor geändert
werden.
/// </summary>
private void InitializeCompo nent()
{
this.treeView1 = new System.Windows. Forms.TreeView( );
this.SuspendLay out();
//
// treeView1
//
this.treeView1. AllowDrop = true;
this.treeView1. HideSelection = false;
this.treeView1. ImageIndex = -1;
this.treeView1. Location = new System.Drawing. Point(40, 16);
this.treeView1. Name = "treeView1" ;
this.treeView1. SelectedImageIn dex = -1;
this.treeView1. Size = new System.Drawing. Size(208, 224);
this.treeView1. TabIndex = 0;
this.treeView1. AfterSelect += new
System.Windows. Forms.TreeViewE ventHandler(thi s.treeView1_Aft erSelect);
//
// Form1
//
this.AutoScaleB aseSize = new System.Drawing. Size(5, 13);
this.ClientSize = new System.Drawing. Size(292, 266);
this.Controls.A dd(this.treeVie w1);
this.Name = "Form1";
this.Text = "Form1";
this.ResumeLayo ut(false);
}
#endregion
[STAThread]
static void Main()
{
Application.Run (new Form1());
}
private void treeView1_After Select(object sender,
System.Windows. Forms.TreeViewE ventArgs e)
{
myTreeNode tn = new myTreeNode("tes t1", "test2");
if (count <= 10)
{
treeView1.Selec tedNode.Nodes.A dd(new myTreeNode( "in1 " +
count.ToString( ), "in2 " + count.ToString( )));
treeView1.Expan dAll();
count++;
}
else
{
myTreeNode myNode = (myTreeNode)e.N ode;
MessageBox.Show ("SelectNode selected is " + myNode.Text + " " +
myNode.text1);
}
}
private void treeView_ItemDr ag(object sender,
System.Windows. Forms.ItemDragE ventArgs e)
{
dragNode = (myTreeNode)tre eView1.Selected Node;
DoDragDrop(e.It em, DragDropEffects .Move);
}
private void treeView_DragEn ter(object sender,
System.Windows. Forms.DragEvent Args e)
{
e.Effect = DragDropEffects .Move;
}
private void treeView_DragDr op(object sender,
System.Windows. Forms.DragEvent Args e)
{
TreeNode NewNode;
myTreeNode NewConvNode;
//if(e.Data.GetDa taPresent("myTr eeNode", false))
//{
Point pt = ((TreeView)send er).PointToClie nt(new Point(e.X, e.Y));
myTreeNode DestinationNode =
(myTreeNode)((T reeView)sender) .GetNodeAt(pt);
MessageBox.Show ("Target Node selected is " + DestinationNode .Text + " "
+ DestinationNode .text1);
MessageBox.Show ("Source Node selected is " + dragNode.Text + " " +
dragNode.text1) ;
DestinationNode .Nodes.Add(new myTreeNode( dragNode.text1,
dragNode.Text)) ;
DestinationNode .Expand();
dragNode.Remove ();
//}
}
public class myTreeNode: TreeNode
{
public string text1;
public myTreeNode(stri ng in1, string in2)
{
text1 = in1;
this.Text = in2;
}
}
}
}