C # implement the drag function in the winform Treeview Control (1)

Source: Internet
Author: User

Using system;
Using system. Collections. Generic;
Using system. componentmodel;
Using system. Data;
Using system. drawing;
Using system. text;
Using system. Windows. forms;

Namespace treeviewtest
{
Public partial class form1: Form
{
Public form1 ()
{
Initializecomponent ();
}

Private void form1_load (Object sender, eventargs E)
{
Treenode node;
Int X, Y;
For (x = 0; x <= 3; X ++)
{
Node = tvwtest. nodes. Add (string. Format ("root node {0}", x * 4 ));
For (y = 1; y <= 4; y ++)
{
Node = node. nodes. Add (string. Format ("-node", x * 4 + y ));
}
}
}

Private void tvwtest_itemdrag (Object sender, itemdrageventargs E)
{
If (E. Button = system. Windows. Forms. mousebuttons. Left)
{
Dodragdrop (E. Item, dragdropeffects. Move );
}
Else if (E. Button = system. Windows. Forms. mousebuttons. Right)
{
Dodragdrop (E. Item, dragdropeffects. copy );
}
}

Private void tvwtest_dragenter (Object sender, drageventargs E)
{
E. effect = E. allowedeffect;
}

Private void tvwtest_dragover (Object sender, drageventargs E)
{
Point targetpoint = (Treeview) (sender). pointtoclient (new point (E. X, E. y ));
(Treeview) (sender). selectednode = (Treeview) (sender). getnodeat (targetpoint );
}

Private void tvwtest_dragdrop (Object sender, drageventargs E)
{
Point targetpoint = (Treeview) (sender). pointtoclient (new point (E. X, E. y ));
Treenode targetnode = (Treeview) (sender). getnodeat (targetpoint );
Treenode draggednode = (treenode) (E. Data. getdata (typeof (treenode )));
If (! Draggednode. Equals (targetnode )&&! Containsnode (draggednode, targetnode ))
{
If (E. effect = dragdropeffects. Move)
{
Draggednode. Remove ();
Targetnode. nodes. Add (draggednode );

}
Else if (E. effect = dragdropeffects. copy)
{
Targetnode. nodes. Add (treenode) (draggednode. Clone ()));
}
Targetnode. Expand ();
}

}
Private bool containsnode (treenode nodel, treenode node2)
{
If (node2.parent = NULL)
{
Return false;
}
If (node2.parent. Equals (nodel ))
{
Return true;
}
Return containsnode (nodel, node2.parent );
}

}
}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.