Was this page helpful?
Your feedback about this content is important. Let us know what you think.
Additional feedback?
1500 characters remaining
Export (0) Print
Expand All

ToolStripManager.Merge Method (ToolStrip, ToolStrip)

Combines two ToolStrip objects of different types.

Namespace:  System.Windows.Forms
Assembly:  System.Windows.Forms (in System.Windows.Forms.dll)

public static bool Merge(
	ToolStrip sourceToolStrip,
	ToolStrip targetToolStrip
)

Parameters

sourceToolStrip
Type: System.Windows.Forms.ToolStrip
The ToolStrip to be combined with the ToolStrip referred to by the targetToolStrip parameter.
targetToolStrip
Type: System.Windows.Forms.ToolStrip
The ToolStrip that receives the ToolStrip referred to by the sourceToolStrip parameter.

Return Value

Type: System.Boolean
true if the merge is successful; otherwise, false.

Use the ToolStripManager.Merge method to combine ToolStrip objects of different types.

Use the ToolStripManager.Merge method to combine ToolStrip objects of identical type, such as ToolStrip objects with other ToolStrip objects, MenuStrip objects with other MenuStrip objects, and so on.

The ToolStrip.AllowMerge property must be set to true for both ToolStrip objects, or this method returns false.

NoteNote

If there are two MenuStrip controls on an MDI child form, setting IsMdiContainer to true for the parent form merges the contents of only one of the MenuStrip controls. Use Merge to merge the contents of additional child MenuStrip controls on the MDI parent form.

The following code example merges menu items based on specified choices. This example is part of a larger example available in the ToolStripManager class overview.


private MergeSample CurrentSample
{
    get { return currentSample; }
    set
    {
        if (currentSample != value)
        {
            bool resetRequired = false;

            if (currentSample == MergeSample.MatchOnly)
            {
                resetRequired = true;
            }
            currentSample = value;
            // Undo previous merge, if any.
            ToolStripManager.RevertMerge(cmsBase, cmsItemsToMerge);
            if (resetRequired)
            {
                RebuildItemsToMerge();
            }

            switch (currentSample)
            {
                case MergeSample.None:
                    return;
                case MergeSample.Append:
                    ScenarioText = "This sample adds items to the end of the list using MergeAction.Append.\r\n\r\nThis is the default setting for MergeAction. A typical scenario is adding menu items to the end of the menu when some part of the program is activated.";
                    ShowAppendSample();
                    break;
                case MergeSample.InsertInSameLocation:
                    ScenarioText = "This sample adds items to the middle of the list using MergeAction.Insert.\r\n\r\nNotice here how the items are added in reverse order: four, three, two, one. This is because they all have the same merge index.\r\n\r\nA typical scenario is adding menu items to the middle or beginning of the menu when some part of the program is activated. ";
                    ShowInsertInSameLocationSample();
                    break;
                case MergeSample.InsertInSameLocationPreservingOrder:
                    ScenarioText = "This sample is the same as InsertInSameLocation, except the items are added in normal order by increasing the MergeIndex of \"two merged items\" to be 3, \"three merged items\" to be 5, and so on.\r\n  You could also add the original items backwards to the source ContextMenuStrip.";
                    ShowInsertInSameLocationPreservingOrderSample();
                    break;
                case MergeSample.ReplacingItems:
                    ScenarioText = "This sample replaces a menu item using MergeAction.Replace. Use this for the MDI scenario where saving does something completely different.\r\n\r\nMatching is based on the Text property. If there is no text match, merging reverts to MergeIndex.";
                    ShowReplaceSample();
                    break;
                case MergeSample.MatchOnly:
                    ScenarioText = "This sample adds only the subitems from the child to the target ContextMenuStrip.";
                    ShowMatchOnlySample();
                    break;

            }
            // Reapply with the new settings.
            ToolStripManager.Merge(cmsItemsToMerge, cmsBase);
        }
    }
}


.NET Framework

Supported in: 4, 3.5, 3.0, 2.0

.NET Framework Client Profile

Supported in: 4, 3.5 SP1

Windows 7, Windows Vista SP1 or later, Windows XP SP3, Windows XP SP2 x64 Edition, Windows Server 2008 (Server Core not supported), Windows Server 2008 R2 (Server Core supported with SP1 or later), Windows Server 2003 SP2

The .NET Framework does not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.

Community Additions

ADD
Show:
© 2015 Microsoft