HOW TO:將按鈕加入至 ToolBar 控制項
更新:2007 年 11 月
注意事項: |
---|
ToolStrip 控制項會取代並且將功能加入至 ToolBar 控制項;不過您也可以選擇保留 ToolBar 控制項,以提供回溯相容性 (Backward Compatibility) 以及供未來使用。 |
ToolBar 控制項的一個整數部分,是您加入的按鈕。這些按鈕可用以輕鬆存取功能表命令,或是也可置於應用程式使用者介面的其他區域,將功能表結構中沒有的命令公開給您的使用者。
下面的範例假設 ToolBar 控制項已加入至 Windows Form (Form1)。
若要以程式設計方式加入按鈕
在程序中,將工具列按鈕加入至 ToolBar.Buttons 集合,建立工具列按鈕。
透過 Buttons 屬性傳遞按鈕的索引來指定個別按鈕的屬性設定。
下列範例假設已將 ToolBar 控制項加入表單。
注意事項: ToolBar.Buttons 集合是以零起始的集合,所以程式碼要根據這點來處理。
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
public void 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. ContextMenu cm = new ContextMenu(); MenuItem miOne = new MenuItem("One"); MenuItem miTwo = new MenuItem("Two"); MenuItem miThree = 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 1 of the buttons. toolBar1.Buttons[1].ToolTipText = "Button 2"; }
public: void 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. System::Windows::Forms::ContextMenu^ cm = gcnew System::Windows::Forms::ContextMenu; MenuItem^ miOne = gcnew MenuItem( "One" ); MenuItem^ miTwo = gcnew MenuItem( "Two" ); MenuItem^ miThree = gcnew 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 1 of the buttons. toolBar1->Buttons[1]->ToolTipText = "Button 2"; }