Cómo: Agregar un ToolStripContainer a un formulario

Actualización: noviembre 2007

Puede agregar mediante programación un panel ToolStripContainer a un formulario Windows Forms y rellenarlo con controles.

Ejemplo

En el ejemplo de código siguiente se muestra cómo agregar un panel ToolStripContainer y un control ToolStrip a formularios Windows Forms, cómo agregar elementos al control ToolStrip y cómo agregar el control ToolStrip a la propiedad TopToolStripPanel del panel 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);

    }
}

Compilar el código

Este ejemplo de código requiere:

  • Referencias a los ensamblados System.Drawing, System.Text y System.Windows.Forms.

Para obtener información sobre la generación de este ejemplo desde la línea de comandos de Visual Basic o Visual C#, vea Generar desde la línea de comandos (Visual Basic) o Generar la línea de comandos con csc.exe. También puede generar este ejemplo en Visual Studio pegando el código en un proyecto nuevo.

Vea también

Referencia

ToolStripContainer

Otros recursos

ToolStripContainer (Control)

ToolStrip (Control de formularios Windows Forms)