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.