Comment : ajouter un ToolStripContainer à un formulaire

Mise à jour : novembre 2007

Vous pouvez ajouter par programme un ToolStripContainer à un Windows Form et le remplir avec des contrôles.

Exemple

L'exemple de code suivant montre comment ajouter un ToolStripContainer et une ToolStrip à un Windows Forms, comment ajouter des éléments à la ToolStrip, et comment ajouter la ToolStrip au TopToolStripPanel du ToolStripContainer.

Imports System
Imports System.Collections.Generic
Imports System.ComponentModel
Imports System.Data
Imports System.Drawing
Imports System.Text
Imports System.Windows.Forms




Public Class Form1
   Inherits Form
   Private toolStripContainer1 As ToolStripContainer
   Private toolStrip1 As ToolStrip


   Public Sub New()
      InitializeComponent()
   End Sub 'New

   <STAThread()>  _
   Shared Sub Main()
      Application.EnableVisualStyles()
      Application.Run(New Form1())
   End Sub 'Main


   Private Sub InitializeComponent()
      toolStripContainer1 = New System.Windows.Forms.ToolStripContainer()
      toolStrip1 = New System.Windows.Forms.ToolStrip()
      ' Add items to the ToolStrip.
      toolStrip1.Items.Add("One")
      toolStrip1.Items.Add("Two")
      toolStrip1.Items.Add("Three")
      ' Add the ToolStrip to the top panel of the ToolStripContainer.
      toolStripContainer1.TopToolStripPanel.Controls.Add(toolStrip1)
      ' Add the ToolStripContainer to the form.
      Controls.Add(toolStripContainer1)
   End Sub 'InitializeComponent 
End Class 'Form1
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;


public class Form1 : Form
{
    private ToolStripContainer toolStripContainer1;
    private ToolStrip toolStrip1;

    public Form1()
    {
        InitializeComponent();
    }
    [STAThread]
    static void Main()
    {
        Application.EnableVisualStyles();
        Application.Run(new Form1());
    }

    private void InitializeComponent()
    {
        toolStripContainer1 = new System.Windows.Forms.ToolStripContainer();
        toolStrip1 = new System.Windows.Forms.ToolStrip();
        // Add items to the ToolStrip.
        toolStrip1.Items.Add("One");
        toolStrip1.Items.Add("Two");
        toolStrip1.Items.Add("Three");
        // Add the ToolStrip to the top panel of the ToolStripContainer.
        toolStripContainer1.TopToolStripPanel.Controls.Add(toolStrip1);
        // Add the ToolStripContainer to the form.
        Controls.Add(toolStripContainer1);

    }
}

Compilation du code

Cet exemple de code nécessite :

  • Références aux assemblys System.Drawing, System.Text et System.Windows.Forms.

Pour plus d'informations sur la génération de cet exemple à partir de la ligne de commande pour Visual Basic ou Visual C#, consultez Génération à partir de la ligne de commande (Visual Basic) ou Génération à partir de la ligne de commande avec csc.exe. Vous pouvez aussi générer cet exemple dans Visual Studio en collant le code dans un nouveau projet.

Voir aussi

Référence

ToolStripContainer

Autres ressources

ToolStripContainer, contrôle

ToolStrip, contrôle (Windows Forms)