如何以编程方式将内容添加到菜单条?

发布于 2024-08-08 19:30:53 字数 248 浏览 3 评论 0原文

我想将文本框中写入的任何内容添加到菜单条中。在文件>我最近的搜索。

我怎样才能以编程方式进行?我是否可以动态分配一个事件处理程序,以便当用户单击该子文件夹中的 X 项目时,文本将复制回文本框?

编辑:如何以编程方式调用文件夹 Busquedas Recientes(如图)

alt text

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

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?

EDIT: How can I programatically call on the folder Busquedas Recientes (in pic)

alt text

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

难忘№最初的完美 2024-08-15 19:30:53

您可以通过利用事件处理程序中的object sender 参数来实现此目的。其中大部分内容都在我的脑海中,所以我只是猜测它会编译,但它应该可以帮助您入门。

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

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);
}
淤浪 2024-08-15 19:30:53

这相当简单。您可以执行以下操作:

ToolStripMenuItem menuItem

foreach (string text in collectionOfText)
{
    ToolStripMenuItem foo = new ToolStripMenuItem(text);
    foo.Click += new EventHandler(ClickEvent);
    menuItem.DropDownItems.Add(foo);
}

随后,如果 Click 事件不起作用(我遇到了无法检测到正确菜单项的问题),您可以向 menuItem 添加“DropDownItemClicked”事件。并获取您单击的项目的文本:

private void DropedDownItemClickedEvent(object sender, ToolStripItemClickedEventArgs e)
{
    string text = e.ClickedItem.Text;
}

我希望有帮助。

哦,别忘了也删除事件。我忘记对我创建的所有动态菜单执行此操作,结果不知何故耗尽了我一半的内存。 :D

It's rather straight forward. You can do the following:

ToolStripMenuItem menuItem

foreach (string text in collectionOfText)
{
    ToolStripMenuItem foo = new ToolStripMenuItem(text);
    foo.Click += new EventHandler(ClickEvent);
    menuItem.DropDownItems.Add(foo);
}

Subsequently, if the Click event doesn't work (I had trouble where it wouldn't detect the correct menu item), you can add a "DropDownItemClicked" event to the menuItem. and to get the text of the item you clicked you do:

private void DropedDownItemClickedEvent(object sender, ToolStripItemClickedEventArgs e)
{
    string text = e.ClickedItem.Text;
}

I hope that helps.

Oh and don't forget to remove the Event as well. I forgot to do that with all the dynamic menus I had created and somehow ended up eating half my memory. :D

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文