webform的treeview怎么实现节点拖拽
发布网友
发布时间:2022-05-09 14:04
我来回答
共2个回答
热心网友
时间:2023-12-24 19:02
private void sdTreeView1_DragDrop(object sender, DragEventArgs e)
{
TreeNode myNode = null;
if (e.Data.GetDataPresent(typeof (sdTreeNode)))
{
myNode = (TreeNode)(e.Data.GetData(typeof(sdTreeNode)));
}
else
{
MessageBox.Show("error");
}
Position.X = e.X;
Position.Y = e.Y;
Position = sdTreeView1.PointToClient(Position);
DropNode = this.sdTreeView1.GetNodeAt(Position);
if (DropNode != null)
DropNode.BackColor = TreeNodeDefaultColor;
else
return;
// 1.目标节点不是空。2.目标节点不是被拖拽接点的字节点。3.目标节点不是被拖拽节点本身
TreeNode node = DropNode.Parent;
while(node!=null&&node.Level>=0)
{
if (node == myNode)
return;
node = node.Parent;
}
if (DropNode != null && DropNode.Parent != myNode && DropNode != myNode)
{
DialogResult dr = MessageBox.Show(this,
"您确定移动此节点?", "移动提示",
MessageBoxButtons.YesNo, MessageBoxIcon.Question);
//中文
if (dr != DialogResult.Yes)
{
return;
}
AfterDragNode(myNode, DropNode);
myNode.ExpandAll();
TreeNode DragNode = myNode;
// 将被拖拽节点从原来位置删除。
myNode.Remove();
// 在目标节点下增加被拖拽节点
DropNode.Nodes.Add(DragNode);
}
// 如果目标节点不存在,即拖拽的位置不存在节点,那么就将被拖拽节点放在根节点之下
if (DropNode == null)
{
DialogResult dr = MessageBox.Show(this,
"您确定移动此节点?", "移动提示",
MessageBoxButtons.YesNo, MessageBoxIcon.Question);
//中文
if (dr != DialogResult.Yes)
{
return;
}
AfterDragNode(myNode, DropNode);
myNode.ExpandAll();
TreeNode DragNode = myNode;
myNode.Remove();
sdTreeView1.Nodes.Add(DragNode);
}
}
private void sdTreeView1_DragOver(object sender,DragEventArgs e)
{
try
{
sdTreeView1.SelectChangeColor = false;
sdTreeView1.SelectedNode = null;
if (DropNode != null)
DropNode.BackColor = TreeNodeDefaultColor;
Position.X = e.X;
Position.Y = e.Y;
Position = sdTreeView1.PointToClient(Position);
DropNode = this.sdTreeView1.GetNodeAt(Position);
DropNode.BackColor = Color.HotPink;
}
finally
{
sdTreeView1.SelectChangeColor = true;
}
}
private void sdTreeView1_DragEnter(object sender, DragEventArgs e)
{
if (e.Data.GetDataPresent(typeof(sdTreeNode)))
e.Effect = DragDropEffects.Move;
else
e.Effect = DragDropEffects.None;
/*
Position.X = e.X;
Position.Y = e.Y;
Position = sdTreeView1.PointToClient(Position);
DropNode = this.sdTreeView1.GetNodeAt(Position);
DropNode.BackColor = Color.IndianRed;*/
}
热心网友
时间:2023-12-24 19:03
需要定义treeview控件的几个事件,并且AllowDrop属性必须设置为true。
需要定义DragDrop、DragLeave、DragOver和DragEnter事件
还可以订阅GiveFeedBack 和QueryContinueDrag事件。
具体的拖放逻辑可以参考1楼的实现代码。