By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
429,401 Members | 820 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 429,401 IT Pros & Developers. It's quick & easy.

Drag and drop with user defined treenodes (long with example code)

P: n/a
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.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;

namespace WindowsApplication1
{

public class Form1 : System.Windows.Forms.Form
{
private System.Windows.Forms.TreeView treeView1;
private System.ComponentModel.Container components = null;
int count = 0;

public Form1()
{

InitializeComponent();
this.treeView1.ItemDrag += new
System.Windows.Forms.ItemDragEventHandler(this.tre eView_ItemDrag);
this.treeView1.DragEnter += new
System.Windows.Forms.DragEventHandler(this.treeVie w_DragEnter);
this.treeView1.DragDrop += new
System.Windows.Forms.DragEventHandler(this.treeVie w_DragDrop);

treeView1.Nodes.Add("Root");
for(int i = 0; i <= 5; i++)
{
treeView1.Nodes.Add(count.ToString());
count++;
}
treeView1.ExpandAll();
}
protected override void Dispose( bool disposing )
{
if( disposing )
{
if (components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
}

private void InitializeComponent()
{
this.treeView1 = new System.Windows.Forms.TreeView();
this.SuspendLayout();
//
// 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.SelectedImageIndex = -1;
this.treeView1.Size = new System.Drawing.Size(208, 224);
this.treeView1.TabIndex = 0;
this.treeView1.AfterSelect += new
System.Windows.Forms.TreeViewEventHandler(this.tre eView1_AfterSelect);
//
// Form1
//
this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
this.ClientSize = new System.Drawing.Size(292, 266);
this.Controls.Add(this.treeView1);
this.Name = "Form1";
this.Text = "Form1";
this.ResumeLayout(false);

}

[STAThread]
static void Main()
{
Application.Run(new Form1());
}

private void treeView1_AfterSelect(object sender,
System.Windows.Forms.TreeViewEventArgs e)
{
if (count <= 10)
{
treeView1.SelectedNode.Nodes.Add(count.ToString()) ;
treeView1.ExpandAll();
count++;
}
}

private void treeView_ItemDrag(object sender,
System.Windows.Forms.ItemDragEventArgs e)
{
DoDragDrop(e.Item, DragDropEffects.Move);
}

private void treeView_DragEnter(object sender,
System.Windows.Forms.DragEventArgs e)
{
e.Effect = DragDropEffects.Move;
}

private void treeView_DragDrop(object sender,
System.Windows.Forms.DragEventArgs e)
{
TreeNode NewNode;

if(e.Data.GetDataPresent("System.Windows.Forms.Tre eNode", false))
{
Point pt = ((TreeView)sender).PointToClient(new Point(e.X, e.Y));
TreeNode DestinationNode = ((TreeView)sender).GetNodeAt(pt);
NewNode = (TreeNode)e.Data.GetData("System.Windows.Forms.Tre eNode");
DestinationNode.Nodes.Add((TreeNode) NewNode.Clone());
DestinationNode.Expand();
//Remove Original Node
NewNode.Remove();
}
}
}
}
==============================================

Listing 2 (not working):
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;

namespace WindowsApplication1
{
public class Form1 : System.Windows.Forms.Form
{
private System.Windows.Forms.TreeView treeView1;
private System.ComponentModel.Container components = null;
int count = 0;

public Form1()
{
InitializeComponent();
this.treeView1.ItemDrag += new
System.Windows.Forms.ItemDragEventHandler(this.tre eView_ItemDrag);
this.treeView1.DragEnter += new
System.Windows.Forms.DragEventHandler(this.treeVie w_DragEnter);
this.treeView1.DragDrop += new
System.Windows.Forms.DragEventHandler(this.treeVie w_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.ExpandAll();
}

protected override void Dispose( bool disposing )
{
if( disposing )
{
if (components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
}

private void InitializeComponent()
{
this.treeView1 = new System.Windows.Forms.TreeView();
this.SuspendLayout();
//
// 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.SelectedImageIndex = -1;
this.treeView1.Size = new System.Drawing.Size(208, 224);
this.treeView1.TabIndex = 0;
this.treeView1.AfterSelect += new
System.Windows.Forms.TreeViewEventHandler(this.tre eView1_AfterSelect);
//
// Form1
//
this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
this.ClientSize = new System.Drawing.Size(292, 266);
this.Controls.Add(this.treeView1);
this.Name = "Form1";
this.Text = "Form1";
this.ResumeLayout(false);

}
[STAThread]
static void Main()
{
Application.Run(new Form1());
}

private void treeView1_AfterSelect(object sender,
System.Windows.Forms.TreeViewEventArgs e)
{
myTreeNode tn = new myTreeNode("test1", "test2");

if (count <= 10)
{
treeView1.SelectedNode.Nodes.Add(new myTreeNode( "in1 " +
count.ToString(), "in2 " + count.ToString()));
treeView1.ExpandAll();
count++;
}
else
{
myTreeNode myNode = (myTreeNode)e.Node;
MessageBox.Show("SelectNode selected is " + myNode.Text + " " +
myNode.text1);
}
}

private void treeView_ItemDrag(object sender,
System.Windows.Forms.ItemDragEventArgs e)
{
DoDragDrop(e.Item, DragDropEffects.Move);
}

private void treeView_DragEnter(object sender,
System.Windows.Forms.DragEventArgs e)
{
e.Effect = DragDropEffects.Move;
}

private void treeView_DragDrop(object sender,
System.Windows.Forms.DragEventArgs 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.GetDataPresent("myTreeNode", false))
//{
Point pt = ((TreeView)sender).PointToClient(new Point(e.X, e.Y));
myTreeNode DestinationNode =
(myTreeNode)((TreeView)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.Data.GetData("System.Windows.Forms.Tre eView");

// 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)NewNode;
//MessageBox.Show("Source Node selected is " + NewConvNode.Text + " " +
NewConvNode.text1);

DestinationNode.Nodes.Add((myTreeNode) NewNode.Clone());
DestinationNode.Expand();
//Remove Original Node
NewNode.Remove();
//}
}

public class myTreeNode: TreeNode
{
public string text1;

public myTreeNode(string in1, string in2)
{
text1 = in1;
this.Text = in2;
}
}

}
}

================================================== ===

Listing 3 (workaround):

using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;

namespace WindowsApplication1
{
public class Form1 : System.Windows.Forms.Form
{
private System.Windows.Forms.TreeView treeView1;
private System.ComponentModel.Container components = null;
int count = 0;
myTreeNode dragNode;

public Form1()
{
InitializeComponent();
this.treeView1.ItemDrag += new
System.Windows.Forms.ItemDragEventHandler(this.tre eView_ItemDrag);
this.treeView1.DragEnter += new
System.Windows.Forms.DragEventHandler(this.treeVie w_DragEnter);
this.treeView1.DragDrop += new
System.Windows.Forms.DragEventHandler(this.treeVie w_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.ExpandAll();
}

protected override void Dispose( bool disposing )
{
if( disposing )
{
if (components != null)
{
components.Dispose();
}
}
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 InitializeComponent()
{
this.treeView1 = new System.Windows.Forms.TreeView();
this.SuspendLayout();
//
// 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.SelectedImageIndex = -1;
this.treeView1.Size = new System.Drawing.Size(208, 224);
this.treeView1.TabIndex = 0;
this.treeView1.AfterSelect += new
System.Windows.Forms.TreeViewEventHandler(this.tre eView1_AfterSelect);
//
// Form1
//
this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
this.ClientSize = new System.Drawing.Size(292, 266);
this.Controls.Add(this.treeView1);
this.Name = "Form1";
this.Text = "Form1";
this.ResumeLayout(false);

}
#endregion
[STAThread]
static void Main()
{
Application.Run(new Form1());
}

private void treeView1_AfterSelect(object sender,
System.Windows.Forms.TreeViewEventArgs e)
{
myTreeNode tn = new myTreeNode("test1", "test2");

if (count <= 10)
{
treeView1.SelectedNode.Nodes.Add(new myTreeNode( "in1 " +
count.ToString(), "in2 " + count.ToString()));
treeView1.ExpandAll();
count++;
}
else
{
myTreeNode myNode = (myTreeNode)e.Node;
MessageBox.Show("SelectNode selected is " + myNode.Text + " " +
myNode.text1);
}
}

private void treeView_ItemDrag(object sender,
System.Windows.Forms.ItemDragEventArgs e)
{
dragNode = (myTreeNode)treeView1.SelectedNode;
DoDragDrop(e.Item, DragDropEffects.Move);

}

private void treeView_DragEnter(object sender,
System.Windows.Forms.DragEventArgs e)
{
e.Effect = DragDropEffects.Move;
}

private void treeView_DragDrop(object sender,
System.Windows.Forms.DragEventArgs e)
{
TreeNode NewNode;
myTreeNode NewConvNode;
//if(e.Data.GetDataPresent("myTreeNode", false))
//{
Point pt = ((TreeView)sender).PointToClient(new Point(e.X, e.Y));
myTreeNode DestinationNode =
(myTreeNode)((TreeView)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(string in1, string in2)
{
text1 = in1;
this.Text = in2;
}
}

}
}
Nov 16 '05 #1
Share this question for a faster answer!
Share on Google+

This discussion thread is closed

Replies have been disabled for this discussion.