惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

S
Secure Thoughts
罗磊的独立博客
T
The Blog of Author Tim Ferriss
人人都是产品经理
人人都是产品经理
博客园 - 叶小钗
Last Week in AI
Last Week in AI
美团技术团队
Google Online Security Blog
Google Online Security Blog
Application and Cybersecurity Blog
Application and Cybersecurity Blog
D
Docker
G
Google Developers Blog
大猫的无限游戏
大猫的无限游戏
酷 壳 – CoolShell
酷 壳 – CoolShell
小众软件
小众软件
月光博客
月光博客
L
LINUX DO - 最新话题
cs.AI updates on arXiv.org
cs.AI updates on arXiv.org
CTFtime.org: upcoming CTF events
CTFtime.org: upcoming CTF events
W
WeLiveSecurity
H
Heimdal Security Blog
Vercel News
Vercel News
SecWiki News
SecWiki News
Forbes - Security
Forbes - Security
Blog — PlanetScale
Blog — PlanetScale
Google DeepMind News
Google DeepMind News
Exploit-DB.com RSS Feed
Exploit-DB.com RSS Feed
www.infosecurity-magazine.com
www.infosecurity-magazine.com
TaoSecurity Blog
TaoSecurity Blog
T
Troy Hunt's Blog
A
About on SuperTechFans
C
Check Point Blog
S
Security Affairs
Hacker News - Newest:
Hacker News - Newest: "LLM"
AI
AI
WordPress大学
WordPress大学
K
KPMG report finds enterprise disconnect between AI and its ROI | CIO
Help Net Security
Help Net Security
博客园_首页
The Last Watchdog
The Last Watchdog
S
SegmentFault 最新的问题
Hugging Face - Blog
Hugging Face - Blog
Security Archives - TechRepublic
Security Archives - TechRepublic
Engineering at Meta
Engineering at Meta
cs.CV updates on arXiv.org
cs.CV updates on arXiv.org
I
Intezer
K
Kaspersky official blog
M
MIT News - Artificial intelligence
J
Java Code Geeks
G
GRAHAM CLULEY
P
Palo Alto Networks Blog

博客园 - Gofficer

决战紫禁之巅 为学 C#网页自动登录和提交POST信息的多种方法 一个C#写的调用外部进程类 PPT转图片 开发人员,敢问路在何方? C# 实现注销、关机、重启电脑功能 ultraGrid 控件中,实现单元格内容换行显示 如何用一条sql取得第10到第20条的记录? 用Sandcastle一键生成CHM帮助文档 实现服务器端与客户端对话 C#中访问WEB页面 使用代理服务器 自定义Ping方法 HTTP请求和应答 Socket套接字实现服务器端连接 Socket套接字实现客户端连接 启动和停止本地系统进程 异常处理
快速实现在Windows应用程序中支持拖拽的TreeView控件(C#)
Gofficer · 2008-10-16 · via 博客园 - Gofficer

概要

快速实现在Windows应用程序中支持拖拽的TreeView控件。

技术描述

TreeView已经提供了支持拖拽的事件:


 ItemDrag: 用户拖拽TreeNode时触发。当它发生时,调用DoDragDrop方法初始化拖拽过程。
 

 DragEnter: 在你初始化拖拽操作后,你必须处理目标TreeView控件的DragEnter事件。 这个事件发生在用户拖拽TreeNode对象从TreeView控件到目标控件范围点内。DragEnter事件能够指定目标TreeView控件,无论拖拽操作对这个控件是否用。代码中仅仅是移动操作。
 

 DragDrop: 最后是要处理目标TreeView的DragDrop事件。这个事件发生在用户推拽TreeNode对象并释放到目标控件中。处理这个事件, 返回TreeNode对象并添加到目标TreeView控件上。代码中用Data对象返回。
 

以下代码实现一个TreeView控件上拖拽任意节点到指定节点上,也可以自己扩展为多个TreeView控件间TreeNode相互拖拽的程序。Data对象的GetData方法返回被拖拽的TreeNode对象。GetNodeAt方法用来确定这个TreeNode对象拖拽到的目标控件(这里就是目标TreeNode对象)。在确定位置之后把源TreeNode对象添加到目标TreeNode对象下面,作为其子结点。因为是移动操作,因此最后会把源TreeNode对象删除掉。

通过以下几步创建实例程序:

1.
 创建C# Windows应用程序
 
2.
 界面增加一个TreeView控件
 
3.
 设置TreeView的AllowDrop属性设置为True
 
4.
 Page_Load方法中增加如下代码:

private void Form1_Load(object sender, System.EventArgs e)

              {

// TreeView控件增加一些测试节点

                     TreeNode ParentNode1;

                     ParentNode1 = treeView1.Nodes.Add("tv1");

                     ParentNode1.Nodes.Add("tv1FirstChild");

                     ParentNode1.Nodes.Add("tv1SecondChild");

                     ParentNode1.Nodes.Add("tv1ThirdChild");

                     ParentNode1.Nodes.Add("tv1FourthChild");

                     ParentNode1.Expand();

// TreeView控件增加事件

                     this.treeView1.ItemDrag += new System.Windows.Forms.ItemDragEventHandler(this.treeView_ItemDrag);

                     this.treeView1.DragEnter += new System.Windows.Forms.DragEventHandler(this.treeView_DragEnter);

                     this.treeView1.DragDrop += new System.Windows.Forms.DragEventHandler(this.treeView_DragDrop);

              }
 
5.
 //treeView_ItemDrag 事件代码:

private void treeView_ItemDrag(object sender,

                     System.Windows.Forms.ItemDragEventArgs e)

              {

                     DoDragDrop(e.Item, DragDropEffects.Move);

              }
 
6.
 // treeView_DragEnter事件代码:

private void treeView_DragEnter(object sender,

                     System.Windows.Forms.DragEventArgs e)

              {

                     e.Effect = DragDropEffects.Move;

              }
 
7.
 

// treeView_DragDrop事件代码:

private void treeView_DragDrop(object sender, System.Windows.Forms.DragEventArgs e)

              {

                     TreeNode NewNode;

                     if(e.Data.GetDataPresent(typeof(TreeNode)))

                     {

                            Point pt = ((TreeView)sender).PointToClient(new Point(e.X, e.Y));

                            TreeNode DestinationNode = ((TreeView)sender).GetNodeAt(pt);

                            NewNode = (TreeNode)e.Data.GetData("System.Windows.Forms.TreeNode");

                            DestinationNode.Nodes.Add((TreeNode) NewNode.Clone());

                            DestinationNode.Expand();

                            //删除已经移动的节点

                            NewNode.Remove();

                     }

              }