Merging MenuStrips
Can someone please point me in the right direction?
In the case of an activated MDChild, we automatically merge toolstrips to the MDIParent. You need to create the source strip control that contains the items you'd like to merge into the parent. There are two aspects of the source strip - it needs to contain the items to be merged and the structure to "match" the target toolstrip. For example, in the following example, I'm trying to merge in the "Save" command.
Parent <- AllowMerge=true
File
Open
Exit
ChildMenuStrip <-visible = false, AllowMerge=true
File <- MergeAction = MatchOnly
Save <- MergeAction = Insert, MergeIndex = 1;
result is
Parent
File
Open
Save
Exit
Note, that File acted only to direct the Save command and was not merged. IIn addition, the toolstripitems are moved instead of being cloned like previous versions. You can also manually merge using ToolStripManager.Merge() and RevertMerge() methods in non MDI cases.
Using your example:
Parent ChildMenuStrip result is How can I get it to do it the other way?
File
Open <- MergeIndex = 10
Exit <- MergeIndex = 30
File
Save <- MergeIndex = 20
File
Open
Exit
Save
Please does anyone know? If I have to do it the way Erick Ellis suggested, that doesn't leave much flexibility if at a later stage I need to add another menu item, i.e. I need to renumber all the menu items after it and that could take ages.
I responded to your MSDN feedback request and will provide it here to see if it holds water.
Microsoft comments: I appreciate you entering the problem report. I've got a non-obvious workaround - I'd like to know if it addresses your issue and what pitfalls it may have
Take the examle of
File
New
Open
>> you want custom items here, grouped
Exit
You could set your custom items like this:
File < MatchOnly
CustomN < Insert, MergeIndex 2
Custom4 < Insert, MergeIndex 2
Custom3 < Insert, MergeIndex 2
Custom2 < Insert, MergeIndex 2
Custom1 < Insert, MergeIndex 2
2 is simply the index *after* where you want your items. After merging you'd get the correct order - and this system would work for multiple child menus (they would be grouped). This would avoid having to resequence your entries (although they would be in reverse order). I hope you find this workaround adequate, because making any changes to this complex area is going to be very challenging to justify. Thanks again for the problem report and using ToolStrips!
Hello Erick, I do exactily as you, but I can not get merged MDI Parente and child toolstrip!!! I'm using VB2005 with Framework.net 2.0, I need a path ?
Thanks,
DCandia
Are you using the ToolStripManager.Merge() method? As
Erick indicated, merging tool strips is different than mergine menus.
The code should look something like this:
ToolStripManager.Merge(childForm.childStrip, parentStrip.Name);
If this still isn't working, can you post the relevant code so we can take a look?
If you're looking for a paper reference, I cover MDI applications,
including merging menus as well as tool strips, in my new book (Windows
Forms in Action).
Erik
I would like to indicate that I am using the C# Express that was downloded from microsoft website, and I had too many confusion with regard to the menustrip merging problem. I did what Erick Ellis suggested it seams to work regardless of the merge index. However I am at the beginning.
Thanks jbattat
Id like to add that the match only text must be "exactly" as the text your trying to match. Sounds very obvious i know but i wasted a good ten minutes trying to work out why my menus didn't merge properly. I then realised that one of my file menu items had text property as "File" while the other was "&File".
Silly oversight on my behalf i know but time is money so i hope i can save someone else a few bucks.
Cheers
Erick Ellis wrote:
Merging with ToolStrips in significantly different than previous versions. Here are some guidelines...In the case of an activated MDChild, we automatically merge toolstrips to the MDIParent. You need to create the source strip control that contains the items you'd like to merge into the parent. There are two aspects of the source strip - it needs to contain the items to be merged and the structure to "match" the target toolstrip. For example, in the following example, I'm trying to merge in the "Save" command.
Parent <- AllowMerge=true
File
Open
ExitChildMenuStrip <-visible = false, AllowMerge=true
File <- MergeAction = MatchOnly
Save <- MergeAction = Insert, MergeIndex = 1;result is
Parent
File
Open
Save
ExitNote, that File acted only to direct the Save command and was not merged. IIn addition, the toolstripitems are moved instead of being cloned like previous versions. You can also manually merge using ToolStripManager.Merge() and RevertMerge() methods in non MDI cases.
Erick, I have a question regarding what you mentioned about the File in the ChildMenuStrip not being merged. If you need to use the DropDownOpening event for the ChildMenuStrip, what do you do? Obviously, since it doesn't get added to the Parent, the event will not get fired unless it's invoked by the File on the ChildMenuStrip. Do you have a solution to this delima?
You are correct that this is a dilema. Any events on the child's File menu are not active in the merged menu, so are basically lost.
The solution is to set this up somewhat artificially in the Parent's File menu. The simplest solution is to make a call from the Parent to a known child method that allows the child to take the appropriate action.
For a more elegant solution, a couple ideas for you. One option is to create an inteface that contains the methods to invoke. Child forms can then optionally support this interface. The parent can then check to see if the active child supports the interface.
IChildInterface ci = ActiveMdiChild as IChildInterface;
if (ci != null)
{
ci.SomeMethod(param1, param2);
}
Another approach, and perhaps more elegant, is to define an event in your parent form that is raised when, for example, the File menu opens. Or you could define a generic ParentDropDown event that is called for any menu open. Your child forms can then simply handle this event instead of the menu DropDown events to perform the necessary logic.
Hope that helps,
Erik