Drag'n'Drop узлов TreeView
Думаю, для начала, будет ясно ;)
[highlight=csharp]
using System.Drawing;
using System.Windows.Forms;
namespace WindowsFormsApplication1 {
public partial class Form1 : Form {
public Form1() {
InitializeComponent();
}
private TreeNode GetHoveringNode(int screen_x, int screen_y) {
Point pt = treeView1.PointToClient(new Point(screen_x, screen_y));
TreeViewHitTestInfo hitInfo = treeView1.HitTest(pt);
return hitInfo.Node;
}
private void treeView1_ItemDrag(object sender, ItemDragEventArgs e) {
treeView1.DoDragDrop(e.Item, DragDropEffects.All);
}
private void treeView1_DragOver(object sender, DragEventArgs e) {
TreeNode hoveringNode = GetHoveringNode(e.X, e.Y);
TreeNode draggingNode = e.Data.GetData(typeof(TreeNode)) as TreeNode;
if (hoveringNode != null && hoveringNode != draggingNode) {
e.Effect = DragDropEffects.Move;
hoveringNode.TreeView.SelectedNode = hoveringNode;
} else {
e.Effect = DragDropEffects.None;
}
}
private void treeView1_DragDrop(object sender, DragEventArgs e) {
if (e.Effect == DragDropEffects.Move) {
TreeNode hoveringNode = GetHoveringNode(e.X, e.Y);
if (hoveringNode != null) {
TreeNode draggingNode = e.Data.GetData(typeof(TreeNode)) as TreeNode;
if (draggingNode != null) {
draggingNode.Remove();
hoveringNode.Nodes.Add(draggingNode);
}
}
}
}
}
}
[/highlight]Даа, у treeView1 свойство AllowDrop стоит в true.
Да тут, по-моему, изложено исчерпывающе... Muchas grac`ias! :)
this.treeView1.ItemDrag += new System.Windows.Forms.ItemDragEventHandler(this.treeView1_ItemDrag);
this.treeView1.DragOver += new System.Windows.Forms.DragEventHandler(this.treeView1_DragOver);
в InitializeComponent()
Друг мой, подобные вещи - очевидны.
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace WindowsFormsApplication1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private TreeNode GetHoveringNode(int screen_x, int screen_y) {
Point pt = treeView1.PointToClient(new Point(screen_x, screen_y));
TreeViewHitTestInfo hitInfo = treeView1.HitTest(pt);
return hitInfo.Node;
}
private void treeView1_ItemDrag(object sender, ItemDragEventArgs e) {
treeView1.DoDragDrop(e.Item, DragDropEffects.All);
}
private void treeView1_DragOver(object sender, DragEventArgs e) {
TreeNode hoveringNode = GetHoveringNode(e.X, e.Y);
TreeNode draggingNode = e.Data.GetData(typeof(TreeNode)) as TreeNode;
if (hoveringNode != null && hoveringNode != draggingNode && draggingNode != hoveringNode.Parent) {
e.Effect = DragDropEffects.Move;
hoveringNode.TreeView.SelectedNode = hoveringNode;
} else {
e.Effect = DragDropEffects.None;
}
}
private void treeView1_DragDrop(object sender, DragEventArgs e) {
if (e.Effect == DragDropEffects.Move) {
TreeNode hoveringNode = GetHoveringNode(e.X, e.Y);
if (hoveringNode != null) {
TreeNode draggingNode = e.Data.GetData(typeof(TreeNode)) as TreeNode;
if (draggingNode != null) {
if (draggingNode.Level == hoveringNode.Level)
{
if (draggingNode.Level != 0)
{
draggingNode.Remove();
hoveringNode.Parent.Nodes.Insert(hoveringNode.Index + 1, draggingNode);
}
if (draggingNode.Level == 0)
{
draggingNode.Remove();
treeView1.Nodes.Insert(hoveringNode.Index + 1, draggingNode);
}
}
if (draggingNode.Level > hoveringNode.Level)
{
draggingNode.Remove();
hoveringNode.Nodes.Insert(0, draggingNode);
}
}
}
}
}
}
}