In C#, ToolStripContainer
controls are used to create a container that contains toolbars and content panels. It provides an easy way to group toolbars and content together and resize and position them as needed. The following is ToolStripContainer
the detailed usage of the control:
-
Drop a control on a form
ToolStripContainer
:
In Visual Studio's Form Designer, drag and drop aToolStripContainer
control from the toolbox onto your form. -
Add toolbar and content panel:
ToolStripContainer
The control consists of two main parts:TopToolStripPanel
andContentPanel
.- Drag and drop toolbar controls into
TopToolStripPanel
the and these toolbars will appear on top of the container. - Drag and drop content controls (such as
Panel
,GroupBox
,DataGridView
etc.) into theContentPanel
, and the content will occupy the main part of the container.
-
To adjust the size and position of toolbars and content panels:
- Toolbars and content panels can be resized by dragging their borders.
- Use
ToolStripContainer.TopToolStripPanelVisible
the attribute to control whether the toolbar is displayed. - Use
ToolStripContainer.ContentPanelCollapsed
the property to control the visibility of the content panel.
Here is an example showing how to use ToolStripContainer
the control:
using System;
using System.Windows.Forms;
namespace ToolStripContainerExample
{
public partial class MainForm : Form
{
public MainForm()
{
InitializeComponent();
}
private void MainForm_Load(object sender, EventArgs e)
{
// 创建工具栏控件
ToolStrip toolStrip1 = new ToolStrip();
ToolStripButton button1 = new ToolStripButton();
button1.Text = "按钮1";
toolStrip1.Items.Add(button1);
// 创建内容控件
Panel contentPanel = new Panel();
contentPanel.BackColor = System.Drawing.Color.White;
Label label1 = new Label();
label1.Text = "这是内容面板";
contentPanel.Controls.Add(label1);
// 将工具栏和内容面板添加到 ToolStripContainer
toolStripContainer1.TopToolStripPanel.Controls.Add(toolStrip1);
toolStripContainer1.ContentPanel.Controls.Add(contentPanel);
}
}
}
In the above example, we created a form application named "MainForm" and placed a ToolStripContainer
control. In the form's load event, we create a toolbar control toolStrip1
and a content control contentPanel
, and add them to the and ToolStripContainer
of the respectively .TopToolStripPanel
ContentPanel
Hope this example can help you understand and use ToolStripContainer
the detailed method of the control. If you have any further questions, please feel free to ask!