Working with the MenuStrip


G

Guest

I am converting from MainMenu to MenuStrip And I want to list the hierarchy
of a clicked menu item. With the MainMenu, it was easy to do using the
parent property of the MenuItem. With the ToolStripMenuItem, it no longer
works.

To better understand my need, please create a VS form and add a MenuStrip,
button and two labels to it with the default names then add the code shown
below to the form. What I need is code in the menuClick method that adds the
names of the parent and grandparent of the clicked menu item (when clicking
the last item in the menu hierarchy). This should be simple to do but my
searches on the Web and experiments with the GetParent() method and parent
property were fruitless. What am I missing?

{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private System.Windows.Forms.ToolStripMenuItem tsItem1;
private System.Windows.Forms.ToolStripMenuItem tsItem2;
private System.Windows.Forms.ToolStripMenuItem tsItem3;
private System.Windows.Forms.ToolStripMenuItem tsItem4;

private void menuClick(object sender, System.EventArgs e)
{
ToolStripMenuItem oStrip = ((ToolStripMenuItem)(sender));
this.label1.Text = "My parent is: "; //I want it to say
“My parent is Down Two Levels!!â€

this.label2.Text = "My grandparent is: "; //I want it to
say “My grandparent is Down One Level!!â€
}

private void button1_Click(object sender, EventArgs e)
{
this.tsItem1 = new System.Windows.Forms.ToolStripMenuItem();
this.tsItem2 = new System.Windows.Forms.ToolStripMenuItem();
this.tsItem3 = new System.Windows.Forms.ToolStripMenuItem();
this.tsItem4 = new System.Windows.Forms.ToolStripMenuItem();
this.tsItem1.Text = "Menu Strip Level";
this.tsItem2.Text = "Down One Level";
this.tsItem3.Text = "Down Two Levels";
this.tsItem4.Text = "Down Three Levels";
tsItem4.Click += new System.EventHandler(menuClick);
this.menuStrip1.Items.Add(tsItem1);
this.tsItem1.DropDownItems.Add(tsItem2);
this.tsItem2.DropDownItems.Add(tsItem3);
this.tsItem3.DropDownItems.Add(tsItem4);
}
}
 
Ad

Advertisements


Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top