Gewusst wie: Definieren der Z-Reihenfolge angedockter ToolStrip-Steuerelemente

Aktualisiert: November 2007

Damit ein ToolStrip-Steuerelement ordnungsgemäß angedockt werden kann, müssen Sie es korrekt in der Z-Reihenfolge des Formulars positionieren.

Beispiel

Das folgende Codebeispiel veranschaulicht, wie ein ToolStrip-Steuerelement und ein angedocktes MenuStrip-Steuerelement durch Angabe der Z-Reihenfolge angeordnet werden.

Class Form2
   Inherits Form

   Public Sub New()
      ' Create a new ToolStrip control.
      Dim ts As New ToolStrip()

      ' Populate the ToolStrip control.
      ts.Items.Add("Apples")
      ts.Items.Add("Oranges")
      ts.Items.Add("Pears")
      ts.Items.Add("Change Colors", Nothing, New EventHandler(AddressOf ChangeColors_Click))

      ' Create a new MenuStrip.
      Dim ms As 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.
      Me.Controls.Add(ts)

      ' Add the MenuStrip control last.
      ' This is important for correct placement in the z-order.
      Me.Controls.Add(ms)
    End Sub

   ' This event handler is invoked when the "Change colors"
   ' ToolStripItem is clicked. It assigns the Renderer
   ' property for the ToolStrip control.
    Sub ChangeColors_Click(ByVal sender As Object, ByVal e As EventArgs)
        ToolStripManager.Renderer = New ToolStripProfessionalRenderer(New CustomProfessionalColors())
    End Sub
End Class
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);
}

Die Z-Reihenfolge wird durch die Reihenfolge bestimmt, in der das ToolStrip-Steuerelement und das MenuStrip-Steuerelement

der Controls-Auflistung des Formulars hinzugefügt werden.

' Add the ToolStrip to Controls collection.
Me.Controls.Add(ts)

' Add the MenuStrip control last.
' This is important for correct placement in the z-order.
Me.Controls.Add(ms)
// 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);

Kehren Sie die Reihenfolge dieser Aufrufe an die Add-Methode um, und beobachten Sie die Auswirkung auf das Layout.

Kompilieren des Codes

Für dieses Beispiel ist Folgendes erforderlich:

  • Verweise auf die Assemblys System.Design, System.Drawing und System.Windows.Forms.

Informationen zum Erstellen dieses Beispiels über die Befehlszeile für Visual Basic oder Visual C# finden Sie unterErstellen von der Befehlszeile aus (Visual Basic) und Erstellen über die Befehlszeile mit csc.exe. Sie können dieses Beispiel auch in Visual Studio erstellen, indem Sie den Code in ein neues Projekt einfügen.

Siehe auch

Referenz

MenuStrip

ToolStrip

Add

Controls

Dock

Weitere Ressourcen

ToolStrip-Steuerelement (Windows Forms)