Results 1 to 9 of 9

Thread: Menu items from another class?

  1. #1

    Thread Starter
    Member
    Join Date
    Nov 2003
    Posts
    52

    Menu items from another class?

    Hi All,

    I have created a menu in another class than the form class. A method in this class returns the meny, and in the form the form.Menu is assigned the result.
    The menu exists and all is fine. The call from the form below:
    Code:
    this.Menu = logik1.fillMenu();
    Now I want to add an event to this menu. I would assume that all menuitems (subitems) for this menu would be accessible through this.Menu because that was assigned the created menu.

    How can I add en event for one of the menuitems (openMenu) as below
    Code:
    this.openMenu.Click += new System.EventHandler(this.openMenu_Click);
    I get an error saying that my form is not containing a definition for openMenu, which is true but since form.Menu is assigned the menu, I think it should be accessable???

    The method in class logik1 that creates the method looks like this:
    Code:
    MainMenu mainMenu1 = new MainMenu();
    MenuItem openMenu = new MenuItem();
    openMenu.Text = "Open";
    mainMenu1.MenuItems.Add(openMenu);
    Regards

    Jonni

  2. #2
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: Menu items from another class?

    You are quite correct that the menu item is accessible from the new form, but you are trying to access it the wrong way. You need to understand the difference between variables and objects. A variable is just a way to refer to an object; it is not the object itself. Just because you have a variable in one form named 'openMenu' that refers to a particular object doesn't mean that you have a variable with that name in any other form.

    It's very important to remember that OOP is designed to mimic the behaviour of real world objects. Let's say that I work for a company and my job title is Junior Software Developer. I am an object and 'Junior Software Developer' is a variable that refers to me. Let's say that I leave that company and go to work for someone else as a Senior Software Developer. At the new company I am still the same object, but I'm now referred to by the 'Senior Software Developer' variable. If someone said "do something when the Junior Software Developer finishes a project" then you'd have a problem because this new company doesn't have a variable named 'Junior Software Developer'. I might then leave that company and go to work somewhere else as one of a number of contractors. I would then be Contractor(5) for instance. See how the same object can be refrred to in many different ways in the real world? The same is true in OOP; the same object can be referred to by many different variables and other types of reference too, like array elements and collection items.

    In your case, the only variable that your new form has is 'Menu'. It refers to the menu itself and you must get a reference to the menu item through it; something like:
    Code:
    this.Menu.MenuItems("Open") += new System.EventHandler(this.openMenu_Click);
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  3. #3

    Thread Starter
    Member
    Join Date
    Nov 2003
    Posts
    52

    Re: Menu items from another class?

    Thanks for the reply. I hear what you are saying.

    I understand that I have to go through this.Menu and adding the eventhandler actually looked like
    Code:
    this.Menu.openMenu.Click
    and not
    Code:
    this.openMenu.Click
    I have tried it in many ways without result:

    Code:
    this.Menu.openMenu.Click
    this.Menu.MenuItems(openMenu).Click
    this.Menu.MenuItems("openMenu").Click
    I can't find a method that takes a string as argument, and the methods that takes menuItem as argument doesn't recognise openMenu item.

    ???

    Jonni

  4. #4
    I'm about to be a PowerPoster!
    Join Date
    Jan 2005
    Location
    Everywhere
    Posts
    13,647

    Re: Menu items from another class?

    Index operator is square brackets, parentheses are method arguments. I suspect jm was in VB.NET mode.

    Code:
    this.Menu.MenuItems["Open"] += new EventHandler(this.openMenu_Click);

  5. #5

    Thread Starter
    Member
    Join Date
    Nov 2003
    Posts
    52

    Re: Menu items from another class?

    I still can't find any MenuItems method that takes a string as argument..

    I assume "Open" in your case (below) is the text property of my openMenu item, how can this be an identifier for what menuItem I want to add a handler for?

    Code:
    this.Menu.MenuItems["Open"].click += new EventHandler(this.openMenu_Click);
    Code from the class which creates and returns the Meny
    Code:
    MainMenu mainMenu1 = new MainMenu();
    MenuItem openMenu = new MenuItem();
    openMenu.Text = "Open";
    mainMenu1.MenuItems.Add(openMenu);
    return mainMenu1;
    For a menu that I defiene in the form-Class the syntax is:
    Code:
    this.menuItem1.Click += new System.EventHandler(this.menuItem1_Click);
    Wouldn't openMenu be as accesible as menuItem1 even though openMenu is created elsewhere? I mean it assigned to the forms menu??

    /Jonni
    Last edited by jonni; Dec 8th, 2006 at 06:38 AM.

  6. #6
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: Menu items from another class?

    Oops, sorry. Mangled my C# with VB. I hate it when I do that. So do you I'll wager.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  7. #7
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: Menu items from another class?

    Ah, sorry again. I really messed this one up. The Item property, which is the indexer for the MenuItemCollection class, is overloaded in .NET 2.0 and takes an Integer index or a String key. In .NET 1.1 only the Integer version exists. Now, you never did say that you were using .NET 1.1 but if you were using 2.0 I think you'd be using a MenuStrip rather than a MainMenu, so I should have known. That means that you are either going to have to know the index of the menu item you want or else loop through them until you find the one with the right name.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  8. #8

    Thread Starter
    Member
    Join Date
    Nov 2003
    Posts
    52

    Resolved Re: Menu items from another class?

    Thanks!

    Now it works.

    I use .NET 2.0 but I only find the indexer with int argument. It's a smart device project for win CE 5.0 maybe that's why the overloaded string method doesn't exsist?

    /Jonni

  9. #9
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: Menu items from another class?

    You're quite correct about the String overload not being supported on the CF, as specified by the documentation.

    I would recommend that you use the MenuStrip class in preference to the MainMenu in .NET 2.0.

    Please always specify your version in future, and alsways specify that you're using the CF if you're not posting in the Mobile forum. Otherwise it's quite possible that people will post irrelevant information, which is a waste of everyone's time.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width