递归DeleteMenuItem

/// <summary>
        /// 删除节点
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        void DeleteMenuItem(object sender, EventArgs e)
        {
            MenuTreeNode curNode = menuTreeView.SelectedNode as MenuTreeNode;
            if (curNode.Nodes.Count > 0)
                DeleteNodes(curNode);
            else
            {
                curNode.Record.Delete();
                curNode.Remove();
            }

        }
        /// <summary>
        /// 递归删除节点,特别说明:递归删除节点不能用foreach和for,因为节点删除后,下次迭代目标是null,不过递减方式遍历也许可以
        /// </summary>
        /// <param name="node">待删除的节点</param>
        void DeleteNodes(MenuTreeNode node)
        {
            MenuTreeNode tmpNode = node;
            
            while (tmpNode != null)
            {
                if (tmpNode.Nodes.Count > 0)
                {
                    DeleteNodes((MenuTreeNode)tmpNode.Nodes[0]);
                }
                MenuTreeNode aNode = tmpNode;
                tmpNode = (MenuTreeNode)tmpNode.NextNode;
                aNode.Record.Delete();
                aNode.Remove();
             
            }
           
posted @ 2013-05-12 19:34  perock  阅读(293)  评论(0编辑  收藏  举报