Merging Menus using class derived from MenuStrip

  • Thread starter Thread starter mfr
  • Start date Start date
M

mfr

I am having problems merging menus in an MDI application. The parent menu
contains 'open' and 'close' under the 'file' menu. The child menu should
append 'print' and 'preview' to the parent menu once the child form is
activated.

This does work correctly if a MenuStrip item is created in the parent form
using "MenuStrip pm = GetMenu1();" in the code below. However, if I create a
class that descends from MenuStrip to do the same then it does not work.

It appears that the menu in the parent window MUST be a ToolStrip class and
cannot be a class descended from ToolStrip. However, it does not seem to
matter how the menu is created in the child window.


Referring to the code below, every thing works fine if the menu in the
parent is created using GetMenu1() but does not work if hte menu is created
using pm = new ParentMenu;


Is this a bug, or am I doing something wrong???

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

namespace MergeMenuTest
{
public class Form1 : Form
{
public Form1()
{
this.SuspendLayout();
this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.ClientSize = new System.Drawing.Size(385, 358);
this.IsMdiContainer = true;
this.Name = "Form1";
this.Text = "Form1";
this.ResumeLayout(false);


// Creating a class that descends from MenuStrip does not work
ParentMenu pm = new ParentMenu();


// Creating a menustrip this way works
//MenuStrip pm = GetMenu1();

this.MainMenuStrip = pm;

this.Controls.Add(pm);


// Open child window
ChildForm2 c = new ChildForm2();
c.MdiParent = this;
c.Show();
}

/// <summary>
/// Creates the parent menu. This way works ok.
/// </summary>
/// <returns></returns>
MenuStrip GetMenu1()
{
MenuStrip ms = new MenuStrip();
ms.AllowMerge = true;

ToolStripItem[] ddi = { new ToolStripMenuItem("Open"), new
ToolStripMenuItem("Close") };
ToolStripMenuItem ts = new ToolStripMenuItem("File", null, ddi);

ms.Items.Add(ts);

return ms;
}
}



public partial class ChildForm2 : Form
{
public ChildForm2()
{
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.Text = "ChildForm";

ChildMenu ms = new ChildMenu();
this.Controls.Add(ms);


}
}



public class ChildMenu : MenuStrip
{
public ChildMenu()
{
AllowMerge = true;

ToolStripMenuItem mi1 = new ToolStripMenuItem("Print");
mi1.MergeAction = MergeAction.Append;

ToolStripMenuItem mi2 = new ToolStripMenuItem("Preview");
mi2.MergeAction = MergeAction.Append;


ToolStripMenuItem ts = new ToolStripMenuItem("File");
ts.MergeAction = MergeAction.MatchOnly;
ts.DropDown.Items.Add(mi1);
ts.DropDown.Items.Add(mi2);

Items.Add(ts);
Visible = false;
}

}


public class ParentMenu : MenuStrip
{
public ParentMenu()
{
AllowMerge = true;

ToolStripItem[] ddi = { new ToolStripMenuItem("Open"), new
ToolStripMenuItem("Close") };


ToolStripMenuItem ts = new ToolStripMenuItem("File", null, ddi);

this.Items.Add(ts);

}
}


static class Program
{
/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main()
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.Run(new Form1());
}
}
}
 
I should add that this is reproducable in VS2005 and VS2008. I'm targetting
the .Net 2.0.

mfr said:
I am having problems merging menus in an MDI application. The parent menu
contains 'open' and 'close' under the 'file' menu. The child menu should
append 'print' and 'preview' to the parent menu once the child form is
activated.

This does work correctly if a MenuStrip item is created in the parent form
using "MenuStrip pm = GetMenu1();" in the code below. However, if I create a
class that descends from MenuStrip to do the same then it does not work.

It appears that the menu in the parent window MUST be a ToolStrip class and
cannot be a class descended from ToolStrip. However, it does not seem to
matter how the menu is created in the child window.


Referring to the code below, every thing works fine if the menu in the
parent is created using GetMenu1() but does not work if hte menu is created
using pm = new ParentMenu;


Is this a bug, or am I doing something wrong???

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

namespace MergeMenuTest
{
public class Form1 : Form
{
public Form1()
{
this.SuspendLayout();
this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.ClientSize = new System.Drawing.Size(385, 358);
this.IsMdiContainer = true;
this.Name = "Form1";
this.Text = "Form1";
this.ResumeLayout(false);


// Creating a class that descends from MenuStrip does not work
ParentMenu pm = new ParentMenu();


// Creating a menustrip this way works
//MenuStrip pm = GetMenu1();

this.MainMenuStrip = pm;

this.Controls.Add(pm);


// Open child window
ChildForm2 c = new ChildForm2();
c.MdiParent = this;
c.Show();
}

/// <summary>
/// Creates the parent menu. This way works ok.
/// </summary>
/// <returns></returns>
MenuStrip GetMenu1()
{
MenuStrip ms = new MenuStrip();
ms.AllowMerge = true;

ToolStripItem[] ddi = { new ToolStripMenuItem("Open"), new
ToolStripMenuItem("Close") };
ToolStripMenuItem ts = new ToolStripMenuItem("File", null, ddi);

ms.Items.Add(ts);

return ms;
}
}



public partial class ChildForm2 : Form
{
public ChildForm2()
{
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.Text = "ChildForm";

ChildMenu ms = new ChildMenu();
this.Controls.Add(ms);


}
}



public class ChildMenu : MenuStrip
{
public ChildMenu()
{
AllowMerge = true;

ToolStripMenuItem mi1 = new ToolStripMenuItem("Print");
mi1.MergeAction = MergeAction.Append;

ToolStripMenuItem mi2 = new ToolStripMenuItem("Preview");
mi2.MergeAction = MergeAction.Append;


ToolStripMenuItem ts = new ToolStripMenuItem("File");
ts.MergeAction = MergeAction.MatchOnly;
ts.DropDown.Items.Add(mi1);
ts.DropDown.Items.Add(mi2);

Items.Add(ts);
Visible = false;
}

}


public class ParentMenu : MenuStrip
{
public ParentMenu()
{
AllowMerge = true;

ToolStripItem[] ddi = { new ToolStripMenuItem("Open"), new
ToolStripMenuItem("Close") };


ToolStripMenuItem ts = new ToolStripMenuItem("File", null, ddi);

this.Items.Add(ts);

}
}


static class Program
{
/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main()
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.Run(new Form1());
}
}
}
 
Hi,

I performed a test based on your sample code and did reproduce the problem
on my side. When the MDI parent uses the ParentMenu and the MDI child form
uses the ChildMenu, the menu in the MDI child form isn't merged into that
in the MDI parent. However, when the MDI parent uses the MenuStrip and the
MDI child form uses the ChildMenu, the menu in the MDI child form can be
merged.

Beside, I have the following two findings:

If both the MDI parent and the MDI child form use the MenuStrip, the menu
in the MDI can be merged into that in the MDI parent;

If the MDI parent uses the ParentMenu and the MDI child form uses the
MenuStrip, the menu in the MDI child form cannot be merged into that in the
MDI parent.

From the above results we can draw a conclusion, i.e if the menu within an
MDI child form is the same type or a derived type of that within its MDI
parent, the menu in the MDI child form can be merged into that in the MDI
parent; otherwise, the menu in the MDI child form cannot be merged into the
menu in the MDI parent.

I think this behavior is by design.

If you have any question, please feel free to let me know.

Sincerely,
Linda Liu
Microsoft Online Community Support

==================================================
Get notification to my posts through email? Please refer to
http://msdn.microsoft.com/subscriptions/managednewsgroups/default.aspx#notif
ications.

Note: The MSDN Managed Newsgroup support offering is for non-urgent issues
where an initial response from the community or a Microsoft Support
Engineer within 1 business day is acceptable. Please note that each follow
up response may take approximately 2 business days as the support
professional working with you may need further investigation to reach the
most efficient resolution. The offering is not appropriate for situations
that require urgent, real-time or phone-based interactions or complex
project analysis and dump analysis issues. Issues of this nature are best
handled working with a dedicated Microsoft Support Engineer by contacting
Microsoft Customer Support Services (CSS) at
http://msdn.microsoft.com/subscriptions/support/default.aspx.
==================================================

This posting is provided "AS IS" with no warranties, and confers no rights.
 
I've now tried deriving the child menu from the parent menu and the merging
now works correctly.

Thank you for you comments.
 
Back
Top