简体   繁体   中英

C# Creating ContextMenuStrip Sub Menus and items Dynamically

I have a contextMenuStrip that generated dynamical form data in a database. The data in the database has a group column and i want to use it to create Sub Menu's dynamically for the application. My problem is that it creates duplicate sub menu items and i want it grouped.

this is my List and Constructor.

List<Tuple<WebLinks>> WebLinksList = new List<Tuple<WebLinks>>();

class WebLinks
{
    public string Name { get; set; }
    public string Link { get; set; }
    public string Group { get; set; }
}

So far this is the working code to build the context menu but I need this to change to the Group view and not just show the Name.

private void CreateMenu()
{

    foreach (Tuple<WebLinks> wl in WebLinksList)
    {
        contextMenuStripMain.Items.Add(wl.Item1.Name);
    }
    contextMenuStripMain.Items.Add("-");
    contextMenuStripMain.Items.Add("Settings");
    contextMenuStripMain.Items.Add("Exit");
}

The above works fine but nothing is grouped, so i need to work in the "wl.Item1.Group" in somewhere but google was no help in my situation.

I tired This Stack Overflow and it gave me the duplicate Groups where i tried to filter it but with no luck. Hope I made sense.

You can use LINQ to group your objects:

private void CreateMenu()
{
    foreach (var group in WebLinksList.GroupBy(wl => wl.Item1.Group))
    {
        var groupItem = new ToolStripMenuItem(group.Key);
        contextMenuStripMain.Items.Add(groupItem);
        groupItem.DropDownItems.AddRange(group.Select(w => new ToolStripMenuItem(w.Item1.Name)).ToArray<ToolStripItem>());
    }

    contextMenuStripMain.Items.Add("-");
    contextMenuStripMain.Items.Add("Settings");
    contextMenuStripMain.Items.Add("Exit");
}

So for each distinct Group a single menu item will be added. To this item the sub items of this group are added by Name .

[EDIT] I now create groupItem as ToolStripMenuItem and add the sub items to the DropDownItems property.

Thanks René Vogt. I fixed if before I read your answer but desided to go with yours anyway.

This was your fix, i just added a click event for the sub menu's.

        foreach (var group in WebLinksList.GroupBy(wl => wl.Level))
        {
            var groupItem = new ToolStripMenuItem(group.Key);
            contextMenuStripMain.Items.Add(groupItem);
            groupItem.DropDownItemClicked += Tm_DropDownItemClicked;
            groupItem.DropDownItems.AddRange(group.Select(w => new ToolStripMenuItem(w.Name)).ToArray<ToolStripItem>());
        }

This was my Fix but it is the same.

        foreach (var group in WebLinksList.GroupBy(wl => wl.Level))
        {
            ToolStripMenuItem tm = new ToolStripMenuItem();
            tm.Text = group.Key;
            tm.DropDownItemClicked += Tm_DropDownItemClicked;
            tm.DropDownItems.AddRange(group.Select(w => new ToolStripMenuItem(w.Name)).ToArray<ToolStripItem>());
            contextMenuStripMain.Items.Add(tm);
        }

Anyway, I would not have done it without you. Thanks

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM