using system;
using system.drawing;
using system.windows.forms;
public class form4 : form
{
private treeview treeview1;
public form4()
{
treeview1 = new treeview();
this.suspendlayout();
// initialize treeview1.
treeview1.allowdrop = true;
treeview1.dock = dockstyle.fill;
// add nodes to treeview1.
treenode node;
for (int x = 0; x < 3; ++x)
{
// add a root node to treeview1.
node = treeview1.nodes.add(string.format("node{0}", x*4));
for (int y = 1; y < 4; ++y)
{
// add a child node to the previously added node.
node = node.nodes.add(string.format("node{0}", x*4 + y));
}
}
// add event handlers for the required drag events.
treeview1.itemdrag += new itemdrageventhandler(treeview1_itemdrag);
treeview1.dragenter += new drageventhandler(treeview1_dragenter);
treeview1.dragover += new drageventhandler(treeview1_dragover);
treeview1.dragdrop += new drageventhandler(treeview1_dragdrop);
// initialize the form.
this.clientsize = new size(292, 273);
this.controls.add(treeview1);
this.resumelayout(false);
}
[stathread]
// static void main()
// {
// application.run(new form1());
// }
//
private void treeview1_itemdrag(object sender, itemdrageventargs e)
{
// move the dragged node when the left mouse button is used.
if (e.button == mousebuttons.left)
{
dodragdrop(e.item, dragdropeffects.move);
}
// copy the dragged node when the right mouse button is used.
else if (e.button == mousebuttons.right)
{
dodragdrop(e.item, dragdropeffects.copy);
}
}
// set the target drop effect to the effect
// specified in the itemdrag event handler.
private void treeview1_dragenter(object sender, drageventargs e)
{
e.effect = e.allowedeffect;
}
// select the node under the mouse pointer to indicate the
// expected drop location.
private void treeview1_dragover(object sender, drageventargs e)
{
// retrieve the client coordinates of the mouse position.
point targetpoint = treeview1.pointtoclient(new point(e.x, e.y));
// select the node at the mouse position.
treeview1.selectednode = treeview1.getnodeat(targetpoint);
}
private void treeview1_dragdrop(object sender, drageventargs e)
{
// retrieve the client coordinates of the drop location.
point targetpoint = treeview1.pointtoclient(new point(e.x, e.y));
// retrieve the node at the drop location.
treenode targetnode = treeview1.getnodeat(targetpoint);
// retrieve the node that was dragged.
treenode draggednode = (treenode)e.data.getdata(typeof(treenode));
// confirm that the node at the drop location is not
// the dragged node or a descendant of the dragged node.
if (!draggednode.equals(targetnode) && !containsnode(draggednode, targetnode))
{
// if it is a move operation, remove the node from its current
// location and add it to the node at the drop location.
if (e.effect == dragdropeffects.move)
{
draggednode.remove();
targetnode.nodes.add(draggednode);
}
// if it is a copy operation, clone the dragged node
// and add it to the node at the drop location.
else if (e.effect == dragdropeffects.copy)
{
targetnode.nodes.add((treenode)draggednode.clone());
}
// expand the node at the location
// to show the dropped node.
targetnode.expand();
}
}
// determine whether one node is a parent
// or ancestor of a second node.
private bool containsnode(treenode node1, treenode node2)
{
// check the parent node of the second node.
if (node2.parent == null) return false;
if (node2.parent.equals(node1)) return true;
// if the parent node is not null or equal to the first node,
// call the containsnode method recursively using the parent of
// the second node.
return containsnode(node1, node2.parent);
}
}
新闻热点
疑难解答