次の方法で共有


ToolBar クラス

Windows ツール バーを表します。

この型のすべてのメンバの一覧については、ToolBar メンバ を参照してください。

System.Object
   System.MarshalByRefObject
      System.ComponentModel.Component
         System.Windows.Forms.Control
            System.Windows.Forms.ToolBar

Public Class ToolBar
   Inherits Control
[C#]
public class ToolBar : Control
[C++]
public __gc class ToolBar : public Control
[JScript]
public class ToolBar extends Control

スレッドセーフ

この型の public static (Visual Basicでは Shared) のすべてのメンバは、マルチスレッド操作で安全に使用できます。インスタンスのメンバの場合は、スレッドセーフであるとは限りません。

解説

ToolBar コントロールは、標準のボタン、トグル スタイルのボタン、またはドロップダウン スタイルのボタンとして表示される ToolBarButton コントロールを表示するために使用されます。ボタンにイメージを割り当てるには、 ImageList を作成し、それをツール バーの ImageList プロパティに割り当て、イメージのインデックス値を各 ToolBarButtonImageIndex プロパティに代入します。次に、 ToolBarButtonText プロパティを設定して、イメージの下または右側に表示されるテキストを割り当てます。

ツール バーの Appearance プロパティを Flat に設定して、ツール バーとそのボタンがフラットな外観になるようにします。マウス ポインタをボタンの上に移動すると、ボタンの外観が 3 次元に変化します。ツール バー ボタンは、区切り記号を使用して論理グループに分けることができます。区切り記号は、 Style プロパティが ToolBarButtonStyle.Separator に設定されている一種のツール バー ボタンです。ツール バーがフラットな外観である場合、ボタンの区切り記号は、各ボタンとの間に空白ではなく線として表示されます。 Appearance プロパティが Normal に設定されている場合、このツール バー ボタンは浮き出した状態の 3D で表示されます。

ButtonSize プロパティの値を指定した場合、ツール バーのすべてのボタンが、指定したサイズに制限されます。指定しない場合、ボタンのサイズは内容によって調整され、 ButtonSize プロパティは最大のボタンの初期サイズを返します。

ToolBar 上に表示する ToolBarButton コントロールのコレクションを作成するには、 Buttons プロパティの Add メソッド、または Insert メソッドを使用して、ボタンを個別に追加します。

.NET Compact Framework - Windows CE .NET プラットフォームに関する注意点: 1 つの Form がサポートする ToolBar は 1 つだけです。複数の ToolBar を追加しようとすると、 NotSupportedException がスローされます。

Form 以外のコントロール、たとえば PanelToolBar を追加することはできません。

使用例

[Visual Basic, C#, C++] ToolBar と 3 つの ToolBarButton コントロールを作成する例を次に示します。ツール バー ボタンはボタン コレクションに割り当てられ、コレクションはツール バーに割り当てられ、ツール バーはフォームに追加されます。ツール バーの ButtonClick イベントが発生すると、 ToolBarButtonClickEventArgsButton プロパティが評価され、該当するダイアログ ボックスが開きます。このコードは、 FormOpenFileDialogSaveFileDialog 、および PrintDialog がすべて作成されていることを前提にしています。

 
Public Sub InitializeMyToolBar()
    ' Create and initialize the ToolBar and ToolBarButton controls.
    Dim toolBar1 As New ToolBar()
    Dim toolBarButton1 As New ToolBarButton()
    Dim toolBarButton2 As New ToolBarButton()
    Dim toolBarButton3 As New ToolBarButton()
    
    ' Set the Text properties of the ToolBarButton controls.
    toolBarButton1.Text = "Open"
    toolBarButton2.Text = "Save"
    toolBarButton3.Text = "Print"
    
    ' Add the ToolBarButton controls to the ToolBar.
    toolBar1.Buttons.Add(toolBarButton1)
    toolBar1.Buttons.Add(toolBarButton2)
    toolBar1.Buttons.Add(toolBarButton3)
    
    ' Add the event-handler delegate.
    AddHandler toolBar1.ButtonClick, AddressOf Me.toolBar1_ButtonClick
    
    ' Add the ToolBar to the Form.
    Controls.Add(toolBar1)
End Sub    

Protected Sub toolBar1_ButtonClick(sender As Object, _
    e As ToolBarButtonClickEventArgs)
    
    ' Evaluate the Button property to determine which button was clicked.
    Select Case toolBar1.Buttons.IndexOf(e.Button)
        Case 0
            openFileDialog1.ShowDialog()
            ' Insert code to open the file.
        Case 1
            saveFileDialog1.ShowDialog()
            ' Insert code to save the file.
        Case 2
            printDialog1.ShowDialog()
            ' Insert code to print the file.
    End Select        
End Sub


[C#] 
public void InitializeMyToolBar()
 {
    // Create and initialize the ToolBar and ToolBarButton controls.
    toolBar1 = new ToolBar();
    ToolBarButton toolBarButton1 = new ToolBarButton();
    ToolBarButton toolBarButton2 = new ToolBarButton();
    ToolBarButton toolBarButton3 = new ToolBarButton();
 
    // Set the Text properties of the ToolBarButton controls.
    toolBarButton1.Text = "Open";
    toolBarButton2.Text = "Save";
    toolBarButton3.Text = "Print";
 
    // Add the ToolBarButton controls to the ToolBar.
    toolBar1.Buttons.Add(toolBarButton1);
    toolBar1.Buttons.Add(toolBarButton2);
    toolBar1.Buttons.Add(toolBarButton3);
    
    // Add the event-handler delegate.
    toolBar1.ButtonClick += new ToolBarButtonClickEventHandler (
       this.toolBar1_ButtonClick);
    
    // Add the ToolBar to the Form.
    Controls.Add(toolBar1);
 }
 
 protected void toolBar1_ButtonClick (
                         Object sender, 
                         ToolBarButtonClickEventArgs e)
 {
   // Evaluate the Button property to determine which button was clicked.
   switch(toolBar1.Buttons.IndexOf(e.Button))
   {
      case 0:
         openFileDialog1.ShowDialog();
         // Insert code to open the file.
         break; 
      case 1:
         saveFileDialog1.ShowDialog();
         // Insert code to save the file.
         break; 
      case 2:
         printDialog1.ShowDialog();
         // Insert code to print the file.    
         break; 
    }
 }


[C++] 
public:
    void InitializeMyToolBar() {
        // Create and initialize the ToolBar and ToolBarButton controls.
        toolBar1 = new ToolBar();
        ToolBarButton __gc *toolBarButton1 = new ToolBarButton();
        ToolBarButton __gc *toolBarButton2 = new ToolBarButton();
        ToolBarButton __gc *toolBarButton3 = new ToolBarButton();

        // Set the Text properties of the ToolBarButton controls.
        toolBarButton1->Text = S"Open";
        toolBarButton2->Text = S"Save";
        toolBarButton3->Text = S"Print";

        // Add the ToolBarButton controls to the ToolBar.
        toolBar1->Buttons->Add(toolBarButton1);
        toolBar1->Buttons->Add(toolBarButton2);
        toolBar1->Buttons->Add(toolBarButton3);

        // Add the event-handler delegate.
        toolBar1->ButtonClick += new ToolBarButtonClickEventHandler(this, &Form1::toolBar1_ButtonClick);

        // Add the ToolBar to the Form.
        Controls->Add(toolBar1);
    };

protected:
    void toolBar1_ButtonClick (Object *sender, ToolBarButtonClickEventArgs *e) {
        // Evaluate the Button property to determine which button was clicked.
        switch(toolBar1->Buttons->IndexOf(e->Button)) {
        case 0:
            openFileDialog1->ShowDialog();
            // Insert code to open the file.
            break; 
        case 1:
            saveFileDialog1->ShowDialog();
            // Insert code to save the file.
            break; 
        case 2:
            printDialog1->ShowDialog();
            // Insert code to print the file.    
            break; 
        };
    };

[JScript] JScript のサンプルはありません。Visual Basic、C#、および C++ のサンプルを表示するには、このページの左上隅にある言語のフィルタ ボタン 言語のフィルタ をクリックします。

必要条件

名前空間: System.Windows.Forms

プラットフォーム: Windows 98, Windows NT 4.0, Windows Millennium Edition, Windows 2000, Windows XP Home Edition, Windows XP Professional, Windows Server 2003 ファミリ, .NET Compact Framework - Windows CE .NET

アセンブリ: System.Windows.Forms (System.Windows.Forms.dll 内)

参照

ToolBar メンバ | System.Windows.Forms 名前空間 | ToolBarButton