我想实现c#中 treeview的节点拖拽功能?
2013-06-20
展开全部
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;*/
}
{
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;*/
}
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询