TreeView控件中实现拖拽的功能
#region 节点拖拽事件
//当用户开始拖动节点时
private void tvModel_ItemDrag(object sender, ItemDragEventArgs e)
{
TreeNode selectNode = e.Item as TreeNode;
this.tvModel.SelectedNode = selectNode;
this.form.DoDragDrop(e.Item, DragDropEffects.Move);
}
//将对象拖入控件的边界时
private void tvModel_DragEnter(object sender, DragEventArgs e)
{
TreeNode enterNode = (TreeNode)(e.Data.GetData(typeof(TreeNode)));
if (enterNode != null)
e.Effect = DragDropEffects.Move;
else
e.Effect = DragDropEffects.None;
}
//在完成拖放操作时
private void tvModel_DragDrop(object sender, DragEventArgs e)
{
TreeNode selectNode = (TreeNode)(e.Data.GetData(typeof(TreeNode)));
//0.如果选择源为空 或者不是二级节点则退出
if (selectNode == null || selectNode.Level != 1)
return;
//1.根据鼠标坐标获得目标节点
Position.X = e.X;
Position.Y = e.Y;
Position = this.tvModel.PointToClient(Position);
TreeNode targetNode = this.tvModel.GetNodeAt(Position);
foreach (TreeNode node in this.tvModel.Nodes[0].Nodes)
node.NodeFont = new Font(this.form.Font, FontStyle.Regular);
//2.获得要插入的目标节点索引
int index = 0;
if (targetNode == null)
index = selectNode.Parent.Nodes.Count - 1; //目标节点为null, 直接插入到最后
else if (targetNode.Level == 1)
index = targetNode.Index + 1; //目标节点为同级, 直接在之后插入
else if (targetNode.Level == 0)
index = 0; //目标为根节点, 直接插入到最前
else if (targetNode.Level > 1)
{ //目标节点为二级节点以后, 遍历到其二级节点
while (targetNode.Level > 1)
targetNode = targetNode.Parent;
index = index = targetNode.Index + 1;
}
//3.源节点删除, 目标节点在索引出插入, 并高亮选中
selectNode.Remove();
this.tvModel.Nodes[0].Nodes.Insert(index, selectNode);
this.tvModel.SelectedNode = selectNode;
}
//将对象拖过控件边缘时
private void tvModel_DragOver(object sender, DragEventArgs e)
{
Position.X = e.X;
Position.Y = e.Y;
Position = this.tvModel.PointToClient(Position);
//在拖过的控件前后显示划线效果
TreeNode targetNode = this.tvModel.GetNodeAt(Position);
if (targetNode != null && targetNode.Level == 1)
{
if (targetNode.PrevNode != null)
targetNode.PrevNode.NodeFont = new Font(this.form.Font, FontStyle.Regular);
if (targetNode.NextNode != null)
targetNode.NextNode.NodeFont = new Font(this.form.Font, FontStyle.Regular);
targetNode.NodeFont = new Font(this.form.Font, FontStyle.Underline);
}
}
#endregion
//当用户开始拖动节点时
private void tvModel_ItemDrag(object sender, ItemDragEventArgs e)
{
TreeNode selectNode = e.Item as TreeNode;
this.tvModel.SelectedNode = selectNode;
this.form.DoDragDrop(e.Item, DragDropEffects.Move);
}
//将对象拖入控件的边界时
private void tvModel_DragEnter(object sender, DragEventArgs e)
{
TreeNode enterNode = (TreeNode)(e.Data.GetData(typeof(TreeNode)));
if (enterNode != null)
e.Effect = DragDropEffects.Move;
else
e.Effect = DragDropEffects.None;
}
//在完成拖放操作时
private void tvModel_DragDrop(object sender, DragEventArgs e)
{
TreeNode selectNode = (TreeNode)(e.Data.GetData(typeof(TreeNode)));
//0.如果选择源为空 或者不是二级节点则退出
if (selectNode == null || selectNode.Level != 1)
return;
//1.根据鼠标坐标获得目标节点
Position.X = e.X;
Position.Y = e.Y;
Position = this.tvModel.PointToClient(Position);
TreeNode targetNode = this.tvModel.GetNodeAt(Position);
foreach (TreeNode node in this.tvModel.Nodes[0].Nodes)
node.NodeFont = new Font(this.form.Font, FontStyle.Regular);
//2.获得要插入的目标节点索引
int index = 0;
if (targetNode == null)
index = selectNode.Parent.Nodes.Count - 1; //目标节点为null, 直接插入到最后
else if (targetNode.Level == 1)
index = targetNode.Index + 1; //目标节点为同级, 直接在之后插入
else if (targetNode.Level == 0)
index = 0; //目标为根节点, 直接插入到最前
else if (targetNode.Level > 1)
{ //目标节点为二级节点以后, 遍历到其二级节点
while (targetNode.Level > 1)
targetNode = targetNode.Parent;
index = index = targetNode.Index + 1;
}
//3.源节点删除, 目标节点在索引出插入, 并高亮选中
selectNode.Remove();
this.tvModel.Nodes[0].Nodes.Insert(index, selectNode);
this.tvModel.SelectedNode = selectNode;
}
//将对象拖过控件边缘时
private void tvModel_DragOver(object sender, DragEventArgs e)
{
Position.X = e.X;
Position.Y = e.Y;
Position = this.tvModel.PointToClient(Position);
//在拖过的控件前后显示划线效果
TreeNode targetNode = this.tvModel.GetNodeAt(Position);
if (targetNode != null && targetNode.Level == 1)
{
if (targetNode.PrevNode != null)
targetNode.PrevNode.NodeFont = new Font(this.form.Font, FontStyle.Regular);
if (targetNode.NextNode != null)
targetNode.NextNode.NodeFont = new Font(this.form.Font, FontStyle.Regular);
targetNode.NodeFont = new Font(this.form.Font, FontStyle.Underline);
}
}
#endregion
作者:阿笨
【官方QQ一群:跟着阿笨一起玩NET(已满)】:422315558
【官方QQ二群:跟着阿笨一起玩C#(已满)】:574187616
【官方QQ三群:跟着阿笨一起玩ASP.NET(已满)】:967920586
【官方QQ四群:Asp.Net Core跨平台技术开发(可加入)】:829227829
【官方QQ五群:.NET Core跨平台开发技术(可加入)】:647639415
【网易云课堂】:https://study.163.com/provider/2544628/index.htm?share=2&shareId=2544628
【腾讯课堂】:https://abennet.ke.qq.com
【51CTO学院】:https://edu.51cto.com/sd/66c64
【微信公众号】:微信搜索:跟着阿笨一起玩NET