473,799 Members | 3,229 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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.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;
}
}

}
}
Nov 16 '05 #1
0 2420

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

Similar topics

0
2279
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 delicately balanced and finding my way through it has descended into one of those awful situations which I refer to as guess & test. From the point at which DoDragDrop() function is called until some later
8
20846
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 and I have access to .net and the like, but I would really like to keep it simple.
3
10420
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== using System; using System.Drawing; using System.Collections; using System.ComponentModel;
3
5914
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 little stumped with which direction to take and advice would be greatly appreciated. I think this is a generally interesting problem but I haven't seen a post this long here before :S I have developed half of the admin interface for an e-commerce
1
2311
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, including between controls and windows. In the past when I've implemented Drag and Drop in other projects, I've always passed the item being dragged (for example the listviewitem or the treenode) to the DoDragDrop and then retrieved it in the...
1
979
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 the node to another Plz Help me thanks
3
2573
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 hand side a have a list of things that I might want to add to the page. They appear as a small rectangle with a name so that user would know what it is. When you will drag an drop it to the main body, the small rectangle will change into a big one...
16
11858
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 that most Windows programs have, so I'm suprised it's not easier to implement in Access/VBA. Through Google, I found two VB6 examples and one VBA example on the Access Web written by Dev Ashish. The VB6 examples used loops to keep checking...
2
3376
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 the nodrop icon stays on and essentially the action is cancelled when I release the mouse. I would like to somehow 'retrieve' the drag action when the mouse moves back inside the treeview to a legitimate drop point. Any thoughts on how to do this...
0
9541
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10251
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
10027
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9072
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7564
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6805
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5463
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
4139
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
2938
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.