您的位置:首页 > 运维架构

.NET TreeView FAQ - Drag and Drop Right Click Menu

2009-01-19 15:28 597 查看
Working with a Windows Forms .NET TreeView control? This article contains code samples for the most common questions I've recevied. Things such as right click on nodes to show context menues, drag and drop nodes on the same TreeView or even another control, nudge nodes up and down in sibling order, and much more.

1. Right Click Show Context Menu:

Form Events

private void tvSample_ItemDrag(object sender,
System.Windows.Forms.ItemDragEventArgs e)
{
DoDragDrop(e.Item, DragDropEffects.Move);
}

private void tvSample_DragEnter(object sender,
System.Windows.Forms.DragEventArgs e)
{
this.DragEnter((TreeView)sender,e);
}

private void tvSample_DragOver(object sender,
System.Windows.Forms.DragEventArgs e)
{
this.DragOver((TreeView)sender,e);
}

private void tvSample_DragDrop(object sender,
System.Windows.Forms.DragEventArgs e)
{

bool dropOnNewControl = false;

try
{

this.DragDrop((TreeView)sender,e,ref dropOnNewControl);

if (dropOnNewControl)
{
// Perform action on old and
// new control
}

}
catch (Exception err) { Messagebox.Show(err.Message); }
}

Resuable methods

public void DragOver(TreeView tv,System.Windows.Forms.DragEventArgs e)
{

try
{

if (!e.Data.GetDataPresent(NodeObjectName, true)) { return; }

Point pt = tv.PointToClient(new Point(e.X,e.Y));

TreeNode tgtnode = tv.GetNodeAt(pt);

if (tv.SelectedNode != tgtnode)
{

tv.SelectedNode = tgtnode;

TreeNode drop = (TreeNode)e.Data.GetData(NodeObjectName);

while (tgtnode != null)
{

if (tgtnode == drop)
{
e.Effect = DragDropEffects.None;
return;
}

tgtnode = tgtnode.Parent;
}

}

e.Effect = DragDropEffects.Move;
}
catch (Exception) { throw; }
}

public void DragDrop(TreeView tv,
System.Windows.Forms.DragEventArgs e,
ref bool dropOnNewControl)
{

try
{

dropOnNewControl = false;

// Was the target control enabled to accept controls
// being dropped on it and was it successful?

if (!e.Data.GetDataPresent(NodeObjectName,true)) { return; }

// If not working solely with TreeViews,you'll
// to perform a .GetType() on .GetData prior to casting
// to a specific object.

TreeNode drop = (TreeNode)e.Data.GetData(NodeObjectName);

TreeNode tgtnode = tv.SelectedNode;

if (drop == drop.TreeView.Nodes[0])
{
// the drop node is the root of the tree.
// You may not want to permit this.
return;
}

if (drop == tgtnode)
{
// Jump out if dropped on self.
return;
}

if (tgtnode.TreeView == drop.TreeView)
{
// If same control, act accordingly

}
else
{
dropOnNewControl = true;
// This is a different control that the node
// was dropped on. You may now need to sync
up the old object's underlying data.
}

// This removes the TreeNodes from the TreeView.

drop.Remove();

if (tgtnode == null)
{
tv.Nodes.Add(drop);
}
else
{
tgtnode.Nodes.Add(drop);
}

drop.EnsureVisible();

tv.SelectedNode = drop;

}
catch (Exception) { throw; }
}

Please download the working sample. It is derived from an old article I did
on an alternative approach concerning data binding a DataSet to a TreeView.

Download.
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: