locked
Merging Menus in .NET RRS feed

  • Question

  • Hi there

    I need some help.

    Im creating a an application a little like Outlook. One shell which includes a verity of other sub applications loaded as plugins.

    My question is conserning the menu. What Id like to do is in my host application I create a menu with standard menu items like [File] > Exit [Help] > About. Those that are general for all plugins. Then when a specific plugin is loaded that plugin merges its menus into the host and again removes them when another plugin is activated. Very much like Outlook works where you have a New under File menu but when Mail is active New mean New Mail and when Calendar is active New means New Appointment.

    I have looked at ToolStripManager.Merge but I cant seem to get it working correct. If I create a [File] menu on both menus I get two [File] items on my main menu in the host after a merge.

    Any good resources out there on this? 

    Wednesday, February 21, 2007 9:43 AM

Answers

  • Holm

    You're using the right properties, alright.  I always found the code written for me by the designer in Initialize component handled the menu merging withut me having to add any code.

    Here's what I did:

    1. Started a new Windows project.
    2. Made Form1 a MdiContainer
    3. Put a menu strip on Form1
    4. Put two menu items on Form1 "File" & "Moose" (Top Level)
    5. Under File pit a seperator & an Exit
    6. Added Form2 to the solution
    7. Coded the click event of the Moose menu item to launch Form2 with Form1 as MdiParent
    8. Put a menu strip on Form2
    9. Set it's Visible property to false
    10. Put a File & Moose menu on Form2, set both to "Match Only" MergeIndex -1
    11. Added an item called "Insert" under File
    12. Set Insert to "Insert", Index zero.
    13. Run the solution, I get File ----- Exit, click moose, get File Insert ----- Exit

    Using vs.net 2005 SP1 -- I had a heck of a time figuring out the menu merge stuff myself, keep at it!

     

    HTH

    Thursday, February 22, 2007 1:47 PM

All replies

  • Hi Holm

    Merging menu's is tricky at first but once you get the hang of it, it works extremely well.  My guess is that your file menu in the merged object's merge action needs to be MatchOnly.

    My advice to to create a little project with an MDI parent and child and spend a few hours playing with it.  Here's some general concepts to get you started:

    If the parent has File / Edit / View ...

    And the child has File / Edit View ...

    And the parent has three items under file, and the child has an item that goes at the bottom:

    The child's File menu item is set to MatchOnly.  The childs file menu item is set to Insert at position 4.  This puts the child's menu at the end.  If it was set to Replace at position 2... It would match on file, then replace the second item with that one.

    HTH

    Wednesday, February 21, 2007 2:31 PM
  • Yeah thanks for the advice.

    I have a very simple MDI and working on creating a couple of child forms with different menus so I can work it out.

    But basically I have to be working with the MergeAction and MergeIndex properties right?

    And then when I move my newfound experience over to the real app I'll have to use ToolStripManager.Merge and ToolStripManager.RevertMerge instead of having MDI do it automatically right?

    Thursday, February 22, 2007 6:55 AM
  • Ok!!! 1 2 3 4 5 6 7 8 9 10...

    Now Im gonna need some help. How do I insert menuitems in between other menuitems? So not working for me now.

    Give an example:

    [File]
    -
    Exit

    [File]
    New
    Open

    Im trying to merge the bottom one woth the top one.New and Open should come in before Seperator and Exit but they just seem to be appending...

    Thursday, February 22, 2007 8:33 AM
  • Holm

    You're using the right properties, alright.  I always found the code written for me by the designer in Initialize component handled the menu merging withut me having to add any code.

    Here's what I did:

    1. Started a new Windows project.
    2. Made Form1 a MdiContainer
    3. Put a menu strip on Form1
    4. Put two menu items on Form1 "File" & "Moose" (Top Level)
    5. Under File pit a seperator & an Exit
    6. Added Form2 to the solution
    7. Coded the click event of the Moose menu item to launch Form2 with Form1 as MdiParent
    8. Put a menu strip on Form2
    9. Set it's Visible property to false
    10. Put a File & Moose menu on Form2, set both to "Match Only" MergeIndex -1
    11. Added an item called "Insert" under File
    12. Set Insert to "Insert", Index zero.
    13. Run the solution, I get File ----- Exit, click moose, get File Insert ----- Exit

    Using vs.net 2005 SP1 -- I had a heck of a time figuring out the menu merge stuff myself, keep at it!

     

    HTH

    Thursday, February 22, 2007 1:47 PM
  • What seems to trouble me is the MergeIndex property!! I dont get it.

    What does -1 mean? And I read another thread where someone tried to insert something at index 20 allready having index 10 and 30 but it didnt work!

    Friday, February 23, 2007 8:13 AM
  • Let me give an exaplme of how Im looking at it but failing.

    Main menu:

    [File]
    - MergeIndex: -1
    Exit MergeIndex: -1

    Menu on Child:

    [File] - MatchOnly -1
    Alert - Insert 10
    Action - Insert 20
    Severity - Insert 30

    My expected result:
    [File]
    Alert
    Action
    Severity
    -
    Exit

    Actual result
    -
    Exit
    Alert
    Action
    Severity

     

    If I use MergeIndex 0 1 and 2 on my child items I get the correct expected result but leaves me very inflexible I feel. Say in my final application Ill end up with a menu like in Outlook. Now its not that big, but it would be a tedious job to have to insert a new item at a specific location and have to retype the order to accomudate this new item..

    But of course I am intitled to write my own MenuStrip if I so please :)

    Oh and thanks for sticking and helping out. I really appriciate it.

    Friday, February 23, 2007 8:50 AM
  • To stop me from waisting to much time I can use this on a regular application right?

    Only that I have to manually use ToolStripManager.Merge and ToolStripManager.revertMerge right?

    Friday, February 23, 2007 9:39 AM
  • Wow

    I think I cracked it. But I am still troubled by the fact that I cant create the initial menu with like 10, 20, 30 mergeindex values etc. to leave room for updates. But then again nobody wants a menu that is too large so I guess it helps keep the menus clean to the point.

     

    Thank you again for supplying info and helping me out. Super thumbs up.

    Friday, February 23, 2007 1:20 PM