Question
I'm trying to merge two main menus together, but am having problems getting the right result with sub-items. I'm using the GroupIndex property on my MenuItems to control the merging/insertion.
Menu1 (with groupindices) is like this
- File=10
- Open=11
- Close=12
- Edit=20
- Cut=21
- Paste=22
- Help=90
- About=91
Menu2 is like this
- Edit=20
- Clear=23
- Widgets=30
- Widget1=31
- Widget2=32
And I'm doing
Menu1.Merge(Menu2);
I want the combined menu to have a new top menu "Widgets"and a new "Clear" command in the Edit menu. "Widgets" is working fine, but the Edit Menu has lost cut and paste, which wasn't what I wanted.
How can I stop the Cut and Paste commands from disappearing?
Solution
The menu merge feature in Delphi works a bit differently than what you'd expect: it's non-recursive (unfortunately!). That means that when you call Menu1.Merge
, Menu1's "Edit" menu gets replaced by Menu2's.
You have two options:
- Add "Cut" and "Paste" manually to Menu2.
- Write your own Merge function.
I had the same problem a while ago (see this SO question):
What I eventually ended up with, is using the Toolbar2000 package for all my menus and toolbars. You can then download a very nice piece of code, called TB2Merge, which does exactly what you want.
You could also base any custom-written menu merge code on TB2Merge, I guess...