473,395 Members | 1,440 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

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

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
0 2392

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: Plumer | last post by:
Hello everyone, Yesterday I posted a message about implementing drag & drop in a TreeView control. I'm having real difficulty getting this to work -- the process seems to be incredibly...
8
by: WindAndWaves | last post by:
Hi everyone, Has anyone got any experience with drop and drag in Access? I would like to make a calendar style form where my users can drop and drag appointments.... I am using Access 2003...
3
by: Ajay Krishnan Thampi | last post by:
I have a slight problem implementing 'drag and drop' from a datagrid to a tree-view. I have pasted my code below. Someone please advice me on what to do...pretty blur right now. ==code== ...
3
by: petermichaux | last post by:
Hi, I am trying to put together the last major pieces of my project's puzzle. This is more website/client-side architecture than JavaScript syntax but I hope this is a good place to ask. I'm a...
1
by: SteveDouglas | last post by:
Hi all, I am currently writing an application in VB.NET that has a lot of controls (treeviews/listviews/labels and so forth) that represent "things" that need to be draggable from place to place,...
1
by: sundarkee | last post by:
how to drag and drop the treeview nodes from one to another and which event can be use to write that code? * I did populate the nodes from the tables using sql server * I want to drag and drop...
3
by: darksniperx | last post by:
I have searched the forum and couldnt find the post similar to mine so I created a new one. here what I would like to do: http://img3.freeimagehosting.net/uploads/39ed52c788.jpg on the right...
16
by: John | last post by:
I am looking for VBA code that will work with Access 2003 to enable dragging and dropping a file/folder name from Windows XP Explorer into an Access form's text box. This is a common functionality...
2
by: bob | last post by:
Hi all, I have a treeview that has drag drop. Works well enough but... If you drag out of bounds of the treeview the nodrop icon comes on. Fair enough. But when I move back inside the treeview...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.