Edit

Share via


How to: Define Z-Ordering of Docked ToolStrip Controls

To position a ToolStrip control correctly with docking, you must position the control correctly in the form's z-order.

Example

The following code example demonstrates how to arrange a ToolStrip control and a docked MenuStrip control by specifying the z-order.

C#
public Form2()
{
    // Create a new ToolStrip control.
    ToolStrip ts = new ToolStrip();

    // Populate the ToolStrip control.
    ts.Items.Add("Apples");
    ts.Items.Add("Oranges");
    ts.Items.Add("Pears");
    ts.Items.Add(
        "Change Colors",
        null,
        new EventHandler(ChangeColors_Click));

    // Create a new MenuStrip.
    MenuStrip ms = new MenuStrip();

    // Dock the MenuStrip control to the top of the form.
    ms.Dock = DockStyle.Top;

    // Add the top-level menu items.
    ms.Items.Add("File");
    ms.Items.Add("Edit");
    ms.Items.Add("View");
    ms.Items.Add("Window");

    // Add the ToolStrip to Controls collection.
    this.Controls.Add(ts);

    // Add the MenuStrip control last.
    // This is important for correct placement in the z-order.
    this.Controls.Add(ms);
}

The z-order is determined by the order in which the ToolStrip and MenuStrip

controls are added to the form's Controls collection.

C#
// Add the ToolStrip to Controls collection.
this.Controls.Add(ts);

// Add the MenuStrip control last.
// This is important for correct placement in the z-order.
this.Controls.Add(ms);

Reverse the order of these calls to the Add method and view the effect on the layout.

Compiling the Code

This example requires:

  • References to the System.Design, System.Drawing, and System.Windows.Forms assemblies.

See also