|
-
Aug 1st, 2006, 07:14 PM
#1
Thread Starter
Hyperactive Member
[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:
' In Form2.Load
For Each item As ToolStripItem In Form1.MainMenuStrip.Items
MenuStrip1.Items.Add(item)
Next ' Exception generated here
EDIT: I already found these two threads, but I couldn't find the answer:
First thread
Second thread
-
Aug 1st, 2006, 07:58 PM
#2
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.
-
Aug 1st, 2006, 08:13 PM
#3
Thread Starter
Hyperactive Member
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
-
Aug 1st, 2006, 08:21 PM
#4
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.
-
Aug 1st, 2006, 08:34 PM
#5
Thread Starter
Hyperactive Member
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.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|