ToolBar 类

表示一个 Windows 工具栏。虽然 ToolStrip 对以前版本的 ToolBar 控件的功能进行了替换和增补,但是考虑到向后兼容性和将来的使用(如果您选择),仍然保留了 ToolBar

**命名空间:**System.Windows.Forms
**程序集:**System.Windows.Forms(在 system.windows.forms.dll 中)

语法

声明
<ComVisibleAttribute(True)> _
<ClassInterfaceAttribute(ClassInterfaceType.AutoDispatch)> _
Public Class ToolBar
    Inherits Control
用法
Dim instance As ToolBar
[ComVisibleAttribute(true)] 
[ClassInterfaceAttribute(ClassInterfaceType.AutoDispatch)] 
public class ToolBar : Control
[ComVisibleAttribute(true)] 
[ClassInterfaceAttribute(ClassInterfaceType::AutoDispatch)] 
public ref class ToolBar : public Control
/** @attribute ComVisibleAttribute(true) */ 
/** @attribute ClassInterfaceAttribute(ClassInterfaceType.AutoDispatch) */ 
public class ToolBar extends Control
ComVisibleAttribute(true) 
ClassInterfaceAttribute(ClassInterfaceType.AutoDispatch) 
public class ToolBar extends Control

备注

ToolBar 控件用于显示可显示为标准按钮、切换式按钮或下拉式按钮的 ToolBarButton 控件。可以为按钮分配图像,方法是创建一个 ImageList,将它分配给工具栏的 ImageList 属性,然后将图像索引值分配给每个 ToolBarButtonImageIndex 属性。然后可以通过设置 ToolBarButtonText 属性,将文本指定为显示在图像的下方或右边。

将工具栏的 Appearance 属性设置为 Flat,为工具栏及其按钮赋予平面外观。当鼠标指针移动到按钮上时,按钮的外观变为三维样式。通过使用分隔符可以将工具栏按钮划分成多个逻辑组。分隔符是 Style 属性设置为 ToolBarButtonStyle.Separator 的工具栏按钮。当工具栏具有平面外观时,按钮之间的按钮分隔符显示为线,而不是间隔。如果将 Appearance 属性设置为 Normal,则工具栏按钮的外观呈现凸起的三维效果。

如果指定 ButtonSize 属性的值,则工具栏中的所有按钮都被限制为指定的大小。否则,这些按钮将根据其内容调整大小,并且 ButtonSize 属性返回最大按钮的初始大小。

若要创建将在 ToolBar 上显示的 ToolBarButton 控件的集合,请使用 Buttons 属性的 AddInsert 方法逐个添加这些按钮。

Windows Mobile for Pocket PC, Windows Mobile for Smartphone, Windows CE 平台说明: 一个窗体只支持一个 ToolBar,尝试添加额外的 ToolBar 会引发 NotSupportedException。不支持向除窗体以外的任何其他控件中添加 ToolBar,例如向 Panel 中添加。

示例

下面的代码示例创建一个 ToolBar 和三个 ToolBarButton 控件。这些工具栏按钮将被分配到按钮集合,该集合又被分配到工具栏,而工具栏将被添加到窗体上。在工具栏的 ButtonClick 事件上计算 ToolBarButtonClickEventArgsButton 属性,并打开适当的对话框。这段代码要求已创建一个 Form、一个 OpenFileDialog、一个 SaveFileDialog 和一个 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    

Private Sub toolBar1_ButtonClick(ByVal sender As Object, _
ByVal 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
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);
 }
 
 private 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; 
    }
 }
public:
   void InitializeMyToolBar()
   {
      // Create and initialize the ToolBar and ToolBarButton controls.
      toolBar1 = gcnew ToolBar;
      ToolBarButton^ toolBarButton1 = gcnew ToolBarButton;
      ToolBarButton^ toolBarButton2 = gcnew ToolBarButton;
      ToolBarButton^ toolBarButton3 = gcnew 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 += gcnew ToolBarButtonClickEventHandler(
         this, &Form1::toolBar1_ButtonClick );
      
      // Add the ToolBar to the Form.
      Controls->Add( toolBar1 );
   }

private:
   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;
      }
   }
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.set_Text("Open");
    toolBarButton2.set_Text("Save");
    toolBarButton3.set_Text("Print");

    // Add the ToolBarButton controls to the ToolBar.
    toolBar1.get_Buttons().Add(toolBarButton1);
    toolBar1.get_Buttons().Add(toolBarButton2);
    toolBar1.get_Buttons().Add(toolBarButton3);

    // Add the event-handler delegate.
    toolBar1.add_ButtonClick(new ToolBarButtonClickEventHandler(
                                 this.toolBar1_ButtonClick));

    // Add the ToolBar to the Form.
    get_Controls().Add(toolBar1);
} //InitializeMyToolBar

protected void toolBar1_ButtonClick(Object sender,
                                    ToolBarButtonClickEventArgs e)
{
    // Evaluate the Button property to determine which button was clicked.
    switch (toolBar1.get_Buttons().IndexOf(e.get_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;
    } 
} //toolBar1_ButtonClick

继承层次结构

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

线程安全

此类型的任何公共静态(Visual Basic 中的 Shared)成员都是线程安全的,但不保证所有实例成员都是线程安全的。

平台

Windows 98、Windows 2000 SP4、Windows CE、Windows Millennium Edition、Windows Mobile for Pocket PC、Windows Server 2003、Windows XP Media Center Edition、Windows XP Professional x64 Edition、Windows XP SP2、Windows XP Starter Edition

.NET Framework 并不是对每个平台的所有版本都提供支持。有关受支持版本的列表,请参见系统要求

版本信息

.NET Framework

受以下版本支持:2.0、1.1、1.0

.NET Compact Framework

受以下版本支持:2.0、1.0

请参见

参考

ToolBar 成员
System.Windows.Forms 命名空间
ToolBarButton