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 ToolStripMenuIt em, 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()
{
InitializeCompo nent();
}
private System.Windows. Forms.ToolStrip MenuItem tsItem1;
private System.Windows. Forms.ToolStrip MenuItem tsItem2;
private System.Windows. Forms.ToolStrip MenuItem tsItem3;
private System.Windows. Forms.ToolStrip MenuItem tsItem4;
private void menuClick(objec t sender, System.EventArg s e)
{
ToolStripMenuIt em oStrip = ((ToolStripMenu Item)(sender));
this.label1.Tex t = "My parent is: "; //I want it to say
“My parent is Down Two Levels!!â€
this.label2.Tex t = "My grandparent is: "; //I want it to
say “My grandparent is Down One Level!!â€
}
private void button1_Click(o bject sender, EventArgs e)
{
this.tsItem1 = new System.Windows. Forms.ToolStrip MenuItem();
this.tsItem2 = new System.Windows. Forms.ToolStrip MenuItem();
this.tsItem3 = new System.Windows. Forms.ToolStrip MenuItem();
this.tsItem4 = new System.Windows. Forms.ToolStrip MenuItem();
this.tsItem1.Te xt = "Menu Strip Level";
this.tsItem2.Te xt = "Down One Level";
this.tsItem3.Te xt = "Down Two Levels";
this.tsItem4.Te xt = "Down Three Levels";
tsItem4.Click += new System.EventHan dler(menuClick) ;
this.menuStrip1 .Items.Add(tsIt em1);
this.tsItem1.Dr opDownItems.Add (tsItem2);
this.tsItem2.Dr opDownItems.Add (tsItem3);
this.tsItem3.Dr opDownItems.Add (tsItem4);
}
}
of a clicked menu item. With the MainMenu, it was easy to do using the
parent property of the MenuItem. With the ToolStripMenuIt em, 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()
{
InitializeCompo nent();
}
private System.Windows. Forms.ToolStrip MenuItem tsItem1;
private System.Windows. Forms.ToolStrip MenuItem tsItem2;
private System.Windows. Forms.ToolStrip MenuItem tsItem3;
private System.Windows. Forms.ToolStrip MenuItem tsItem4;
private void menuClick(objec t sender, System.EventArg s e)
{
ToolStripMenuIt em oStrip = ((ToolStripMenu Item)(sender));
this.label1.Tex t = "My parent is: "; //I want it to say
“My parent is Down Two Levels!!â€
this.label2.Tex t = "My grandparent is: "; //I want it to
say “My grandparent is Down One Level!!â€
}
private void button1_Click(o bject sender, EventArgs e)
{
this.tsItem1 = new System.Windows. Forms.ToolStrip MenuItem();
this.tsItem2 = new System.Windows. Forms.ToolStrip MenuItem();
this.tsItem3 = new System.Windows. Forms.ToolStrip MenuItem();
this.tsItem4 = new System.Windows. Forms.ToolStrip MenuItem();
this.tsItem1.Te xt = "Menu Strip Level";
this.tsItem2.Te xt = "Down One Level";
this.tsItem3.Te xt = "Down Two Levels";
this.tsItem4.Te xt = "Down Three Levels";
tsItem4.Click += new System.EventHan dler(menuClick) ;
this.menuStrip1 .Items.Add(tsIt em1);
this.tsItem1.Dr opDownItems.Add (tsItem2);
this.tsItem2.Dr opDownItems.Add (tsItem3);
this.tsItem3.Dr opDownItems.Add (tsItem4);
}
}
Comment