ToolBar.ToolBarButtonCollection.Insert 方法

在指定位置插入工具栏按钮集合中的现有工具栏按钮。

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

语法

声明
Public Sub Insert ( _
    index As Integer, _
    button As ToolBarButton _
)
用法
Dim instance As ToolBarButtonCollection
Dim index As Integer
Dim button As ToolBarButton

instance.Insert(index, button)
public void Insert (
    int index,
    ToolBarButton button
)
public:
void Insert (
    int index, 
    ToolBarButton^ button
)
public void Insert (
    int index, 
    ToolBarButton button
)
public function Insert (
    index : int, 
    button : ToolBarButton
)

参数

  • index
    集合中要插入工具栏按钮的索引位置。

备注

还可以使用 AddAddRange 方法,向集合中添加新的 ToolBarButton 对象。

若要移除先前添加的 ToolBarButton,请使用 RemoveRemoveAtClear 方法。

示例

下面的代码示例从 ToolBar 控件中移除一个现有的 ToolBarButton(如果该按钮存在),并将四个新的 ToolBarButton 对象添加和插入到 ToolBar 中。本示例要求有一个在其上具有 ToolBar 控件的 Form

Private Sub AddToolbarButtons(toolBar As ToolBar)
   If Not toolBar.Buttons.IsReadOnly Then
      ' If toolBarButton1 in in the collection, remove it.
      If toolBar.Buttons.Contains(toolBarButton1) Then
         toolBar.Buttons.Remove(toolBarButton1)
      End If

      ' Create three toolbar buttons.
      Dim tbb1 As New ToolBarButton("tbb1")
      Dim tbb2 As New ToolBarButton("tbb2")
      Dim tbb3 As New ToolBarButton("tbb3")

      ' Add toolbar buttons to the toolbar.     
      toolBar.Buttons.AddRange(New ToolBarButton() {tbb2, tbb3})
      toolBar.Buttons.Add("tbb4")

      ' Insert tbb1 into the first position in the collection.
      toolBar.Buttons.Insert(0, tbb1)
   End If
End Sub
private void AddToolbarButtons(ToolBar toolBar)
{
   if(!toolBar.Buttons.IsReadOnly)
   {
      // If toolBarButton1 in in the collection, remove it.
      if(toolBar.Buttons.Contains(toolBarButton1))
      {
         toolBar.Buttons.Remove(toolBarButton1);
      }
    
      // Create three toolbar buttons.
      ToolBarButton tbb1 = new ToolBarButton("tbb1");
      ToolBarButton tbb2 = new ToolBarButton("tbb2");
      ToolBarButton tbb3 = new ToolBarButton("tbb3");
      
      // Add toolbar buttons to the toolbar.        
      toolBar.Buttons.AddRange(new ToolBarButton[] {tbb2, tbb3});
      toolBar.Buttons.Add("tbb4");
    
      // Insert tbb1 into the first position in the collection.
      toolBar.Buttons.Insert(0, tbb1);
   }
}
void AddToolbarButtons( ToolBar^ toolBar )
{
   if (  !toolBar->Buttons->IsReadOnly )
   {
      
      // If toolBarButton1 in in the collection, remove it.
      if ( toolBar->Buttons->Contains( toolBarButton1 ) )
      {
         toolBar->Buttons->Remove( toolBarButton1 );
      }
      
      // Create three toolbar buttons.
      ToolBarButton^ tbb1 = gcnew ToolBarButton( "tbb1" );
      ToolBarButton^ tbb2 = gcnew ToolBarButton( "tbb2" );
      ToolBarButton^ tbb3 = gcnew ToolBarButton( "tbb3" );
      
      // Add toolbar buttons to the toolbar.
      array<ToolBarButton^>^buttons = {tbb2,tbb3};
      toolBar->Buttons->AddRange( buttons );
      toolBar->Buttons->Add( "tbb4" );
      
      // Insert tbb1 into the first position in the collection.
      toolBar->Buttons->Insert( 0, tbb1 );
   }
}
private void AddToolbarButtons(ToolBar toolBar)
{
    if (!(toolBar.get_Buttons().get_IsReadOnly())) {
        // If toolBarButton1 in in the collection, remove it.
        if (toolBar.get_Buttons().Contains(toolBarButton1)) {
            toolBar.get_Buttons().Remove(toolBarButton1);
        }
        // Create three toolbar buttons.
        ToolBarButton tbb1 = new ToolBarButton("tbb1");
        ToolBarButton tbb2 = new ToolBarButton("tbb2");
        ToolBarButton tbb3 = new ToolBarButton("tbb3");
        // Add toolbar buttons to the toolbar.        
        toolBar.get_Buttons().AddRange(new ToolBarButton[] { tbb2, tbb3 });
        toolBar.get_Buttons().Add("tbb4");
        // Insert tbb1 into the first position in the collection.
        toolBar.get_Buttons().Insert(0, tbb1);
    }
} //AddToolbarButtons

平台

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.ToolBarButtonCollection 类
ToolBar.ToolBarButtonCollection 成员
System.Windows.Forms 命名空间
Add
AddRange