Detailed usage of toolStripContainer1 control in C#

In C#, ToolStripContainercontrols 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 ToolStripContainerthe detailed usage of the control:

  1. Drop a control on a form ToolStripContainer:
    In Visual Studio's Form Designer, drag and drop a ToolStripContainercontrol from the toolbox onto your form.

  2. Add toolbar and content panel:

    • ToolStripContainerThe control consists of two main parts: TopToolStripPaneland ContentPanel.
    • Drag and drop toolbar controls into TopToolStripPanelthe and these toolbars will appear on top of the container.
    • Drag and drop content controls (such as Panel, GroupBox, DataGridViewetc.) into the ContentPanel, and the content will occupy the main part of the container.
  3. To adjust the size and position of toolbars and content panels:

    • Toolbars and content panels can be resized by dragging their borders.
    • Use ToolStripContainer.TopToolStripPanelVisiblethe attribute to control whether the toolbar is displayed.
    • Use ToolStripContainer.ContentPanelCollapsedthe property to control the visibility of the content panel.

Here is an example showing how to use ToolStripContainerthe 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 ToolStripContainercontrol. In the form's load event, we create a toolbar control toolStrip1and a content control contentPanel, and add them to the and ToolStripContainerof the respectively .TopToolStripPanelContentPanel

Hope this example can help you understand and use ToolStripContainerthe detailed method of the control. If you have any further questions, please feel free to ask!

Guess you like

Origin blog.csdn.net/xiaogongzhu001/article/details/131112172