Comment : ajouter un contrôle à un ToolStripContentPanel

Mise à jour : novembre 2007

Vous pouvez ajouter par programme un ou plusieurs contrôles à un ToolStripContentPanel.

Exemple

L'exemple de code suivant montre comment ajouter une RichTextBox à un ToolStripContentPanel.

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 tsc As ToolStripContainer
   Private rtb As RichTextBox
   Public Sub New()
      InitializeComponent()
   End Sub

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

   Private Sub InitializeComponent()
      Me.tsc = New System.Windows.Forms.ToolStripContainer()
      Me.rtb = New System.Windows.Forms.RichTextBox()
      Me.tsc.ContentPanel.Controls.Add(Me.rtb)
      Me.Controls.Add(tsc)
   End Sub
End Class
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 tsc;
    private RichTextBox rtb;

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

    private void InitializeComponent()
    {
        this.tsc = new System.Windows.Forms.ToolStripContainer();
        this.rtb = new System.Windows.Forms.RichTextBox();
        this.tsc.ContentPanel.Controls.Add(this.rtb);
        this.Controls.Add(this.tsc);

    }
}

Compilation du code

Cet exemple de code nécessite :

  • Références aux assemblys System, System.Data 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

ToolStripContentPanel

ToolStripContainer

Autres ressources

ToolStripContainer, contrôle

ToolStrip, contrôle (Windows Forms)