日期:2014-05-17  浏览次数:21092 次

c#winform treeview根据菜单创建节点

以下是递归生成树的代码:

public void CreateTree(MenuStrip menuS)
        {
            foreach (ToolStripMenuItem item in menuS.Items)
            {
                TreeNode root = new TreeNode();
                root.Text = item.Text;
                treeView1.Nodes.Add(root);
                if (item.HasDropDownItems)
                {
                    CreateTreeNode(item, root);
                }
            }
        }

        public void CreateTreeNode(ToolStripMenuItem item, TreeNode node)
        {
            foreach (ToolStripMenuItem it in item.DropDownItems)
            {
                TreeNode subNode = new TreeNode();
                subNode.Text = it.Text;
                node.Nodes.Add(subNode);
                if (it.HasDropDownItems)
                {
                    CreateTreeNode(it, subNode);
                }
            }
        }