Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Multiple MainMenu objects with some MenuItem objects shared

Status
Not open for further replies.

polocar

Programmer
Sep 20, 2004
89
IT
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...

Code:
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;
    }
 
If you are interested in a different GUI concept than main menus - I would suggest you take a look at the new Office 2007 Beta. Word and Excel now use "the ribbon" which is a cool idea.


If you want to stick with menus, simply create one main menu and a bunch of menu items. I would actually put all your main menu items in a separate class and call a method like PopulateMyMenu(mainMenu1) that will add or remove menuitems as needed. The actual problem you're having is: you can't have 1 GUI Item in 2 different places.


Hope that gives you some ideas.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top