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());
}
}
}
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());
}
}
}