1

我想知道 SO 是否可以帮助简化一些逻辑。我有一个包含 System.Windows.Forms.MenuStrip 的 Windows 窗体 (C# 2.0)。

  1. 我想将 ToolStripMenueItems 动态添加到 MenuStrip。添加的项目将从数据库中删除(但为简单起见,我已从下面的代码中删除了该部分)。
  2. 我希望能够构建复杂的菜单(即工具>数学>计算、帮助>文档、帮助>关于、格式>编码>西方、格式>编码>其他>希腊)。

下面的代码似乎可以工作,但你会怎么做才能使 loadToolbars() 更高效/更简单?

这是我需要帮助的功能:

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

下面的一切,我很满意,但我提供它是为了帮助其他人跟随我正在做的事情。

此功能是数据驱动的,但为了 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;
}

自定义类以帮助保持我的点击事件简单

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

所有工具栏项的点击事件都将在此函数中处理

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

1 回答 1

1

问题在于您的数据所在的形式。如果您坚持这种形式,我认为没有什么可做的。

否则,将您的数据从“工具>Microsoft>Math”之类的平面字段结构更改为TreeList 之类的东西,例如包含Microsoft 列表的工具列表,其中包含包含您的应用程序条目的数学列表。您甚至可以在数据库中构建该结构。

然后您可以轻松地递归添加菜单项。

于 2009-10-12T19:22:02.847 回答