操作說明:定義停駐之 ToolStrip 控制項的疊置順序
若要使用停駐將 ToolStrip 控制項正確地定位,您必須依照表單的疊置順序,將控制項正確地定位。
範例
下列程式碼範例示範如何指定疊置順序,以排列 ToolStrip 控制項和停駐的 MenuStrip 控制項。
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);
}
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
堆疊順序取決於 ToolStrip 和 MenuStrip 的順序
控制項會新增至表單的 Controls 集合。
// 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);
' 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 方法的這些呼叫順序反轉,並檢視配置上的效果。
編譯程式碼
這個範例需要:
- System.Design、System.Drawing 和 System.Windows.Forms 組件的參考。