none
Traverse ToolStripMenuItem ? RRS feed

  • 問題

  • Hello,

    How can I retrieve all the ToolStripMenuItem in a winform ?

    For example, I want to show thems in a treeview:

    A
      - a1
      - a2
      - a3
          - a31
          - a32

    B
      - b1
      - b2


    The above is the menu structure in a form, how can I retrieve them and present them in a treeview ? Please help.

    Best regards,
    Eric


    2007年5月17日 下午 01:23

所有回覆

  • Try this.

    private void Form1_Load(object sender, EventArgs e)
    {
        buildTree(menuStrip1.Items, treeView1.Nodes);
    }

    private void buildTree(ToolStripItemCollection ms, TreeNodeCollection pnodes)
    {
        foreach(ToolStripMenuItem m in ms)
        {
             buildTree(m.DropDownItems, pnodes.Add(m.Text).Nodes);
         }
    }

    2007年5月17日 下午 06:26