如何:向 ToolStripContentPanel 添加控件

更新:2007 年 11 月

可以用编程方式向 ToolStripContentPanel 添加一个或多个控件。

示例

下面的代码示例演示如何向 ToolStripContentPanel 添加 RichTextBox

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);

    }
}

编译代码

此代码示例需要:

  • 对 System、System.Data 和 System.Windows.Forms 程序集的引用。

有关从 Visual Basic 或 Visual C# 的命令行生成此示例的信息,请参见从命令行生成 (Visual Basic)在命令行上使用 csc.exe 生成。也可以通过将代码粘贴到新项目,在 Visual Studio 中生成此示例。 如何:使用 Visual Studio 编译和运行完整的 Windows 窗体代码示例
如何:使用 Visual Studio 编译和运行完整的 Windows 窗体代码示例
如何:使用 Visual Studio 编译和运行完整的 Windows 窗体代码示例
如何:使用 Visual Studio 编译和运行完整的 Windows 窗体代码示例
如何:使用 Visual Studio 编译和运行完整的 Windows 窗体代码示例

“ToolStripContainer 任务”对话框
“ToolStripContainer 任务”对话框
“ToolStripContainer 任务”对话框
“ToolStripContainer 任务”对话框
“ToolStripContainer 任务”对话框

请参见

参考

ToolStripContentPanel

ToolStripContainer

其他资源

ToolStripContainer 控件

ToolStrip 控件(Windows 窗体)