C#实现Treeview中节点的拖拉
1
using System;
2
using System.Drawing;
3
using System.Windows.Forms;
4
5
public class Form4 : Form
6
{
7
private TreeView treeView1;
8
9
public Form4()
10
{
11
treeView1 = new TreeView();
12
13
this.SuspendLayout();
14
15
// Initialize treeView1.
16
treeView1.AllowDrop = true;
17
treeView1.Dock = DockStyle.Fill;
18
19
// Add nodes to treeView1.
20
TreeNode node;
21
for (int x = 0; x < 3; ++x)
22
{
23
// Add a root node to treeView1.
24
node = treeView1.Nodes.Add(String.Format("Node{0}", x*4));
25
for (int y = 1; y < 4; ++y)
26
{
27
// Add a child node to the previously added node.
28
node = node.Nodes.Add(String.Format("Node{0}", x*4 + y));
29
}
30
}
31
32
// Add event handlers for the required drag events.
33
treeView1.ItemDrag += new ItemDragEventHandler(treeView1_ItemDrag);
34
treeView1.DragEnter += new DragEventHandler(treeView1_DragEnter);
35
treeView1.DragOver += new DragEventHandler(treeView1_DragOver);
36
treeView1.DragDrop += new DragEventHandler(treeView1_DragDrop);
37
38
// Initialize the form.
39
this.ClientSize = new Size(292, 273);
40
this.Controls.Add(treeView1);
41
42
this.ResumeLayout(false);
43
}
44
45
[STAThread]
46
// static void Main()
47
// {
48
// Application.Run(new Form1());
49
// }
50
//
51
private void treeView1_ItemDrag(object sender, ItemDragEventArgs e)
52
{
53
// Move the dragged node when the left mouse button is used.
54
if (e.Button == MouseButtons.Left)
55
{
56
DoDragDrop(e.Item, DragDropEffects.Move);
57
}
58
59
// Copy the dragged node when the right mouse button is used.
60
else if (e.Button == MouseButtons.Right)
61
{
62
DoDragDrop(e.Item, DragDropEffects.Copy);
63
}
64
}
65
66
// Set the target drop effect to the effect
67
// specified in the ItemDrag event handler.
68
private void treeView1_DragEnter(object sender, DragEventArgs e)
69
{
70
e.Effect = e.AllowedEffect;
71
}
72
73
// Select the node under the mouse pointer to indicate the
74
// expected drop location.
75
private void treeView1_DragOver(object sender, DragEventArgs e)
76
{
77
// Retrieve the client coordinates of the mouse position.
78
Point targetPoint = treeView1.PointToClient(new Point(e.X, e.Y));
79
80
// Select the node at the mouse position.
81
treeView1.SelectedNode = treeView1.GetNodeAt(targetPoint);
82
}
83
84
private void treeView1_DragDrop(object sender, DragEventArgs e)
85
{
86
// Retrieve the client coordinates of the drop location.
87
Point targetPoint = treeView1.PointToClient(new Point(e.X, e.Y));
88
89
// Retrieve the node at the drop location.
90
TreeNode targetNode = treeView1.GetNodeAt(targetPoint);
91
92
// Retrieve the node that was dragged.
93
TreeNode draggedNode = (TreeNode)e.Data.GetData(typeof(TreeNode));
94
95
// Confirm that the node at the drop location is not
96
// the dragged node or a descendant of the dragged node.
97
if (!draggedNode.Equals(targetNode) && !ContainsNode(draggedNode, targetNode))
98
{
99
// If it is a move operation, remove the node from its current
100
// location and add it to the node at the drop location.
101
if (e.Effect == DragDropEffects.Move)
102
{
103
draggedNode.Remove();
104
targetNode.Nodes.Add(draggedNode);
105
}
106
107
// If it is a copy operation, clone the dragged node
108
// and add it to the node at the drop location.
109
else if (e.Effect == DragDropEffects.Copy)
110
{
111
targetNode.Nodes.Add((TreeNode)draggedNode.Clone());
112
}
113
114
// Expand the node at the location
115
// to show the dropped node.
116
targetNode.Expand();
117
}
118
}
119
120
// Determine whether one node is a parent
121
// or ancestor of a second node.
122
private bool ContainsNode(TreeNode node1, TreeNode node2)
123
{
124
// Check the parent node of the second node.
125
if (node2.Parent == null) return false;
126
if (node2.Parent.Equals(node1)) return true;
127
128
// If the parent node is not null or equal to the first node,
129
// call the ContainsNode method recursively using the parent of
130
// the second node.
131
return ContainsNode(node1, node2.Parent);
132
}
133
134
}
135
136
137
using System;2
using System.Drawing;3
using System.Windows.Forms;4

5
public class Form4 : Form6
{7
private TreeView treeView1;8

9
public Form4()10
{11
treeView1 = new TreeView();12

13
this.SuspendLayout();14

15
// Initialize treeView1.16
treeView1.AllowDrop = true;17
treeView1.Dock = DockStyle.Fill;18

19
// Add nodes to treeView1.20
TreeNode node;21
for (int x = 0; x < 3; ++x)22
{23
// Add a root node to treeView1.24
node = treeView1.Nodes.Add(String.Format("Node{0}", x*4));25
for (int y = 1; y < 4; ++y)26
{27
// Add a child node to the previously added node.28
node = node.Nodes.Add(String.Format("Node{0}", x*4 + y));29
}30
}31

32
// Add event handlers for the required drag events.33
treeView1.ItemDrag += new ItemDragEventHandler(treeView1_ItemDrag);34
treeView1.DragEnter += new DragEventHandler(treeView1_DragEnter);35
treeView1.DragOver += new DragEventHandler(treeView1_DragOver);36
treeView1.DragDrop += new DragEventHandler(treeView1_DragDrop);37

38
// Initialize the form.39
this.ClientSize = new Size(292, 273);40
this.Controls.Add(treeView1);41

42
this.ResumeLayout(false);43
}44

45
[STAThread]46
// static void Main() 47
// {48
// Application.Run(new Form1());49
// }50
//51
private void treeView1_ItemDrag(object sender, ItemDragEventArgs e)52
{53
// Move the dragged node when the left mouse button is used.54
if (e.Button == MouseButtons.Left)55
{56
DoDragDrop(e.Item, DragDropEffects.Move);57
}58

59
// Copy the dragged node when the right mouse button is used.60
else if (e.Button == MouseButtons.Right)61
{62
DoDragDrop(e.Item, DragDropEffects.Copy);63
}64
}65

66
// Set the target drop effect to the effect 67
// specified in the ItemDrag event handler.68
private void treeView1_DragEnter(object sender, DragEventArgs e)69
{70
e.Effect = e.AllowedEffect;71
}72

73
// Select the node under the mouse pointer to indicate the 74
// expected drop location.75
private void treeView1_DragOver(object sender, DragEventArgs e)76
{77
// Retrieve the client coordinates of the mouse position.78
Point targetPoint = treeView1.PointToClient(new Point(e.X, e.Y));79

80
// Select the node at the mouse position.81
treeView1.SelectedNode = treeView1.GetNodeAt(targetPoint);82
}83

84
private void treeView1_DragDrop(object sender, DragEventArgs e)85
{86
// Retrieve the client coordinates of the drop location.87
Point targetPoint = treeView1.PointToClient(new Point(e.X, e.Y));88

89
// Retrieve the node at the drop location.90
TreeNode targetNode = treeView1.GetNodeAt(targetPoint);91

92
// Retrieve the node that was dragged.93
TreeNode draggedNode = (TreeNode)e.Data.GetData(typeof(TreeNode));94

95
// Confirm that the node at the drop location is not 96
// the dragged node or a descendant of the dragged node.97
if (!draggedNode.Equals(targetNode) && !ContainsNode(draggedNode, targetNode))98
{99
// If it is a move operation, remove the node from its current 100
// location and add it to the node at the drop location.101
if (e.Effect == DragDropEffects.Move)102
{103
draggedNode.Remove();104
targetNode.Nodes.Add(draggedNode);105
}106

107
// If it is a copy operation, clone the dragged node 108
// and add it to the node at the drop location.109
else if (e.Effect == DragDropEffects.Copy)110
{111
targetNode.Nodes.Add((TreeNode)draggedNode.Clone());112
}113

114
// Expand the node at the location 115
// to show the dropped node.116
targetNode.Expand();117
}118
}119

120
// Determine whether one node is a parent 121
// or ancestor of a second node.122
private bool ContainsNode(TreeNode node1, TreeNode node2)123
{124
// Check the parent node of the second node.125
if (node2.Parent == null) return false;126
if (node2.Parent.Equals(node1)) return true;127

128
// If the parent node is not null or equal to the first node, 129
// call the ContainsNode method recursively using the parent of 130
// the second node.131
return ContainsNode(node1, node2.Parent);132
}133

134
}135

136

137




浙公网安备 33010602011771号