Please excuse my terminoligy, as I'm not certain of it in regards winforms components.
I have a ContextMenuStrip on my form and in it there are some SubItems created manually. I need to add more to SubItem dynamically.
I'm unsure if I'm saying this correctly so I'll post a screen shot.
http://s23.postimg.org/3riz5ovtn/daniweb_Context1.jpg
I need to add items underneath "default", for example when the button is pressed. I know how to add a Cat2, but cannot get my head around adding to the sub item.
I have included basic code where only the contextmenustrip and button are present.

Thank you kindly for taking time to look in.

Form1.Designer.cs

namespace ContextMenuStrip
{
    partial class Form1
    {
        /// <summary>
        /// Required designer variable.
        /// </summary>
        private System.ComponentModel.IContainer components = null;

        /// <summary>
        /// Clean up any resources being used.
        /// </summary>
        /// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
        protected override void Dispose(bool disposing)
        {
            if (disposing && (components != null))
            {
                components.Dispose();
            }
            base.Dispose(disposing);
        }

        #region Windows Form Designer generated code

        /// <summary>
        /// Required method for Designer support - do not modify
        /// the contents of this method with the code editor.
        /// </summary>
        private void InitializeComponent()
        {
            this.components = new System.ComponentModel.Container();
            this.contextMenuStrip1 = new System.Windows.Forms.ContextMenuStrip(this.components);
            this.cat1ToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem();
            this.defaultToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem();
            this.button1 = new System.Windows.Forms.Button();
            this.contextMenuStrip1.SuspendLayout();
            this.SuspendLayout();
            // 
            // contextMenuStrip1
            // 
            this.contextMenuStrip1.Items.AddRange(new System.Windows.Forms.ToolStripItem[] {
            this.cat1ToolStripMenuItem});
            this.contextMenuStrip1.Name = "contextMenuStrip1";
            this.contextMenuStrip1.Size = new System.Drawing.Size(99, 26);
            // 
            // cat1ToolStripMenuItem
            // 
            this.cat1ToolStripMenuItem.DropDownItems.AddRange(new System.Windows.Forms.ToolStripItem[] {
            this.defaultToolStripMenuItem});
            this.cat1ToolStripMenuItem.Name = "cat1ToolStripMenuItem";
            this.cat1ToolStripMenuItem.Size = new System.Drawing.Size(98, 22);
            this.cat1ToolStripMenuItem.Text = "Cat1";
            // 
            // defaultToolStripMenuItem
            // 
            this.defaultToolStripMenuItem.Name = "defaultToolStripMenuItem";
            this.defaultToolStripMenuItem.Size = new System.Drawing.Size(152, 22);
            this.defaultToolStripMenuItem.Text = "default";
            // 
            // button1
            // 
            this.button1.Location = new System.Drawing.Point(13, 13);
            this.button1.Name = "button1";
            this.button1.Size = new System.Drawing.Size(75, 23);
            this.button1.TabIndex = 1;
            this.button1.Text = "button1";
            this.button1.UseVisualStyleBackColor = true;
            this.button1.Click += new System.EventHandler(this.button1_Click);
            // 
            // Form1
            // 
            this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
            this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
            this.ClientSize = new System.Drawing.Size(284, 262);
            this.Controls.Add(this.button1);
            this.Name = "Form1";
            this.Text = "Form1";
            this.contextMenuStrip1.ResumeLayout(false);
            this.ResumeLayout(false);

        }

        #endregion

        private System.Windows.Forms.ContextMenuStrip contextMenuStrip1;
        private System.Windows.Forms.ToolStripMenuItem cat1ToolStripMenuItem;
        private System.Windows.Forms.ToolStripMenuItem defaultToolStripMenuItem;
        private System.Windows.Forms.Button button1;
    }
}

Form1.cs

using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace ContextMenuStrip
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            contextMenuStrip1.Items.Add("Cat2");
            // Add some Items to Cat1 sub items here
            contextMenuStrip1.Show();
        }
    }
}

I'll also need to remove the new items, but I'm pretty sure once I get how to add them, I will understand that.

As is sometimes the case, I've discovered how to do it after asking, I swear I looked for a couple of hours first.
Unfortunately, removing them does not appear to be so simple, and I wind up with errors.
Here's how I add.

using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace ContextMenuStrip
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            contextMenuStrip1.Items.Add("Cat2");
            cat1ToolStripMenuItem.DropDownItems.Add("New");
            contextMenuStrip1.Show();
        }
    }
}

And here are my attempts to remove.

private void button_Remove_Click(object sender, EventArgs e)
        {
            ToolStripMenuItem tmi = null;
            //ToolStripMenuItem tmi = (ToolStripMenuItem)cat1ToolStripMenuItem.DropDownItems.Find("New", false)[1];
            foreach (ToolStripMenuItem itm in cat1ToolStripMenuItem.DropDownItems)
            {
                if (itm.Text == "New")
                {
                    tmi = itm;
                    break;
                }
            }
            if (tmi != null)
            {
                cat1ToolStripMenuItem.DropDownItems.Remove(tmi);
            }
            //cat1ToolStripMenuItem.DropDownItems.Remove(cat1ToolStripMenuItem.DropDownItems.Find("New", false)[1]);
            contextMenuStrip1.Show();
        }

The uncommented actually works but I'm hoping there might be a better, cleaner way.

My qusetion is all over the place and I apologise for that, sorry.
What I'm basically looking for here is this...

Add ToolstripMenuItems dynamically // done
When one of those items is clicked, read the text and remove it from the list // not done

Thank you again for your patience.

Ok, got it.
Thanks for reading and sorry if I wasted anyones time.

private void cat1ToolStripMenuItem_DropDownItemClicked(object sender, ToolStripItemClickedEventArgs e)
        {
            Debug.WriteLine(e.ClickedItem.Text);
            cat1ToolStripMenuItem.DropDownItems.Remove(e.ClickedItem);
        }

You might also have a look here a newly posted snippet.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.