如何东西添加到编程的MenuStrip? [英] How to add things to a menustrip programatically?

查看:300
本文介绍了如何东西添加到编程的MenuStrip?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我要添加无论是写在一个文本框到的MenuStrip。在文件>最近搜索的东西我有。

I want to add whatever is written in a textbox to a menustrip. In the File > Recent Searches thing I have.

我可怎么办编程? ?而且我可以动态地分配事件处理程序,以便当用户单击X上的项目在该子文件夹,文本BACK复制到文本框

How can I do programatically? And can I assign an event handler dynamically so that when a user clicks on X item in that subfolder, the text is copied BACK to the textbox?

编辑:我如何编程文件夹Busquedas Recientes(以PIC)

推荐答案

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

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

这篇关于如何东西添加到编程的MenuStrip?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆