472,800 Members | 1,185 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,800 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 2334

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...
3
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 2 August 2023 starting at 18:00 UK time (6PM UTC+1) and finishing at about 19:15 (7.15PM) The start time is equivalent to 19:00 (7PM) in Central...
0
by: kcodez | last post by:
As a H5 game development enthusiast, I recently wrote a very interesting little game - Toy Claw ((http://claw.kjeek.com/))。Here I will summarize and share the development experience here, and hope it...
0
by: Taofi | last post by:
I try to insert a new record but the error message says the number of query names and destination fields are not the same This are my field names ID, Budgeted, Actual, Status and Differences ...
14
DJRhino1175
by: DJRhino1175 | last post by:
When I run this code I get an error, its Run-time error# 424 Object required...This is my first attempt at doing something like this. I test the entire code and it worked until I added this - If...
0
by: Rina0 | last post by:
I am looking for a Python code to find the longest common subsequence of two strings. I found this blog post that describes the length of longest common subsequence problem and provides a solution in...
5
by: DJRhino | last post by:
Private Sub CboDrawingID_BeforeUpdate(Cancel As Integer) If = 310029923 Or 310030138 Or 310030152 Or 310030346 Or 310030348 Or _ 310030356 Or 310030359 Or 310030362 Or...
0
by: lllomh | last post by:
Define the method first this.state = { buttonBackgroundColor: 'green', isBlinking: false, // A new status is added to identify whether the button is blinking or not } autoStart=()=>{
0
by: lllomh | last post by:
How does React native implement an English player?
0
by: Mushico | last post by:
How to calculate date of retirement from date of birth

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.