树节点移动操作----使用微软treeview控件[原创]

树节点移动操作<?xml:namespace prefix = o ns = "urn:schemas-microsoft-com:office:office" />

----使用微软treeview控件

作者:pfengk

MSN pfengk@hotmail.com

更多文章请访问:http://www.cnblogs.com/pfengk/

 

目录

概述................................................................................................................................. 1

操作演示.......................................................................................................................... 1

实现源码:[C#]................................................................................................................ 2

概述

本文集中体现树节点移动实现代码其中很多其他代码省略。如果你对treeview控件不了解,请参阅我的另外一篇文章《web方式下权限分配简单实现》。

       源码中引用命名空间Microsoft.Web.UI.WebControls。

    本文描述节点移动算法效率非常低下,仅仅适合于节点不多的情况下使用。还请高手们多多指教。联系我的msn,谢谢你们!

操作演示

窗体载入初始界面

<?xml:namespace prefix = v ns = "urn:schemas-microsoft-com:vml" />

 050705007.JPG

 

选择节点之后

 050705002.JPG

 

点击[赋予]按钮之后

 050705003.JPG

 

再次点击[赋予]按钮之后

 

实现源码:[C#]

  1 ExpandedBlockStart.gif ContractedBlock.gif "一棵树到另一棵树的 节点移动方法" #region "一棵树到另一棵树的 节点移动方法"
  2InBlock.gif
  3ExpandedSubBlockStart.gifContractedSubBlock.gif        /**//// <summary>
  4InBlock.gif
  5InBlock.gif        /// 从parentTreeView(森林)的所有根节点中查找childTreeNode是否存在(使用Text属性为关键字比较),存在返回true.
  6InBlock.gif
  7InBlock.gif        /// </summary>
  8InBlock.gif
  9InBlock.gif        /// <param name="childTreeNode"></param>
 10InBlock.gif
 11InBlock.gif        /// <param name="parentTreeNode"></param>
 12InBlock.gif
 13ExpandedSubBlockEnd.gif        /// <returns></returns>

 14InBlock.gif
 15InBlock.gif        public bool FindNodeFromChildNodes(TreeNode childTreeNode,TreeView parentTreeView)
 16InBlock.gif
 17ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
 18InBlock.gif
 19InBlock.gif            bool returnValue = false;
 20InBlock.gif
 21InBlock.gif            
 22InBlock.gif
 23InBlock.gif            foreach(TreeNode tempTreeNode in parentTreeView.Nodes)
 24InBlock.gif
 25ExpandedSubBlockStart.gifContractedSubBlock.gif            dot.gif{
 26InBlock.gif
 27InBlock.gif                if (childTreeNode.Text ==  tempTreeNode.Text)
 28InBlock.gif
 29ExpandedSubBlockStart.gifContractedSubBlock.gif                dot.gif{
 30InBlock.gif
 31InBlock.gif                    returnValue = true;
 32InBlock.gif
 33InBlock.gif                    break;
 34InBlock.gif
 35ExpandedSubBlockEnd.gif                }

 36InBlock.gif
 37ExpandedSubBlockEnd.gif            }

 38InBlock.gif
 39InBlock.gif            return returnValue;
 40InBlock.gif
 41ExpandedSubBlockEnd.gif        }

 42InBlock.gif
 43InBlock.gif 
 44InBlock.gif
 45ExpandedSubBlockStart.gifContractedSubBlock.gif        /**//// <summary>
 46InBlock.gif
 47InBlock.gif        /// 从parentTreeView(森林)的所有根节点中查找childTreeNode是否存在(使用Text属性为关键字比较),存在返回true.
 48InBlock.gif
 49InBlock.gif        /// </summary>
 50InBlock.gif
 51InBlock.gif        /// <param name="childTreeNode"></param>
 52InBlock.gif
 53InBlock.gif        /// <param name="parentTreeNode"></param>
 54InBlock.gif
 55ExpandedSubBlockEnd.gif        /// <returns></returns>

 56InBlock.gif
 57InBlock.gif        public bool FindNodeFromChildNodes(TreeNode childTreeNode,TreeView parentTreeView,ref TreeNode foundTreeNode)
 58InBlock.gif
 59ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
 60InBlock.gif
 61InBlock.gif            bool returnValue = false;
 62InBlock.gif
 63InBlock.gif            
 64InBlock.gif
 65InBlock.gif            foreach(TreeNode tempTreeNode in parentTreeView.Nodes)
 66InBlock.gif
 67ExpandedSubBlockStart.gifContractedSubBlock.gif            dot.gif{
 68InBlock.gif
 69InBlock.gif                if (childTreeNode.Text ==  tempTreeNode.Text)
 70InBlock.gif
 71ExpandedSubBlockStart.gifContractedSubBlock.gif                dot.gif{
 72InBlock.gif
 73InBlock.gif                    returnValue = true;
 74InBlock.gif
 75InBlock.gif                    foundTreeNode = tempTreeNode;
 76InBlock.gif
 77InBlock.gif                    break;
 78InBlock.gif
 79ExpandedSubBlockEnd.gif                }

 80InBlock.gif
 81ExpandedSubBlockEnd.gif            }

 82InBlock.gif
 83InBlock.gif            return returnValue;
 84InBlock.gif
 85ExpandedSubBlockEnd.gif        }

 86InBlock.gif
 87InBlock.gif 
 88InBlock.gif
 89ExpandedSubBlockStart.gifContractedSubBlock.gif        /**//// <summary>
 90InBlock.gif
 91InBlock.gif        /// 从parentTreeNode(根节点)的所有节点中查找childTreeNode是否存在(使用Text属性为关键字比较),存在返回true.
 92InBlock.gif
 93InBlock.gif        /// </summary>
 94InBlock.gif
 95InBlock.gif        /// <param name="childTreeNode"></param>
 96InBlock.gif
 97InBlock.gif        /// <param name="parentTreeNode"></param>
 98InBlock.gif
 99ExpandedSubBlockEnd.gif        /// <returns></returns>

100InBlock.gif
101InBlock.gif        public bool FindNodeFromChildNodes(TreeNode childTreeNode,TreeNode parentTreeNode)
102InBlock.gif
103ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
104InBlock.gif
105InBlock.gif            bool returnValue = false;
106InBlock.gif
107InBlock.gif            
108InBlock.gif
109InBlock.gif            foreach(TreeNode tempTreeNode in parentTreeNode.Nodes)
110InBlock.gif
111ExpandedSubBlockStart.gifContractedSubBlock.gif            dot.gif{
112InBlock.gif
113InBlock.gif                if (childTreeNode.Text ==  tempTreeNode.Text)
114InBlock.gif
115ExpandedSubBlockStart.gifContractedSubBlock.gif                dot.gif{
116InBlock.gif
117InBlock.gif                    returnValue = true;
118InBlock.gif
119InBlock.gif                    break;
120InBlock.gif
121ExpandedSubBlockEnd.gif                }

122InBlock.gif
123ExpandedSubBlockEnd.gif            }

124InBlock.gif
125InBlock.gif            return returnValue;
126InBlock.gif
127ExpandedSubBlockEnd.gif        }

128InBlock.gif
129InBlock.gif 
130InBlock.gif
131ExpandedSubBlockStart.gifContractedSubBlock.gif        /**//// <summary>
132InBlock.gif
133InBlock.gif        /// 从parentTreeView(森林)的所有根节点中查找childTreeNode是否存在(使用Text属性为关键字比较),存在返回true.
134InBlock.gif
135InBlock.gif        /// </summary>
136InBlock.gif
137InBlock.gif        /// <param name="childTreeNode"></param>
138InBlock.gif
139InBlock.gif        /// <param name="parentTreeNode"></param>
140InBlock.gif
141ExpandedSubBlockEnd.gif        /// <returns></returns>

142InBlock.gif
143InBlock.gif        public bool FindNodeFromChildNodes(TreeNode childTreeNode,TreeNode parentTreeNode,ref TreeNode foundTreeNode)
144InBlock.gif
145ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
146InBlock.gif
147InBlock.gif            bool returnValue = false;
148InBlock.gif
149InBlock.gif            
150InBlock.gif
151InBlock.gif            foreach(TreeNode tempTreeNode in parentTreeNode.Nodes)
152InBlock.gif
153ExpandedSubBlockStart.gifContractedSubBlock.gif            dot.gif{
154InBlock.gif
155InBlock.gif                if (childTreeNode.Text ==  tempTreeNode.Text)
156InBlock.gif
157ExpandedSubBlockStart.gifContractedSubBlock.gif                dot.gif{
158InBlock.gif
159InBlock.gif                    returnValue = true;
160InBlock.gif
161InBlock.gif                    foundTreeNode = tempTreeNode;
162InBlock.gif
163InBlock.gif                    break;
164InBlock.gif
165ExpandedSubBlockEnd.gif                }

166InBlock.gif
167ExpandedSubBlockEnd.gif            }

168InBlock.gif
169InBlock.gif            return returnValue;
170InBlock.gif
171ExpandedSubBlockEnd.gif        }

172InBlock.gif
173InBlock.gif 
174InBlock.gif
175ExpandedSubBlockStart.gifContractedSubBlock.gif        /**//// <summary>
176InBlock.gif
177InBlock.gif        /// 移除叶子节点。首先判断是否为叶子节点或根节点,是则移除,否则跳出返回
178InBlock.gif
179InBlock.gif        /// </summary>
180InBlock.gif
181InBlock.gif        /// <param name="treeNode"></param>
182InBlock.gif
183ExpandedSubBlockEnd.gif        /// <returns></returns>

184InBlock.gif
185InBlock.gif        public bool RemoveLeafageChildNode(ref TreeNode treeNode)
186InBlock.gif
187ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
188InBlock.gif
189InBlock.gif            bool returnValue = false;
190InBlock.gif
191InBlock.gif            if (treeNode.Nodes == null | treeNode.Nodes.Count == 0)
192InBlock.gif
193ExpandedSubBlockStart.gifContractedSubBlock.gif            dot.gif{//是子节点时候
194InBlock.gif
195InBlock.gif                if (treeNode.Parent is TreeNode)
196InBlock.gif
197ExpandedSubBlockStart.gifContractedSubBlock.gif                dot.gif{//父节点为节点类型时
198InBlock.gif
199InBlock.gif                    int nodesCount = ((TreeNode)treeNode.Parent).Nodes.Count;
200InBlock.gif
201InBlock.gif                    if (nodesCount > 1)
202InBlock.gif
203ExpandedSubBlockStart.gifContractedSubBlock.gif                    dot.gif{//还有兄弟叶子节点时候,仅仅删除本节点
204InBlock.gif
205InBlock.gif                        treeNode.Remove();
206InBlock.gif
207InBlock.gif                        returnValue = true;
208InBlock.gif
209ExpandedSubBlockEnd.gif                    }

210InBlock.gif
211InBlock.gif                    else
212InBlock.gif
213ExpandedSubBlockStart.gifContractedSubBlock.gif                    dot.gif{//是唯一叶子节点时候,还要删除父节点
214InBlock.gif
215InBlock.gif                        //删除本节点
216InBlock.gif
217InBlock.gif                        TreeNode thisParentTreeNode = (TreeNode)treeNode.Parent;
218InBlock.gif
219InBlock.gif                        treeNode.Remove();
220InBlock.gif
221InBlock.gif                        //删除父节点
222InBlock.gif
223InBlock.gif                        returnValue = RemoveLeafageChildNode(ref thisParentTreeNode);
224InBlock.gif
225ExpandedSubBlockEnd.gif                    }

226InBlock.gif
227ExpandedSubBlockEnd.gif                }

228InBlock.gif
229InBlock.gif                else
230InBlock.gif
231ExpandedSubBlockStart.gifContractedSubBlock.gif                dot.gif{//父节点不为节点类型时,他一定是根节点
232InBlock.gif
233InBlock.gif                    treeNode.Remove();
234InBlock.gif
235InBlock.gif                    returnValue = true;
236InBlock.gif
237ExpandedSubBlockEnd.gif                }

238InBlock.gif
239ExpandedSubBlockEnd.gif            }

240InBlock.gif
241InBlock.gif            return returnValue;
242InBlock.gif
243ExpandedSubBlockEnd.gif        }

244InBlock.gif
245InBlock.gif 
246InBlock.gif
247ExpandedSubBlockStart.gifContractedSubBlock.gif        /**//// <summary>
248InBlock.gif
249InBlock.gif        /// 【MoveNodes】将节点分支中所有被选中的节点,移动到另外一个节点分支上。前提根节点必须保留,且各节点Text值唯一。
250InBlock.gif
251InBlock.gif        /// </summary>
252InBlock.gif
253InBlock.gif        /// <param name="fromTreeView"></param>
254InBlock.gif
255ExpandedSubBlockEnd.gif        /// <param name="toTreeNode"></param>

256InBlock.gif
257InBlock.gif        public void MoveNodes(ref TreeNode fromTreeNode,ref TreeNode toTreeNode)
258InBlock.gif
259ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
260InBlock.gif
261InBlock.gif            bool selected = true;
262InBlock.gif
263InBlock.gif            //遍历fromTreeNode子节点,比较选中子节点在toTreeNode的子节点集中是否存在,不存在则移动子节点;存在跳过
264InBlock.gif
265InBlock.gif            int h = fromTreeNode.Nodes.Count-1;
266InBlock.gif
267InBlock.gif            for (int i = h ; i>=0;i--)
268InBlock.gif
269ExpandedSubBlockStart.gifContractedSubBlock.gif            dot.gif{
270InBlock.gif
271InBlock.gif                TreeNode rootNode = new TreeNode();
272InBlock.gif
273InBlock.gif                rootNode = fromTreeNode.Nodes[i];
274InBlock.gif
275InBlock.gif                //根节点是否被选中,否则跳过。
276InBlock.gif
277InBlock.gif                if (rootNode.Checked == selected)
278InBlock.gif
279ExpandedSubBlockStart.gifContractedSubBlock.gif                dot.gif{
280InBlock.gif
281InBlock.gif                    //本节点是否为叶子节点
282InBlock.gif
283InBlock.gif                    bool isDeleted = false;
284InBlock.gif
285InBlock.gif                    //子节点在 toTreeNode中的子节点集 中是否存在
286InBlock.gif
287InBlock.gif                    if (!FindNodeFromChildNodes (rootNode,toTreeNode))
288InBlock.gif
289ExpandedSubBlockStart.gifContractedSubBlock.gif                    dot.gif{
290InBlock.gif
291InBlock.gif                        //移动节点,判断是否为子节点。是子节点则从fromTreeNode中移除,同时判断是否为唯一节点,是同时移除父节点,
292InBlock.gif
293InBlock.gif                        //反之忽略。
294InBlock.gif
295InBlock.gif                        TreeNode rootNodeClone = ((TreeNode)rootNode.Clone());
296InBlock.gif
297InBlock.gif                        int k = rootNodeClone.Nodes.Count;
298InBlock.gif
299InBlock.gif                        for(int j=0; j<k;j++)
300InBlock.gif
301ExpandedSubBlockStart.gifContractedSubBlock.gif                        dot.gif{
302InBlock.gif
303InBlock.gif                            rootNodeClone.Nodes.RemoveAt(0);
304InBlock.gif
305ExpandedSubBlockEnd.gif                        }

306InBlock.gif
307InBlock.gif                        toTreeNode.Nodes.Add(rootNodeClone);
308InBlock.gif
309InBlock.gif 
310InBlock.gif
311InBlock.gif                        isDeleted = RemoveLeafageChildNode(ref rootNode);
312InBlock.gif
313ExpandedSubBlockEnd.gif                    }

314InBlock.gif
315InBlock.gif                    
316InBlock.gif
317InBlock.gif                    //移动子节点的整个分支
318InBlock.gif
319InBlock.gif                    if (!isDeleted)
320InBlock.gif
321ExpandedSubBlockStart.gifContractedSubBlock.gif                    dot.gif{
322InBlock.gif
323InBlock.gif                        TreeNode toRootNode = new TreeNode();
324InBlock.gif
325InBlock.gif                        //定位toTreeNode中与rootNode相同的节点位置。
326InBlock.gif
327InBlock.gif                        FindNodeFromChildNodes(rootNode,toTreeNode,ref toRootNode);
328InBlock.gif
329InBlock.gif                        //移动fromTreeNode中rootNode的分支中所有选中节点到toTreeNode的toRootNode分支中
330InBlock.gif
331InBlock.gif                        MoveNodes(ref rootNode,ref toRootNode);
332InBlock.gif
333ExpandedSubBlockEnd.gif                    }

334InBlock.gif
335ExpandedSubBlockEnd.gif                }

336InBlock.gif
337ExpandedSubBlockEnd.gif            }

338InBlock.gif
339ExpandedSubBlockEnd.gif        }

340InBlock.gif
341ExpandedSubBlockStart.gifContractedSubBlock.gif        /**//// <summary>
342InBlock.gif
343InBlock.gif        /// 【MoveNodes】将一棵树中所有被选中的节点,移动到另外一棵树上。前提根节点必须保留,且各节点Text值唯一。
344InBlock.gif
345InBlock.gif        /// </summary>
346InBlock.gif
347InBlock.gif        /// <param name="fromTreeNode"></param>
348InBlock.gif
349ExpandedSubBlockEnd.gif        /// <param name="toTreeNode"></param>

350InBlock.gif
351InBlock.gif        public void MoveNodes(ref TreeView fromTreeView,ref TreeView toTreeView)
352InBlock.gif
353ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
354InBlock.gif
355InBlock.gif            bool selected = true;
356InBlock.gif
357InBlock.gif 
358InBlock.gif
359InBlock.gif            //遍历fromTreeView根节点,比较选中节点在toTreeView的根节点集中是否存在,不存在则移动节点;存在跳过
360InBlock.gif
361InBlock.gif            
362InBlock.gif
363InBlock.gif            int h = fromTreeView.Nodes.Count-1;
364InBlock.gif
365InBlock.gif            for (int i = h ; i>=0;i--)
366InBlock.gif
367ExpandedSubBlockStart.gifContractedSubBlock.gif            dot.gif{
368InBlock.gif
369InBlock.gif                TreeNode rootNode = new TreeNode();
370InBlock.gif
371InBlock.gif                rootNode = fromTreeView.Nodes[i];
372InBlock.gif
373InBlock.gif                //根节点是否被选中,否则跳过。
374InBlock.gif
375InBlock.gif                if (rootNode.Checked == selected)
376InBlock.gif
377ExpandedSubBlockStart.gifContractedSubBlock.gif                dot.gif{
378InBlock.gif
379InBlock.gif                    //本节点是否为叶子节点
380InBlock.gif
381InBlock.gif                    bool isDeleted = false;
382InBlock.gif
383InBlock.gif                    //根节点在 toTreeView中的根节点中是否存在
384InBlock.gif
385InBlock.gif                    if (!FindNodeFromChildNodes (rootNode,toTreeView))
386InBlock.gif
387ExpandedSubBlockStart.gifContractedSubBlock.gif                    dot.gif{
388InBlock.gif
389InBlock.gif                        //移动节点,判断是否为子节点。是子节点则从fromTreeView中移除,同时判断是否为唯一节点,是同时移除父节点,
390InBlock.gif
391InBlock.gif                        //反之忽略。
392InBlock.gif
393InBlock.gif                        TreeNode rootNodeClone = ((TreeNode)rootNode.Clone());
394InBlock.gif
395InBlock.gif                        int k = rootNodeClone.Nodes.Count;
396InBlock.gif
397InBlock.gif                        for(int j=0; j<k;j++)
398InBlock.gif
399ExpandedSubBlockStart.gifContractedSubBlock.gif                        dot.gif{
400InBlock.gif
401InBlock.gif                            rootNodeClone.Nodes.RemoveAt(0);
402InBlock.gif
403ExpandedSubBlockEnd.gif                        }

404InBlock.gif
405InBlock.gif 
406InBlock.gif
407InBlock.gif                        toTreeView.Nodes.Add(rootNodeClone);
408InBlock.gif
409InBlock.gif 
410InBlock.gif
411InBlock.gif                        isDeleted = RemoveLeafageChildNode(ref rootNode);
412InBlock.gif
413ExpandedSubBlockEnd.gif                    }

414InBlock.gif
415InBlock.gif                    //移动根节点的整个分支
416InBlock.gif
417InBlock.gif                    if (!isDeleted)
418InBlock.gif
419ExpandedSubBlockStart.gifContractedSubBlock.gif                    dot.gif{
420InBlock.gif
421InBlock.gif                        TreeNode toRootNode = new TreeNode();
422InBlock.gif
423InBlock.gif                        //定位toTreeView中与rootNode相同的节点位置。
424InBlock.gif
425InBlock.gif                        FindNodeFromChildNodes(rootNode,toTreeView,ref toRootNode);
426InBlock.gif
427InBlock.gif                        //移动fromTreeView中rootNode的分支中所有选中节点到toTreeView的toRootNode分支中
428InBlock.gif
429InBlock.gif                        MoveNodes(ref rootNode,ref toRootNode);
430InBlock.gif
431ExpandedSubBlockEnd.gif                    }

432InBlock.gif
433ExpandedSubBlockEnd.gif                }

434InBlock.gif
435ExpandedSubBlockEnd.gif            }

436InBlock.gif
437ExpandedSubBlockEnd.gif        }

438InBlock.gif
439InBlock.gif 
440InBlock.gif
441ExpandedBlockEnd.gif        #endregion

442 None.gif

 

 

[THE END]

搜索一下相关内容

转载于:https://www.cnblogs.com/pfengk/articles/187031.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值