Home > Mobile >  Dynamically make ToolStripItems for ContextMenuStrip in C#
Dynamically make ToolStripItems for ContextMenuStrip in C#

Time:12-13

I have 3 folders which contains a bunch of subfolders and files. I need to create a ContextMenuStrip with all the folders and files as ToolStripMenuItems (as a tree hierarchy). I know how to hardcode it:

    public class CustomApplicationContext : ApplicationContext
    {
        private NotifyIcon trayIcon;
        private ContextMenuStrip contextMenuStrip = new ContextMenuStrip();
        ToolStripMenuItem option1 = new ToolStripMenuItem("Option 1");
        ToolStripMenuItem option1a = new ToolStripMenuItem("Option 1a");
        ToolStripMenuItem option1b = new ToolStripMenuItem("Option 1b");
        ToolStripMenuItem option1c = new ToolStripMenuItem("Option 1c");
        ToolStripMenuItem option1ca = new ToolStripMenuItem("Option 1ca");
        ToolStripMenuItem option2 = new ToolStripMenuItem("Option 2");
        ToolStripMenuItem option3 = new ToolStripMenuItem("Option 3");
        ToolStripMenuItem option4 = new ToolStripMenuItem("Option 4");

        public CustomApplicationContext()
        {
            contextMenuStrip.Items.AddRange(new ToolStripItem[]
            {
                this.option1,
                this.option2,
                this.option3,
                this.option4
            });
            this.option1.DropDownItems.AddRange(new ToolStripItem[]
            {
                this.option1a,
                this.option1b,
                this.option1c
            });
            this.option1c.DropDownItems.AddRange(new ToolStripItem[] 
            {
                this.option1ca
            });
            trayIcon = new NotifyIcon()
            {
                Icon = Resources.AppIcon,
                ContextMenuStrip = contextMenuStrip,
                Visible = true
            };
        }
    }

However, I would like to make it more dynamically so if something change within one of the folders, it'll also be dynamically changed.

How would I be able to do that?

CodePudding user response:

It can be done with a recursive method that iterates through a directory tree recursively.

private static void AddMenuItemsRecusively(
    DirectoryInfo root, 
    ToolStripItemCollection itemCollection)
{
    FileInfo[] files = root.GetFiles("*.*");
    if (files != null) {
        foreach (FileInfo fi in files) {
            itemCollection.Add(new ToolStripMenuItem(fi.Name));
        }

        DirectoryInfo[] subDirs = root.GetDirectories();
        foreach (DirectoryInfo dirInfo in subDirs) {
            var menuItem = new ToolStripMenuItem(dirInfo.Name);
            itemCollection.Add(menuItem);
            AddMenuItemsRecusively(dirInfo, menuItem.DropDownItems);
        }
    }
}

Since we must be able to add menu items to a ContextMenuStrip.Items as well as to ToolStripMenuItem.DropDownItems, we pass a ToolStripItemCollection to the method.

We create the menu structure like this with baseDir as DirectoryInfo containing the base directory.

AddMenuItemsRecusively(baseDir, contextMenuStrip.Items);

Call contextMenuStrip.Items.Clear(); before adding the menu items if you want to populate the context menu repeatedly.

  • Related