Go4Expert

Go4Expert (http://www.go4expert.com/)
-   C# (http://www.go4expert.com/articles/c-sharp-tutorials/)
-   -   DragDrop in TreeView (http://www.go4expert.com/articles/dragdrop-in-treeview-t1308/)

shabbir 30Aug2006 18:59

DragDrop in TreeView
 
1 Attachment(s)
When I started on Drag and Drop in C# I could not find much on the net and so I have decided to tackle the problem here. First follow the steps to see the working sample.

1. Create a new C sharp windows application. Rename Form1 to a suitable name. I would refer it as TestForm
2. Add a treeview control with some dummy nodes randomly.
3. Extract DragDropTreeView.cs from the attached zipped file and copy DragDropTreeView.cs to the project directory.
4. Add DragDropTreeView.cs as an Existing item to the project
5. Go to TestForm.Designer.cs
6. Add
Code: CSHARP

using DragDropTreeSample;

at the top of the file.
7. Now find
Code: CSHARP

private System.Windows.Forms.TreeView treeView1;

and replace with
Code: CSHARP

private DragDropTreeView treeView1;

8. Go to InitializeComponent function and find
Code: CSHARP

this.treeView1 = new System.Windows.Forms.TreeView();

and replace with
Code: CSHARP

this.treeView1 = new DragDropTreeView();

Thats it. Simple isnt it. All done and now you will see that your tree view control has the drag and drop facility with the nodes you have added. Use Ctrl Key to copy a node or move the nodes in the tree using mouse drag.

Now the question comes is what is in DragDropTreeView.cs so that it behaves in such a fashion.

First thing that it does is sets the property AllowDrop of the DragDropTreeView to true so that we can handle the drag drop events for the TreeView control.

Next it overrides the 3 basic or you can say minimal drag drop event handler of TreeView. i.e. OnDragDrop, OnDragOver, OnItemDrag
Code: CSHARP

protected override void OnItemDrag(ItemDragEventArgs e)
{
    srcNodeForDrag = (TreeNode)e.Item;
    this.DoDragDrop(e.Item, DragDropEffects.All);
}

When any item is dragged we store the source node so that we can operate on the node and then start the DragDrop operation.
Code: CSHARP

protected override void OnDragOver(DragEventArgs e)
{
    if ((e.KeyState & 8) == 8 &&
        (e.AllowedEffect & DragDropEffects.Copy) == DragDropEffects.Copy)
    {
        // CTL KeyState for copy.
        e.Effect = DragDropEffects.Copy;
    }
    else if ((e.AllowedEffect & DragDropEffects.Move) == DragDropEffects.Move)
    {
        e.Effect = DragDropEffects.Move;
    }
}

When any item is drag over other item we check the keyboard if we CTRL is pressed we set the copy mode of the nodes or else its the move mode.
Code: CSHARP

protected override void OnDragDrop(DragEventArgs e)
{
    Point pt = PointToClient(new Point(e.X, e.Y));
    TreeNode DestinationNode = this.GetNodeAt(pt);

    // Ensure that the list item index is contained in the data.
    if (e.Data.GetDataPresent(typeof(TreeNode)))
    {
        // Perform drag-and-drop, depending upon the effect.
        if (e.Effect == DragDropEffects.Copy)
        {
            TreeNode tmpTN = (TreeNode)srcNodeForDrag.Clone();
            DestinationNode.Nodes.Add(tmpTN);
        }
        else if (e.Effect == DragDropEffects.Move)
        {
            TreeNode tmpTN = (TreeNode)srcNodeForDrag.Clone();
            DestinationNode.Nodes.Add(tmpTN);
            srcNodeForDrag.Remove();
        }
    }
}

Here we check the Data type of the drop element and depending on the operation in the drag over we copy/move the node at the destination node.

viswa56@gmail.com 29Nov2006 11:01

Re: DragDrop in TreeView
 
Dear Sir,

I have seen your code regarding drag and drop operations in a treeview.It is adding as a child at a dragged position.But i need to move nodes up and down with in a same level(drag and dropping of nodes at required position at a same level).

I need this code very urgent.I will be waiting for your reply......

Thanking You Sir........ :)

viswa56@gmail.com 29Nov2006 11:30

Re: DragDrop in TreeView
 
Dear Sir,

I have seen your code regarding drag and drop operations in a treeview.It is adding as a child at a dragged position.But i need to move nodes up and down with in a same level(drag and dropping of nodes at required position at a same level).

I need this code very urgent.I will be waiting for your reply......

Thanking You Sir........

shabbir 29Nov2006 12:12

Re: DragDrop in TreeView
 
Do you mean you need to add the node as its siblings to the node where the drop has taken place.

If thats the case modify OnDragDrop to find the parent node of the DestinationNode and add it to the child of the parent node.

viswa56@gmail.com 29Nov2006 13:28

Re: DragDrop in TreeView
 
Dear Sir,

I am very thankful for giving the suggestion.


My problem is to add the dragged node as a next node(immediate sibling),at dropping position which are at same level.

OR

I need code,such that when we a right-click on a node,the context menu has controls for moving the selected node up and down.


I will be waiting for your esteemed reply........


Thanking You Sir........

viswa56@gmail.com 29Nov2006 13:34

Re: DragDrop in TreeView
 
--------------------------------------------------------------------------------

Dear Sir,

I am very thankful for giving the suggestion.


My problem is to add the dragged node as a next node(immediate sibling),at dropping position which are at same level with in a single tree.

OR

I need code,such that when we a right-click on a node,the context menu has controls for moving the selected node up and down with in a single tree.


I will be waiting for your esteemed reply........


Thanking You Sir........

shabbir 29Nov2006 14:05

Re: DragDrop in TreeView
 
Quote:

Originally Posted by viswa56@gmail.com
My problem is to add the dragged node as a next node(immediate sibling),at dropping position which are at same level with in a single tree.

After you get the parent where you would like to add node you can do that easily. Just try it out.
Quote:

Originally Posted by viswa56@gmail.com
I need code,such that when we a right-click on a node,the context menu has controls for moving the selected node up and down with in a single tree.

The above case also applies to the nodes to move using the menu or something similar.

viswa56@gmail.com 29Nov2006 14:23

Re: DragDrop in TreeView
 
Dear Sir,

With help of your advice ,I can move the node.But it is adding as a last child to the parent. I want to place the dragged node at dropping position itself.


I want help on this context.


Please kindly reply me.

I will be waiting for your reply.......


Thanking You Sir.........

viswa56@gmail.com 29Nov2006 14:31

Re: DragDrop in TreeView
 
--------------------------------------------------------------------------------

Dear Sir,

With help of your advice ,I can move the node.But it is adding as a last child to the parent. I want to place the dragged node at dropping position itself.


I want help on this context.


Please kindly reply me.

I will be waiting for your reply.......


Thanking You Sir.........

shabbir 29Nov2006 14:52

Re: DragDrop in TreeView
 
You need to move the node to the needed position as Add does not have any overloaded which can add a specified position of the parent node.

sivaprakash 29Nov2006 15:09

Re: DragDrop in TreeView
 
Is it possible to show the node in a color currently the mouse over on it

shabbir 29Nov2006 15:34

Re: DragDrop in TreeView
 
Quote:

Originally Posted by sivaprakash
Is it possible to show the node in a color currently the mouse over on it

Its definitely possible. Override the paint and paint the node using different color

viswa56@gmail.com 29Nov2006 16:05

Re: DragDrop in TreeView
 
Dear Sir,

With your help,I can able to place the nodes at required positions.But I can't save the file.How can I save it?If I saves it 80% of my work will be complete.

Please kindly help me on this context.....

I will be waiting for your reply...

Thanking You Siir.........

shabbir 29Nov2006 16:16

Re: DragDrop in TreeView
 
Which file you are trying to save??

viswa56@gmail.com 29Nov2006 16:55

Re: DragDrop in TreeView
 
Dear Sir,

Actually I am working on XML File.I am representing an XML File in a treeview.After implementing required operatons(moving of nodes) ,eventhough I am giving a statement to save the file,the changes are not appearing in the original file.

And one more thing is ,I am not able to scroll treeview while performing Drag And Drop Operations.


KIndly help me on these two things.......

Waiting for your reply........


Thanking You Sir...........

shabbir 29Nov2006 17:00

Re: DragDrop in TreeView
 
You need to provide some more details about the xml file so that we can understand how you are reading/writing and why the changes are not happening.

Regarding scrolling while you are dragging it should work with the mouse and not with the mouse scroll as with any other normal window/controls.

viswa56@gmail.com 29Nov2006 17:13

Re: DragDrop in TreeView
 
Dear Sir,

I am moving moving the nodes of an xml file in a treeview to the desired positions.I can move nodes in a treeview to a desired position.These changes(new positions for nodes) can be seen in a treeview.But even though I am saving the file after moving the nodes,I am not able to see those changes,when we load the file next time.


I am not understanding regarding of scrolling.


Waiting for your reply..................

Thanking You Sir............

shabbir 29Nov2006 17:21

Re: DragDrop in TreeView
 
Regarding scrolling it should be behaving as any other normal window tree view does as we dont handle or interrupt anything in the tree view.

viswa56@gmail.com 29Nov2006 17:25

Re: DragDrop in TreeView
 
Dear Sir,

I am moving moving the nodes of an xml file in a treeview to the desired positions.I can move nodes in a treeview to a desired position.These changes(new positions for nodes) can be seen in a treeview.But even though I am saving the file after moving the nodes,I am not able to see those changes,when we load the file next time.





Waiting for your reply..................

Thanking You Sir............

viswa56@gmail.com 30Nov2006 10:05

Re: DragDrop in TreeView
 
Dear Sir,

Can I implement autoscrolling in the treeview which is presented by you?If so how can I?

shabbir 30Nov2006 10:28

Re: DragDrop in TreeView
 
Quote:

Originally Posted by viswa56@gmail.com
Dear Sir,

Can I implement autoscrolling in the treeview which is presented by you?If so how can I?

As far as I know it should be there unless you have stopped it.

viswa56@gmail.com 30Nov2006 10:35

Re: DragDrop in TreeView
 
Dear Sir,

I want attach your code in my class itself,where I have performed some other functions.To do so,what are the things to be done?

shabbir 30Nov2006 10:39

Re: DragDrop in TreeView
 
Quote:

Originally Posted by viswa56@gmail.com
Dear Sir,

I want attach your code in my class itself,where I have performed some other functions.To do so,what are the things to be done?

Thats specified in the first post.

viswa56@gmail.com 30Nov2006 10:52

Re: DragDrop in TreeView
 
Dear Sir,

Thanks for the help...........

shabbir 30Nov2006 11:02

Re: DragDrop in TreeView
 
Quote:

Originally Posted by viswa56@gmail.com
Thanks for the help...........

My pleasure.

viswa56@gmail.com 30Nov2006 11:20

Re: DragDrop in TreeView
 
Dear Sir,

I have one more problem.When a mouse moves on node ,how can I differentiate that node from other nodes?If so,how can I do it?

shabbir 30Nov2006 11:44

Re: DragDrop in TreeView
 
Change the font of the node to something else.

viswa56@gmail.com 30Nov2006 12:00

Re: DragDrop in TreeView
 
Deat Sir,

Whether we need to change the font of nodes in Mouse_Move Event?

shabbir 30Nov2006 14:56

Re: DragDrop in TreeView
 
It depends on your requirement.

viswa56@gmail.com 30Nov2006 15:10

Re: DragDrop in TreeView
 
Dear Sir,
Actually i want to show a node in color whenever a mouse moves over it.How can I do that?

shabbir 30Nov2006 16:33

Re: DragDrop in TreeView
 
change the font of nodes in Mouse_Move Event

viswa56@gmail.com 30Nov2006 17:21

Re: DragDrop in TreeView
 
Dear Sir,

Thanks for the help...............

viswa56@gmail.com 30Nov2006 17:26

Re: DragDrop in TreeView
 
Dear Sir,

When I write code in mouse move event,it showing error .

here is the error

Object reference not set to an instance of an object.



Use the new keyword to create an object instance


What to do?

shabbir 30Nov2006 18:48

Re: DragDrop in TreeView
 
Please post your specific query as new thread as its not related to drag drop in tree view.

tuhiniu06 4Jun2014 18:54

Re: DragDrop in TreeView
 
Dear sir,
I have a problem,i.e,when i create a setup file it only works for my pc or those pc where installed dotnet 3.5 . How can i include .net file in my c# setup project.


All times are GMT +5.5. The time now is 01:31.