如何实现treeview节点间的拖放

 我来答
安徽新华电脑专修学院
2018-11-24 · 百度知道合伙人官方认证企业
安徽新华电脑专修学院
安徽新华电脑专修学院始建于1988年,隶属于新华教育集团,是国家信息化教育示范基地、中国 IT 教育影响力品牌院校.
向TA提问
展开全部
public Form1()
{
//
// Windows 窗体设计器支持所必需的
//
InitializeComponent();
//
// TODO: 在 InitializeComponent 调用后添加任何构造函数代码
//
}
/// <summary>
/// 清理所有正在使用的资源。
/// </summary>
protected override void Dispose( bool disposing )
{
if( disposing )
{
if (components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
}
#region Windows 窗体设计器生成的代码
/// <summary>
/// 设计器支持所需的方法 - 不要使用代码编辑器修改
/// 此方法的内容。
/// </summary>
private void InitializeComponent()
{
this.treeView1 = new System.Windows.Forms.TreeView();
this.SuspendLayout();
//
// treeView1
//
this.treeView1.AllowDrop = true;
this.treeView1.Dock = System.Windows.Forms.DockStyle.Fill;
this.treeView1.HotTracking = true;
this.treeView1.ImageIndex = -1;
this.treeView1.Location = new System.Drawing.Point(0, 0);
this.treeView1.Name = "treeView1";
this.treeView1.Nodes.AddRange(new System.Windows.Forms.TreeNode[] {
new System.Windows.Forms.TreeNode("节点0", new System.Windows.Forms.TreeNode[] {
new System.Windows.Forms.TreeNode("节点1", new System.Windows.Forms.TreeNode[] {
new System.Windows.Forms.TreeNode("节点2", new System.Windows.Forms.TreeNode[] {
new System.Windows.Forms.TreeNode("节点3")})}),
new System.Windows.Forms.TreeNode("节点4", new System.Windows.Forms.TreeNode[] {
new System.Windows.Forms.TreeNode("节点6", new System.Windows.Forms.TreeNode[] {
new System.Windows.Forms.TreeNode("节点7")})}),
new System.Windows.Forms.TreeNode("节点5", new System.Windows.Forms.TreeNode[] {
new System.Windows.Forms.TreeNode("节点8", new System.Windows.Forms.TreeNode[] {
new System.Windows.Forms.TreeNode("节点9")})})})});
this.treeView1.SelectedImageIndex = -1;
this.treeView1.Size = new System.Drawing.Size(352, 349);
this.treeView1.TabIndex = 0;
this.treeView1.AfterExpand += new System.Windows.Forms.TreeViewEventHandler(this.treeView1_AfterExpand);
this.treeView1.AfterCollapse += new System.Windows.Forms.TreeViewEventHandler(this.treeView1_AfterCollapse);
this.treeView1.DragOver += new System.Windows.Forms.DragEventHandler(this.treeView1_DragOver);
this.treeView1.DragEnter += new System.Windows.Forms.DragEventHandler(this.treeView1_DragEnter);
this.treeView1.ItemDrag += new System.Windows.Forms.ItemDragEventHandler(this.treeView1_ItemDrag);
this.treeView1.DragDrop += new System.Windows.Forms.DragEventHandler(this.treeView1_DragDrop);
//
// Form1
//
this.AllowDrop = true;
this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
this.ClientSize = new System.Drawing.Size(352, 349);
this.Controls.Add(this.treeView1);
this.Name = "Form1";
this.Text = "Form1";
this.ResumeLayout(false);
}
#endregion
/// <summary>
/// 应用程序的主入口点。
/// </summary>
[STAThread]
static void Main()
{
Application.Run(new Form1());
}
protected void treeView1_DragDrop (object sender, DragEventArgs e)
{
//恢复上次鼠标位置处节点的颜色
oldNode.BackColor = oldNodeBackColor;
oldNode.ForeColor = oldNodeForeColor;
//把这个节点设为null
oldNode = null;
//如果允许移动,则可以肯定这是可以接受的数据
if(e.Effect == DragDropEffects.Move)
{
//获取鼠标位置处的节点
TreeNode overNode = FindTreeNode(e.X, e.Y);
//获取当前移动的节点
TreeNode movingNode = (TreeNode)(e.Data.GetData(typeof(TreeNode)));
//如果鼠标位置处没有节点
if(overNode == null)
{
}
else if( !IsIn( overNode, movingNode ) )
{
if(movingNode.Parent != null)
{
if(movingNode.Parent.Nodes.Count == 1)
{
movingNode.Parent.ImageIndex = 2;
movingNode.Parent.SelectedImageIndex = 2;
}
}

//把被移动的节点作为这个节点的子节点

movingNode.Remove();
overNode.Nodes.Add(movingNode);
//展开这个节点
overNode.Expand();
//更改为展开后的图标
overNode.ImageIndex = 1;
}

}
}
protected void treeView1_DragEnter (object sender, DragEventArgs e)
{
//获取TreeNode格式的数据
object data = e.Data.GetData(typeof(TreeNode));
//如果数据类型为TreeNode
if(data != null)
{
//把这个数据转换成TreeNode类型
TreeNode dragedNode = (TreeNode)data;
//检查它是否属于这个窗体内的treeView控件
if(dragedNode.TreeView.Equals(treeView1))
{
//如果是,则可以实现移动拖放功能
e.Effect = DragDropEffects.Move;
}
else
{
//否在,不接受这个数据
e.Effect = DragDropEffects.None;
}
}
else
{
//如果数据类型不为TreeNode,则不接受这个数据
e.Effect = DragDropEffects.None;
}
}
protected void treeView1_DragOver (object sender, DragEventArgs e)
{
//如果允许移动,则可以肯定这是可以接受的数据
if(e.Effect == DragDropEffects.Move)
{
//获取鼠标位置处的节点
TreeNode overNode = FindTreeNode(e.X, e.Y);
//如果鼠标位置处的节点不为null,且不是上一次鼠标位置处的节点
if(overNode != null && !overNode.Equals(oldNode))
{
//如果上一次鼠标位置处有节点,恢复那个节点的颜色
if(oldNode != null)
{
oldNode.BackColor = oldNodeBackColor;
oldNode.ForeColor = oldNodeForeColor;
}
//把这一次的节点设为老节点
oldNode = overNode;
//保存当前鼠标位置处节点的颜色
oldNodeBackColor = overNode.BackColor;
oldNodeForeColor = overNode.ForeColor;
//获取当前移动的节点
TreeNode movingNode = (TreeNode)(e.Data.GetData(typeof(TreeNode)));
//根据当前鼠标位置处的节点是否为正被拖动的节点分别设置颜色
if(overNode.Equals(movingNode))
{
overNode.BackColor = Color.Red;
overNode.ForeColor = Color.White;
}
else
{
overNode.BackColor = Color.Green;
overNode.ForeColor = Color.White;
}
}
}
}
protected void treeView1_ItemDrag (object sender, ItemDragEventArgs e)
{
//启动拖放操作
DoDragDrop(e.Item, DragDropEffects.Move);
}
//AfterCollapse事件方法
protected void treeView1_AfterCollapse (object sender, TreeViewEventArgs e)
{
//改变ImageIndex
e.Node.ImageIndex = 0;
//改变SelectedImageIndex
e.Node.SelectedImageIndex = 0;
}
//AfterExpand事件方法
protected void treeView1_AfterExpand (object sender, TreeViewEventArgs e)
{
//改变ImageIndex
e.Node.ImageIndex = 1;
//改变SelectedImageIndex
e.Node.SelectedImageIndex = 1;
}
private TreeNode FindTreeNode (int x, int y)
{
//把当前的鼠标位置转换成控件内的坐标
Point pt = treeView1.PointToClient(new Point(x, y));
//获取第一个可见的节点,如果窗口没有滚动,这将是第一个根节点
TreeNode node = treeView1.TopNode;
while(node != null)
{
//检查鼠标坐标是否包含在这个节点内部
if(node.Bounds.Contains(pt))
{
return node;
}
//获取下一个可见的节点
node = node.NextVisibleNode;
}
return null;
}
private bool IsIn( TreeNode nodeA,TreeNode nodeB )
{
bool a ;
if ( nodeB == null )
a = false;
a = System.Text.RegularExpressions.Regex.IsMatch( nodeA.FullPath.Replace("//",""),nodeB.FullPath.Replace("//","") );
return a;

}
}
}
推荐律师服务: 若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询

为你推荐:

下载百度知道APP,抢鲜体验
使用百度知道APP,立即抢鲜体验。你的手机镜头里或许有别人想知道的答案。
扫描二维码下载
×

类别

我们会通过消息、邮箱等方式尽快将举报结果通知您。

说明

0/200

提交
取消

辅 助

模 式