将 ToolStripMenuItem 动态添加到 MenuStrip (C#/ Winforms)

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1757574/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-06 20:34:02  来源:igfitidea点击:

Dynamically adding ToolStripMenuItems to a MenuStrip (C#/ Winforms)

c#winformsmenuitem

提问by IbrarMumtaz

I have my solution implemented (basic solution) and I'm happy.

我已经实施了我的解决方案(基本解决方案),我很高兴。

Problem is when I add new items to a ToolStripItemCollection using the 'Add' method, I get a few overloads ... the meaningful one being a string parameter, an image parameter and an EventHandler parameter.

问题是当我使用“添加”方法向 ToolStripItemCollection 添加新项目时,我得到了一些重载……有意义的一个是字符串参数、图像参数和 EventHandler 参数。

Because my drop down list is going to be serving as a dynamic history at RunTime, means it going to be empty at compile time. This means I can't add an event handler through the standard route of using the designer surface (On click). I am forced to use the overload described above.

因为我的下拉列表将在运行时用作动态历史记录,这意味着它在编译时将为空。这意味着我无法通过使用设计器表面(点击时)的标准路线添加事件处理程序。我被迫使用上述重载。

I image is of no use to me but adding the event handler dynamically is what I am interested in and need help with.

我的形象对我没有用,但动态添加事件处理程序是我感兴趣并需要帮助的。

URL: http://msdn.microsoft.com/en-us/library/bxdt0s8t.aspx

网址:http: //msdn.microsoft.com/en-us/library/bxdt0s8t.aspx

There is no other overload to help me, so I have to use an Image ... anyone got any ideas to get around this and show me how to fully satisfy this overloaded version of the add method.

没有其他重载可以帮助我,所以我必须使用 Image ......任何人都有任何想法来解决这个问题并向我展示如何完全满足 add 方法的这个重载版本。

TIA.

TIA。

UPDATE: I re did this again in a current project but using more slicker code but the principle is the same, add event handlers dynamically at run time. I will update this with some sample code when I get home.

更新:我在当前项目中再次执行此操作,但使用了更流畅的代码,但原理相同,在运行时动态添加事件处理程序。我回家后会用一些示例代码更新它。

采纳答案by jasonh

The way I do it is to create an array of ToolStripMenuItemsand populate that array with the items I'm adding. I create one method to handle the click events and have it check something unique about each item I create at run-time. You might try using the Nameor Tagproperties of each ToolStripMenuItem. Then use AddRangeon the spot in the menu you're adding to. So your code might look something like this:

我这样做的方法是创建一个数组ToolStripMenuItems并用我添加的项目填充该数组。我创建了一种方法来处理点击事件,并让它检查我在运行时创建的每个项目的独特之处。您可以尝试使用每个的NameTag属性ToolStripMenuItem。然后AddRange在要添加到的菜单中当场使用。所以你的代码可能看起来像这样:

private void BuildMenuItems()
{
    ToolStripMenuItem[] items = new ToolStripMenuItem[2]; // You would obviously calculate this value at runtime
    for (int i = 0; i < items.Length; i++)
    {
        items[i] = new ToolStripMenuItem();
        items[i].Name = "dynamicItem" + i.ToString();
        items[i].Tag = "specialDataHere";
        items[i].Text = "Visible Menu Text Here";    
        items[i].Click += new EventHandler(MenuItemClickHandler);
    }

    myMenu.DropDownItems.AddRange(items);
}

private void MenuItemClickHandler(object sender, EventArgs e)
{
    ToolStripMenuItem clickedItem = (ToolStripMenuItem)sender;
    // Take some action based on the data in clickedItem
}

回答by Philip Wallace

What is wrong with:

出什么问题了:

ToolStripItem item = toolStripItems.Add("MyItem");
item.Click += new EventHandler(....);

Am I missing something?

我错过了什么吗?

回答by QuickDanger

I was having similar issues as Philip Wallace. It's important to note the difference between a ToolStripItem, and a ToolStripMenuItem. I was adding ToolStripItems to a ToolStripMenuItem's DropDownItems, and they would show up, and have all properties set correctly, and accessible in code, but they would not show any text! Switching to a ToolStripMenuItem solved this.

我和菲利普华莱士有类似的问题。请务必注意 ToolStripItem 和 ToolStripMenuItem 之间的区别。我将 ToolStripItems 添加到 ToolStripMenuItem 的 DropDownItems,它们会显示出来,并且所有属性设置正确,并且可以在代码中访问,但它们不会显示任何文本!切换到 ToolStripMenuItem 解决了这个问题。

In regards to the original question, I've been using the empty constructor, and setting the fields I needed. (I'm in vb.net with .net 4.0, and it won't let me call New ToolStripMenuItem()since it has a MustInherittag, so I made this class:

关于原始问题,我一直在使用空构造函数,并设置我需要的字段。(我在 vb.net 中使用 .net 4.0,它不会让我打电话,New ToolStripMenuItem()因为它有一个MustInherit标签,所以我做了这个类:

Public Class DynamicToolStripMenuItem
    Inherits ToolStripMenuItem

    Public Sub New(value As Integer, text As String, handler As System.EventHandler)
        MyBase.New()
        Me.Text = text
        Me.Visible = True
        Me.Tag = value
        AddHandler Me.Click, handler
    End Sub
End Class

回答by John

You can simply pass null for the image.

您可以简单地为图像传递 null 。

Menu.DropDownItems.Add(Text, null, EventHandler);