以下是遞歸生成樹的代碼: 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); } } }