Results 1 to 5 of 5

Thread: [RESOLVED] [2005] Multiple forms sharing one menustrip

  1. #1

    Thread Starter
    Hyperactive Member ZaNi's Avatar
    Join Date
    Jun 2006
    Location
    Australia
    Posts
    360

    Resolved [RESOLVED] [2005] Multiple forms sharing one menustrip

    I'm working with multiple forms, and have created a menustrip on one form. I wan't to make any changes to the menustrip on this form, then have the menu copy at runtime to the other forms where I need it.

    I'm looking for suggestions on how to have the second form build its menu dynamically based on what's in the menu in the first form. I already tried the loop below (among other things), and have not had any success (the loop generates an error at 'Next' stating something has changed and the iteration may have an error, and at the same time, an exception is generated (which I don't handle)). Hopefully there's an easier way then maintaining multiple copies of the same menu (though when I finish the program, I might change it to that instead anyway).

    Thanks in advance.

    VB Code:
    1. ' In Form2.Load
    2.         For Each item As ToolStripItem In Form1.MainMenuStrip.Items
    3.             MenuStrip1.Items.Add(item)
    4.         Next ' Exception generated here

    EDIT: I already found these two threads, but I couldn't find the answer:
    First thread
    Second thread
    * Don't limit yourself to sanity
    * I'd rather be optimistic and naive than pessimistic and right
    * Ask good questions, get good answers.

    My Codebank submissions:
    How to write one rountine to handle many events
    Accessing and Using variables across multiple forms
    Printing/Previewing a form or control

    Links I've "borrowed" from other people:
    vbdotnetboy - Awesome site for tips

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

    Re: [2005] Multiple forms sharing one menustrip

    I very much doubt that a single menu can belong to more than one form or that a single item can belong to more than one menu. If these two forms are being displayed at the same time then you're going to have to make copies of the items on the first menu and add them to the second. If you aren't displaying both forms at the same time then just use the same MenuStrip object on both forms and pass it back and forth between the two.
    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
    Hyperactive Member ZaNi's Avatar
    Join Date
    Jun 2006
    Location
    Australia
    Posts
    360

    Re: [2005] Multiple forms sharing one menustrip

    Well, only one will be displayed at a time (at least for the time I plan to use this code), so I'm happy to move it between forms. I am happy to copy as well, but I can't figure out the code for it... I assume I need to iterate through the menu items and then throught the children, and I'm happy with that or whatever.

    The only problem is the appropriate code to make it work... If you could get me started, it would be greatly appreciated
    * Don't limit yourself to sanity
    * I'd rather be optimistic and naive than pessimistic and right
    * Ask good questions, get good answers.

    My Codebank submissions:
    How to write one rountine to handle many events
    Accessing and Using variables across multiple forms
    Printing/Previewing a form or control

    Links I've "borrowed" from other people:
    vbdotnetboy - Awesome site for tips

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

    Re: [2005] Multiple forms sharing one menustrip

    If you plan to use the same object then its just a matter of passing anobject between two forms. I'm sure you've done that before. You just have to add it to the Controls collection of one form and it will be removed from the other. Just keep in mind though that if you have event handlers on both forms that refer to the same object then they will both be executed if that event is raised. If you want to copy then you just ahve to treat the menu as a tree structure and use recursion.
    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

  5. #5

    Thread Starter
    Hyperactive Member ZaNi's Avatar
    Join Date
    Jun 2006
    Location
    Australia
    Posts
    360

    Re: [2005] Multiple forms sharing one menustrip

    I'll probably go with Me.Controls.Add(menu), but if I was doing it recursively, what types do I use for the different elements? As I pointed out in my first post, I attempted this already, but the code I tried didn't like me Could you give a little more detail on that?

    EDIT: Tried moving control between forms, and it works fine if the second form takes it from the first form, but at compile time the menu does not exist in the second form, so I cannot just Me.Controls.Add(Form2.MenuStrip1) from Form1. Any thoughts on that?

    EDIT2: I feel sexy I used Me.Controls.Add(Me.MenuStrip1). Go team. Thanks for the help.
    Last edited by ZaNi; Aug 1st, 2006 at 09:00 PM.
    * Don't limit yourself to sanity
    * I'd rather be optimistic and naive than pessimistic and right
    * Ask good questions, get good answers.

    My Codebank submissions:
    How to write one rountine to handle many events
    Accessing and Using variables across multiple forms
    Printing/Previewing a form or control

    Links I've "borrowed" from other people:
    vbdotnetboy - Awesome site for tips

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