次の方法で共有


方法 : ツール バー コントロールにボタンを追加する

更新 : 2007 年 11 月

メモ :

ToolStrip コントロールは、ToolBar コントロールに代わると共に追加の機能を提供します。ただし、ToolBar コントロールは、下位互換性を保つ目的および将来使用する目的で保持されます。

ToolBar コントロールの重要な要素は、このコントロールに追加するボタンです。追加するボタンは、メニュー コマンドへ手軽にアクセスするために使用できます。また、アプリケーションのユーザー インターフェイスの別の部分にボタンを配置することにより、メニュー構造では利用できないコマンドをユーザーに提供することもできます。

次の例では、Windows フォーム (Form1) に ToolBar コントロールが追加されていると仮定しています。

プログラム実行時にボタンを追加するには

  1. プロシージャで、ToolBar.Buttons コレクションにツール バー ボタンを追加することで作成します。

  2. Buttons プロパティを通じてボタンのインデックスを渡すことにより、各ボタンごとにプロパティの設定値を指定します。

    次の例は、既に ToolBar コントロールが追加されたフォームを想定しています。

    メモ :

    ToolBar.Buttons コレクションは、インデックス番号が 0 から始まるコレクションです。コードはそれに合わせて処理する必要があります。

    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";
       }
    

参照

処理手順

方法 : ツール バー ボタンのアイコンを定義する

方法 : ツール バー ボタンのメニュー イベントをトリガする

参照

ToolBar

ToolBar コントロールの概要 (Windows フォーム)

その他の技術情報

ToolBar コントロール (Windows フォーム)