C# Winforms Dynamic menu entries

Joran Stoops picture Joran Stoops · Jul 28, 2014 · Viewed 7.2k times · Source

I'm attempting to create a dynamic menu strip in my program. Here's an example of what it looks like right now:

Image

Creating the ToolstripMenuItems dynamically itself is easy. What I plan to do is to check if the current month already exists in the folder paths that my program works with, and if it doesn't then it will create an extra menu strip with the date (for example once we hit August, it should create August 2014, with sub-items "NL" & "PL").

However the part that i am stuck at is how to link functionality to these dynamically created sub-items. As I've been unable to find a way to do this, both the "NL" and "PL" tabs open a specific .TXT file of that specific month (which is created elsewhere in the program). However when I make them dynamically, I can't find a way to make them do this and they just don't have any functionality.

At this point I find myself manually creating new menu items & code every month for this. And I would very much prefer the program to run itself.

Any tips on how to make dynamic menuitems functional?

Added code:

    private void CreateMenu()
    {
        ToolStripMenuItem item = new ToolStripMenuItem();
        item.Text = "Logs";
        DirectoryInfo dir = new DirectoryInfo(@"Y:\Heineken\Tools\Logs\");

        foreach (DirectoryInfo directory in dir.GetDirectories())
        {
            ToolStripMenuItem dateItem = new ToolStripMenuItem(directory.Name);
            ToolStripMenuItem NLMenu = new ToolStripMenuItem("NL"); // <--- This needs to open a specific text file on a network share
            ToolStripMenuItem PLMenu = new ToolStripMenuItem("PL"); // <--- This needs to open a specific text file on a network share
            dateItem.DropDownItems.Add(NLMenu);
            dateItem.DropDownItems.Add(PLMenu);
            item.DropDownItems.Add(dateItem);
        }
        menuToolStripMenuItem.DropDownItems.Add(item);

    }

Answer

glopes picture glopes · Jul 28, 2014

It would help if you would post some code snippet of how you are dynamically creating your menu items. In general, you can link functionality to dynamic menu entries by simply passing a delegate into the ToolStripMenuItem constructor, like so:

var entry = new ToolStripMenuItem("NL", null, delegate
{
    //TODO: do something
});

owner.DropDownItems.Add(entry);

This assumes the variable "owner" is your parent menu entry.

Edit: Given the code you supplied, you could do it like this

private void OpenTextFile(string id)
{
    //TODO: logic for opening the shared file
}

private void CreateMenu()
{
    ToolStripMenuItem item = new ToolStripMenuItem();
    item.Text = "Logs";
    DirectoryInfo dir = new DirectoryInfo(@"Y:\Heineken\Tools\Logs\");

    foreach (DirectoryInfo directory in dir.GetDirectories())
    {
        ToolStripMenuItem dateItem = new ToolStripMenuItem(directory.Name);
        ToolStripMenuItem NLMenu = new ToolStripMenuItem("NL", null, (sender, e) => OpenTextFile("NL"));
        ToolStripMenuItem PLMenu = new ToolStripMenuItem("PL", null, (sender, e) => OpenTextFile("PL"));
        dateItem.DropDownItems.Add(NLMenu);
        dateItem.DropDownItems.Add(PLMenu);
        item.DropDownItems.Add(dateItem);
    }
    menuToolStripMenuItem.DropDownItems.Add(item);

}