Q: 把MenuStrip的每一项复制到TreeView,选择TreeView的某一项效果和MenuStrip一样
A:
![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif)
Code
//调用CopyMenuToTree(menuStrip1.Items, treeView1.Nodes);
private void CopyMenuToTree(ToolStripItemCollection itemColl,TreeNodeCollection nodeColl)
{
foreach (ToolStripMenuItem item in itemColl)
{
TreeNode node = new TreeNode(item.Text);
node.Tag = item;
nodeColl.Add(node);
CopyMenuToTree(item.DropDownItems, node.Nodes);
}
}
//设置TreeView的AfterSelect事件
private void treeView1_AfterSelect(object sender, TreeViewEventArgs e)
{
ToolStripMenuItem item = e.Node.Tag as ToolStripMenuItem;
item.PerformClick();
}