Multiple MainMenu objects with some MenuItem objects shared

P

polocar

Hi,
I'm writing a program using Visual C# 2005 Professional Edition, and
I was trying to assign multiple MainMenu objects (one by one, of
course) to the same Form (let's suppose 2 MainMenu objects).
It is possible (and it's my case) that these 2 MainMenu objects use
some different MenuItem objects and some identical MenuItem objects.
For example, let's assume that:
mainMenu1 contains miFile, miEdit, miHelp
mainMenu2 contains miFile, miView, miHelp
(so, miFile and miHelp are in both MainMenu objects)
I have noticed that, if I define only one miFile MenuItem and only one
miHelp MenuItem and I add them to the MenuItems of both MainMenu
objects, miFile and miHelp are present only in the second MainMenu.
The statement:
mainMenu1.MenuItems.AddRange(new MenuItem[] {miFile, miEdit, miHelp});
adds miFile, miEdit and miHelp in the MenuItems of mainMenu1.
The statement:
mainMenu2.MenuItems.AddRange(new MenuItem[]{miFile, miView, miHelp});
adds miFile, miView and miHelp in the MenuItems of mainMenu2 and
removes miFile and miHelp from the MenuItems of mainMenu1.

To test the program, I have inserted two buttons in the Form (the first
to assign mainMenu1, the second to assign mainMenu2 to the Form Menu
property):

void btnViewMainMenu1_Click(object sender, EventArgs e)
{
Menu = mainMenu1;
}

void btnViewMainMenu2_Click(object sender, EventArgs e)
{
Menu = mainMenu2;
}

If you click on btnViewMainMenu1, you will see only the miEdit
MenuItem, while, if you click on btnViewMainMenu2, you will see miFile,
miView and miHelp.

I have read that MainMenu has a "CloneMenu" method that duplicates
the MenuItems included, but I have to duplicate only miFile and miHelp,
not all the MenuItems.

Is there a way to share the same MenuItem object to two (or more)
different MainMenu objects, avoiding to use the "CloneMenu" method
and avoiding to define one different MenuItem for every MainMenu
(miFile1, miFile2 and miHelp1, miHelp2) ?

Thank you very much

P.S.: If someone wants to try, I have attached the sample code below...

using System;
using System.Collections;
using System.Data;
using System.Data.OleDb;
using System.Data.Common;
using System.Drawing;
using System.Windows.Forms;

class MainMenuIssue : Form
{
MainMenu mainMenu1, mainMenu2;

MenuItem miFile, miEdit, miView, miHelp;

Button btnViewMainMenu1, btnViewMainMenu2;

public static void Main()
{
Application.Run(new MainMenuIssue());
}

public MainMenuIssue()
{
Size = new Size(350, 200);
CenterToScreen();

mainMenu1 = new MainMenu();
mainMenu2 = new MainMenu();

miFile = new MenuItem();
miFile.Text = "File";

miEdit = new MenuItem();
miEdit.Text = "Edit";

miView = new MenuItem();
miView.Text = "View";

miHelp = new MenuItem();
miHelp.Text = "Help";

mainMenu1.MenuItems.AddRange(new MenuItem[] { miFile, miEdit,
miHelp });
mainMenu2.MenuItems.AddRange(new MenuItem[] { miFile, miView,
miHelp });

Menu = mainMenu1;

btnViewMainMenu1 = new Button();
btnViewMainMenu1.Parent = this;
btnViewMainMenu1.Size = new Size(120, 25);
btnViewMainMenu1.Location = new Point(20, 20);
btnViewMainMenu1.Text = "View mainMenu1";
btnViewMainMenu1.Click += new
EventHandler(btnViewMainMenu1_Click);

btnViewMainMenu2 = new Button();
btnViewMainMenu2.Parent = this;
btnViewMainMenu2.Size = new Size(120, 25);
btnViewMainMenu2.Location = new Point(180, 20);
btnViewMainMenu2.Text = "View mainMenu2";
btnViewMainMenu2.Click += new
EventHandler(btnViewMainMenu2_Click);
}

void btnViewMainMenu1_Click(object sender, EventArgs e)
{
Menu = mainMenu1;
}

void btnViewMainMenu2_Click(object sender, EventArgs e)
{
Menu = mainMenu2;
}
}
 
C

chanmm

Menumenu? Do you mean menuStrip control? just curious why do you need 2?

chanmm

polocar said:
Hi,
I'm writing a program using Visual C# 2005 Professional Edition, and
I was trying to assign multiple MainMenu objects (one by one, of
course) to the same Form (let's suppose 2 MainMenu objects).
It is possible (and it's my case) that these 2 MainMenu objects use
some different MenuItem objects and some identical MenuItem objects.
For example, let's assume that:
mainMenu1 contains miFile, miEdit, miHelp
mainMenu2 contains miFile, miView, miHelp
(so, miFile and miHelp are in both MainMenu objects)
I have noticed that, if I define only one miFile MenuItem and only one
miHelp MenuItem and I add them to the MenuItems of both MainMenu
objects, miFile and miHelp are present only in the second MainMenu.
The statement:
mainMenu1.MenuItems.AddRange(new MenuItem[] {miFile, miEdit, miHelp});
adds miFile, miEdit and miHelp in the MenuItems of mainMenu1.
The statement:
mainMenu2.MenuItems.AddRange(new MenuItem[]{miFile, miView, miHelp});
adds miFile, miView and miHelp in the MenuItems of mainMenu2 and
removes miFile and miHelp from the MenuItems of mainMenu1.

To test the program, I have inserted two buttons in the Form (the first
to assign mainMenu1, the second to assign mainMenu2 to the Form Menu
property):

void btnViewMainMenu1_Click(object sender, EventArgs e)
{
Menu = mainMenu1;
}

void btnViewMainMenu2_Click(object sender, EventArgs e)
{
Menu = mainMenu2;
}

If you click on btnViewMainMenu1, you will see only the miEdit
MenuItem, while, if you click on btnViewMainMenu2, you will see miFile,
miView and miHelp.

I have read that MainMenu has a "CloneMenu" method that duplicates
the MenuItems included, but I have to duplicate only miFile and miHelp,
not all the MenuItems.

Is there a way to share the same MenuItem object to two (or more)
different MainMenu objects, avoiding to use the "CloneMenu" method
and avoiding to define one different MenuItem for every MainMenu
(miFile1, miFile2 and miHelp1, miHelp2) ?

Thank you very much

P.S.: If someone wants to try, I have attached the sample code below...

using System;
using System.Collections;
using System.Data;
using System.Data.OleDb;
using System.Data.Common;
using System.Drawing;
using System.Windows.Forms;

class MainMenuIssue : Form
{
MainMenu mainMenu1, mainMenu2;

MenuItem miFile, miEdit, miView, miHelp;

Button btnViewMainMenu1, btnViewMainMenu2;

public static void Main()
{
Application.Run(new MainMenuIssue());
}

public MainMenuIssue()
{
Size = new Size(350, 200);
CenterToScreen();

mainMenu1 = new MainMenu();
mainMenu2 = new MainMenu();

miFile = new MenuItem();
miFile.Text = "File";

miEdit = new MenuItem();
miEdit.Text = "Edit";

miView = new MenuItem();
miView.Text = "View";

miHelp = new MenuItem();
miHelp.Text = "Help";

mainMenu1.MenuItems.AddRange(new MenuItem[] { miFile, miEdit,
miHelp });
mainMenu2.MenuItems.AddRange(new MenuItem[] { miFile, miView,
miHelp });

Menu = mainMenu1;

btnViewMainMenu1 = new Button();
btnViewMainMenu1.Parent = this;
btnViewMainMenu1.Size = new Size(120, 25);
btnViewMainMenu1.Location = new Point(20, 20);
btnViewMainMenu1.Text = "View mainMenu1";
btnViewMainMenu1.Click += new
EventHandler(btnViewMainMenu1_Click);

btnViewMainMenu2 = new Button();
btnViewMainMenu2.Parent = this;
btnViewMainMenu2.Size = new Size(120, 25);
btnViewMainMenu2.Location = new Point(180, 20);
btnViewMainMenu2.Text = "View mainMenu2";
btnViewMainMenu2.Click += new
EventHandler(btnViewMainMenu2_Click);
}

void btnViewMainMenu1_Click(object sender, EventArgs e)
{
Menu = mainMenu1;
}

void btnViewMainMenu2_Click(object sender, EventArgs e)
{
Menu = mainMenu2;
}
}
 
P

polocar

Menumenu? Do you mean menuStrip control? just curious why do you need 2?
Not MenuStrip, I just wanted to use two MainMenu objects...
In my program I have a TabControl with two TabPages, and I would like
that, when the user clicks on TabPage1, the form Menu property was
assigned to MainMenu1 (a set of MenuItems), and when he clicks on
TabPage2, the form Menu property was assigned to MainMenu2 (another set
of MenuItems, different from the first one).
 

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