在過程中,創建工具欄按鈕,方法是將這些按鈕添加到 System.Windows.Forms.ToolBar.Buttons 集合中。
通過 Buttons 屬性傳遞按鈕的索引來指定單個按鈕的屬性設置。
下面的示例假定壹個已添加了 ToolBar 控件的窗體。
註意
System.Windows.Forms.ToolBar.Buttons 集合是壹個從零開始的集合,因此,編寫代碼時應使用相應的初始值。
Visual Basic 復制代碼
Public Sub CreateToolBarButtons()
' Create buttons and set text property.
ToolBar1.Buttons.Add("One")
ToolBar1.Buttons.Add("Two")
ToolBar1.Buttons.Add("Three")
ToolBar1.Buttons.Add("Four")
' Set properties of StatusBar panels.
' Set Style property.
ToolBar1.Buttons(0).Style = ToolBarButtonStyle.PushButton
ToolBar1.Buttons(1).Style = ToolBarButtonStyle.Separator
ToolBar1.Buttons(2).Style = ToolBarButtonStyle.ToggleButton
ToolBar1.Buttons(3).Style = ToolBarButtonStyle.DropDownButton
' Set the ToggleButton's PartialPush property.
ToolBar1.Buttons(2).PartialPush = True
' Instantiate a ContextMenu component and menu items.
' Set the DropDownButton's DropDownMenu property to the context menu.
Dim cm As New ContextMenu()
Dim miOne As New MenuItem("One")
Dim miTwo As New MenuItem("Two")
Dim miThree As New MenuItem("Three")
cm.MenuItems.Add(miOne)
cm.MenuItems.Add(miTwo)
cm.MenuItems.Add(miThree)
ToolBar1.Buttons(3).DropDownMenu = cm
' Set the PushButton's Pushed property.
ToolBar1.Buttons(0).Pushed = True
' Set the ToolTipText property of one of the buttons.
ToolBar1.Buttons(1).ToolTipText = "Button 2"
End Sub