.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.
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.
相关文章推荐
- .NET中的Drag and Drop操作(三)
- .NET中的Drag and Drop操作(一)
- .NET中的Drag and Drop操作(三)
- .NET中的Drag and Drop操作(二)
- [Selenium]How to click on a hidden link ,move to the drop down menu and click submenu
- .NET中的Drag and Drop操作(二)
- .NET中的Drag and Drop操作(一)
- Android 用户界面---拖放(Drag and Drop)(三)
- Tower Tips and Tricks (part 1): drag and drop
- HTML 5 drag and drop 简介
- Drag and Drop(OLE)-OLE的拖放功能
- Remove “Shared Folder Synchronization” from Right Click Context Menu
- 利用Dojo实现拖动(Drag and Drop)效果
- js拖拽和放置(javascript drag and drop)
- Right-click is slow or weird behavior caused by context menu handlers-右键慢
- How to Drag and Drop in JavaScript
- 4.2. Drag and Drop
- [.Net码农]DragDrop 注册失败的问题
- [Ext JS 4] 拖放[drag and drop]
- Android:Drag and Drop的应用