且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何东西添加到编程的MenuStrip?

更新时间:2023-12-02 23:49:22

您可以通过利用对象发件人的做到这一点参数的事件处理程序。这其中大部分是从我的头顶,所以我只的猜测的,它会编译,但它应该让你开始。

You can do this by taking advantage of the object sender parameter in the event handler. Most of this is off the top of my head so I'm only guessing that it will compile but it should get you started.

void AddMenuItem(string text, string action)
{
   ToolStripMenuItem item = new ToolStripMenuItem();
   item.Text = text;
   item.Click += new EventHandler(item_Click);
   item.Tag = action;

   //first option, inserts at the top
   //historyMenu.Items.Add(item);

   //second option, should insert at the end
   historyMenuItem.DropDownItems.Insert(historyMenuItem.DropDownItems.Count, item);
}

private void someHistoryMenuItem_Click(object sender, EventArgs e)
{
   ToolStripMenuItem menuItem = sender as ToolStripMenuItem;

   string args = menuItem.Tag.ToString();

   YourSpecialAction(args);
}