title: C# Dynamically add ToolStripMenueItems to MenuStrip
body: <p>I was wondering if SO could help simplify some logic.  I have a windows form (C# 2.0) which contains a System.Windows.Forms.MenuStrip.  </p>
  
  <ol>
  <li>I want to dynamically add
  ToolStripMenueItems to the
  MenuStrip.  The added items will be
  driven off of a database (but for
  simplicity I have removed that part
  from the code below).</li>
  <li>I would like to be able to build
  complex menus (i.e Tools>Math>Calc,
  Help>Documents, Help>About, 
  Format>Encoding>Western,
  Format>Encoding>Other>Greek).</li>
  </ol>
  
  <p><strong>The code below seems to work but what would you do to make loadToolbars() more efficient/simpler?</strong></p>
  
  <p>This is the function I need help with:</p>
  
  <pre><code>void loadToolbars()
  {
      foreach(Toolbar t in getToolStripItems())
      {
          string[] toolPath = t.toolbar.Split("&gt;".ToCharArray(),StringSplitOptions.RemoveEmptyEntries);
          ToolStripMenuItem root = null;
          ToolStripItem[] foundItems;
  
          /*
           * follow the path of each Toolbar item.  If we find a dead-end,
           * add the missing part
           */
          for(int i=0; i&lt;toolPath.Length; i++)
          {
              if(root == null)
              {
                  //Search the main menu strip (System.Windows.Forms.MenuStrip)
                  foundItems = DA_Menu.Items.Find(toolPath[i],false);
              }else
              {
                  //Continue searching were we left off
                  foundItems = root.DropDownItems.Find(toolPath[i],false);
              }
  
              if(foundItems.Length&gt;0)
              {
                  foreach(ToolStripItem item in foundItems)
                  {
                      //Is this the Toolbar item I am looking for?
                      if(item.Text == toolPath[i])
                      {
                          if(item.OwnerItem != null &amp;&amp; i&gt;0)
                          {
                              if((item.OwnerItem.Text == toolPath[i-1]) 
                                  &amp;&amp; (item.Text == toolPath[i]))
                                  root = (ToolStripMenuItem)item;
                          }else
                          {
                              root = (ToolStripMenuItem)item;
                          }
                      }
                  }
              }else
              {
                  //We hit a dead-end.  Add the missing path
                  if(root == null)
                  {
                      root = new ToolStripMenuItem(toolPath[i]);
                      root.Name = toolPath[i];
                      DA_Menu.Items.Add(root);
                  }else
                  {
                      ToolStripMenuItem tsmi = new ToolStripMenuItem(toolPath[i]);
                      tsmi.Name = toolPath[i];
                      root.DropDownItems.Add(tsmi);
                      root = tsmi;
                  }
              }
          }
  
          //Add the Toobar item to the path that was built above
          t.Click +=new EventHandler(Toolbar_Click);
          ((ToolStripMenuItem)root).DropDownItems.Add(t);
      }
  }
  </code></pre>
  
  <p>Everthing below, I am happy with but I am providing it to help others follow what I am doing.</p>
  
  <p>This function is data driven but hard coded for the benefit of SO</p>
  
  <pre><code>private List&lt;Toolbar&gt; getToolStripItems()
  {
     List&lt;Toolbar&gt;toolbars = new List&lt;Toolbar&gt;();
  
     Toolbar t = new Toolbar();
     t.Text = "Calc";
     t.path = "c:\windows\system32\calc.exe";
     t.toolbar = "Tools&gt;Microsoft&gt;Math";
  
     toolbars.Add(t);
  
     t = new Toolbar()
     t.Text = "Calc2";
     t.path = "c:\windows\system32\calc.exe";
     t.toolbar = "Tools&gt;Math&gt;Microsoft";
  
     toolbars.Add(t);
  
     return toolbars;
  }
  </code></pre>
  
  <p>Custom class to help keep my click events simple</p>
  
  <pre><code>class Toolbar:ToolStripMenuItem
  {
      public string path;
      public string toolbar;
      public Toolbar()
      {
          /*
           * Set the name to the Text value
           * so that it can be found in collection
           * by key
           */
          base.Name = Text;
      }
  }
  </code></pre>
  
  <p>All Toolbar item Click events will be handled in this function</p>
  
  <pre><code>void Toolbar_Click(object sender, EventArgs e)
  {
      //Get the Toolbar item that was clicked
      Toolbar t = (Toolbar)sender;
  
      //Start new process
      System.Diagnostics.Process p = new System.Diagnostics.Process();
      p.StartInfo.FileName = t.path;
      p.Start();
  }
  </code></pre>