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

发布于 2024-08-11 21:12:05 字数 601 浏览 9 评论 0原文

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

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

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

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

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

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

TIA。

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

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.

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

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.

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.

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

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

发布评论

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

评论(5

何止钟意 2024-08-18 21:12:05

我的方法是创建一个 ToolStripMenuItems 数组,并用我要添加的项目填充该数组。我创建了一种方法来处理单击事件,并让它检查我在运行时创建的每个项目的独特之处。您可以尝试使用每个 ToolStripMenuItemNameTag 属性。然后在要添加的菜单中使用 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
}

The way I do it is to create an array of ToolStripMenuItems and 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 Name or Tag properties of each ToolStripMenuItem. Then use AddRange on the spot in the menu you're adding to. So your code might look something like this:

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
}
夜司空 2024-08-18 21:12:05

有什么问题:

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

我错过了什么吗?

What is wrong with:

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

Am I missing something?

只涨不跌 2024-08-18 21:12:05

您只需为图像传递 null 即可。

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

You can simply pass null for the image.

Menu.DropDownItems.Add(Text, null, EventHandler);
薄荷梦 2024-08-18 21:12:05

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

关于最初的问题,我一直在使用空构造函数,并设置我需要的字段。 (我在使用 .net 4.0 的 vb.net 中,它不会让我调用 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

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.

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 MustInherit tag, so I made this class:

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
情绪少女 2024-08-18 21:12:05
oMenusSiscon = clsLNUsuario.ObtenerMenu_v2(FrameworkFunctions.USER.Perfil);
bool exists = oMenusSiscon.AsEnumerable().Where(dr => dr.Field<Int32>("Menu_Opcion") == 1).Count() > 0;
DataTable Despacho = oMenusSiscon.AsEnumerable().Where(x => x.Field<Int32>("Menu_Opcion") == 1).Select(x => x).CopyToDataTable();
ToolStripMenuItem oMenuItem = new ToolStripMenuItem();
ToolStripMenuItem itemsSub;
if (exists == true)
{
    DataTable Menu = Despacho.AsEnumerable().Where(x => x.Field<Int32>("Menu_Orden") == 0).Select(x => x).CopyToDataTable();
    oMenuItem.Text = Menu.Rows[0][2].ToString();
    DataTable SubMenu = Despacho.AsEnumerable().Where(x => x.Field<Int32>("Menu_Orden") != 0).Select(x => x).CopyToDataTable();
    for (int i = 0; i < SubMenu.Rows.Count; i++)
    {
        itemsSub = new ToolStripMenuItem();
        itemsSub.Name = SubMenu.Rows[i][4].ToString();
        itemsSub.Enabled = SubMenu.Rows[i][5].ToString() == "A" ? true : false;
        itemsSub.Text = SubMenu.Rows[i][3].ToString();
        itemsSub.Click += new EventHandler(submenu_selected);
        oMenuItem.DropDownItems.Add(itemsSub);

    }

    //myMenu.DropDownItems.AddRange(items);
    menuStrip.Items.Add(oMenuItem);
oMenusSiscon = clsLNUsuario.ObtenerMenu_v2(FrameworkFunctions.USER.Perfil);
bool exists = oMenusSiscon.AsEnumerable().Where(dr => dr.Field<Int32>("Menu_Opcion") == 1).Count() > 0;
DataTable Despacho = oMenusSiscon.AsEnumerable().Where(x => x.Field<Int32>("Menu_Opcion") == 1).Select(x => x).CopyToDataTable();
ToolStripMenuItem oMenuItem = new ToolStripMenuItem();
ToolStripMenuItem itemsSub;
if (exists == true)
{
    DataTable Menu = Despacho.AsEnumerable().Where(x => x.Field<Int32>("Menu_Orden") == 0).Select(x => x).CopyToDataTable();
    oMenuItem.Text = Menu.Rows[0][2].ToString();
    DataTable SubMenu = Despacho.AsEnumerable().Where(x => x.Field<Int32>("Menu_Orden") != 0).Select(x => x).CopyToDataTable();
    for (int i = 0; i < SubMenu.Rows.Count; i++)
    {
        itemsSub = new ToolStripMenuItem();
        itemsSub.Name = SubMenu.Rows[i][4].ToString();
        itemsSub.Enabled = SubMenu.Rows[i][5].ToString() == "A" ? true : false;
        itemsSub.Text = SubMenu.Rows[i][3].ToString();
        itemsSub.Click += new EventHandler(submenu_selected);
        oMenuItem.DropDownItems.Add(itemsSub);

    }

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