ToolStripManager.Merge Method

Definition

Combines ToolStrip controls.

Overloads

Merge(ToolStrip, String)

Combines two ToolStrip objects of the same type.

Merge(ToolStrip, ToolStrip)

Combines two ToolStrip objects of different types.

Merge(ToolStrip, String)

Source:
ToolStripManager.cs
Source:
ToolStripManager.cs
Source:
ToolStripManager.cs

Combines two ToolStrip objects of the same type.

C#
public static bool Merge(System.Windows.Forms.ToolStrip sourceToolStrip, string targetName);

Parameters

sourceToolStrip
ToolStrip

The ToolStrip to be combined with the ToolStrip referred to by the targetName parameter.

targetName
String

The name of the ToolStrip that receives the ToolStrip referred to by the sourceToolStrip parameter.

Returns

true if the merge is successful; otherwise, false.

Exceptions

sourceToolStrip or targetName is null.

sourceToolStrip or targetName refer to the same ToolStrip.

Remarks

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.

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

The ToolStrip.AllowMerge property must be set to true for both ToolStrip objects, and the source and target types must be identical, or this method returns false.

Note

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.

See also

Applies to

.NET Framework 4.8.1 and other versions
Product Versions
.NET Framework 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
Windows Desktop 3.0, 3.1, 5, 6, 7, 8, 9

Merge(ToolStrip, ToolStrip)

Source:
ToolStripManager.cs
Source:
ToolStripManager.cs
Source:
ToolStripManager.cs

Combines two ToolStrip objects of different types.

C#
public static bool Merge(System.Windows.Forms.ToolStrip sourceToolStrip, System.Windows.Forms.ToolStrip targetToolStrip);

Parameters

sourceToolStrip
ToolStrip

The ToolStrip to be combined with the ToolStrip referred to by the targetToolStrip parameter.

targetToolStrip
ToolStrip

The ToolStrip that receives the ToolStrip referred to by the sourceToolStrip parameter.

Returns

true if the merge is successful; otherwise, false.

Examples

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.

C#
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);
        }
    }
}

Remarks

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.

Note

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.

See also

Applies to

.NET Framework 4.8.1 and other versions
Product Versions
.NET Framework 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
Windows Desktop 3.0, 3.1, 5, 6, 7, 8, 9