Joran Stoops
Joran Stoops

Reputation: 163

C# Winforms Dynamic menu entries

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

    }

Upvotes: 3

Views: 8963

Answers (2)

glopes
glopes

Reputation: 4380

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

}

Upvotes: 2

petelids
petelids

Reputation: 12815

You need to hook up the click event handler to the dynamically created ToolstripMenuItems. In the click event handler you can then cast the sender to ToolstripMenuItem and access any properties you may need to work out which text file you need to load.

For example this click handler will give a message box with the text of the menu item:

private void toolStripMenuItem_Click(object sender, EventArgs e)
{
    MessageBox.Show(((ToolStripMenuItem)sender).Text);
}

To add a new dynamic menu item and link it to the click handler you can do this:

ToolStripMenuItem newItem = new ToolStripMenuItem("Dynamic Option");
newItem.Click += toolStripMenuItem_Click;

existingToolStripMenuItem.DropDownItems.Add(newItem);

Edit after code posted

You can store the file that is supposed to be opened when the menu item is clicked in the Tag property. When the menu item is then clicked you can open the file based on the path in the Tag.

For example, in your CreateMenu method you would need to do something like this:

ToolStripMenuItem NLMenu = new ToolStripMenuItem("NL");
//store the filename here for later
NLMenu.Tag = Path.Combine(dir.FullName, "nl.txt");
//attach the click handler
NLMenu.Click += toolStripMenuItem_Click;
//repeat for PLMenu...

Then in the click handler you can do this:

private void toolStripMenuItem_Click(object sender, EventArgs e)
{
    string filename = ((ToolStripMenuItem)sender).Tag;
    //do something with the file.
}

Upvotes: 0

Related Questions