C# Dynamisch hinzufügen ToolStripMenueItems zu MenuStrip

Ich Frage mich, wenn das SO ist könnte helfen, vereinfachen einige Logik. Ich habe eine windows form (C# 2.0), die enthält eine System.Windows.Formen.MenuStrip.

  1. Ich möchte dynamisch hinzufügen
    ToolStripMenueItems der
    MenuStrip. Die hinzugefügten Elemente werden
    angetrieben aus einer Datenbank (aber für
    Einfachheit halber habe ich entfernt, das Teil
    aus dem code unten).
  2. Ich möchte in der Lage sein zu bauen
    komplexe Menüs (ich.e Tools>Mathematik>Calc,
    Hilfe - >Dokumente, Hilfe - >Etwa,
    Format>Codierung>Western,
    Format>Codierung>Andere>Griechisch).

Sie den code unten scheint zu funktionieren, aber was würde Sie tun, um loadToolbars() effizienter/einfacher?

Dies ist die Funktion, die ich brauche Hilfe bei:

void loadToolbars()
{
    foreach(Toolbar t in getToolStripItems())
    {
        string[] toolPath = t.toolbar.Split(">".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<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>0)
            {
                foreach(ToolStripItem item in foundItems)
                {
                    //Is this the Toolbar item I am looking for?
                    if(item.Text == toolPath[i])
                    {
                        if(item.OwnerItem != null && i>0)
                        {
                            if((item.OwnerItem.Text == toolPath[i-1]) 
                                && (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);
    }
}

Alles, was unten ist, ich bin zufrieden mit, aber ich liefere es, anderen zu helfen, das zu befolgen, was ich Tue.

Diese Funktion ist Daten-getrieben, sondern hart codiert für das nutzen von SO

private List<Toolbar> getToolStripItems()
{
   List<Toolbar>toolbars = new List<Toolbar>();

   Toolbar t = new Toolbar();
   t.Text = "Calc";
   t.path = "c:\windows\system32\calc.exe";
   t.toolbar = "Tools>Microsoft>Math";

   toolbars.Add(t);

   t = new Toolbar()
   t.Text = "Calc2";
   t.path = "c:\windows\system32\calc.exe";
   t.toolbar = "Tools>Math>Microsoft";

   toolbars.Add(t);

   return toolbars;
}

Benutzerdefinierte Klasse, um zu helfen, meine click-Ereignisse einfache

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;
    }
}

Alle Toolbar-Element, Klicken Sie auf Ereignisse behandelt werden, die in dieser Funktion

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();
}

InformationsquelleAutor J.Hendrix | 2009-10-12

Schreibe einen Kommentar