[Yugong Series] Detailed explanation of ToolStripContainer control on Winform control special topic in September 2023


Preface

Winform controls are user interface elements in Windows Forms. They can be used to create various visual and interactive components of Windows applications, such as buttons, labels, text boxes, drop-down list boxes, check boxes, radio boxes, progress bars, etc. . Developers can use Winform controls to build user interfaces and respond to user actions to create powerful desktop applications.

1. Detailed explanation of ToolStripContainer control

The ToolStripContainer control is a container control in WinForm, which can contain controls such as ToolStrip, MenuStrip, and StatusStrip. The ToolStripContainer control can place these controls in a common container and automatically adjust the position and size of these controls when the form size changes.

The advantage of using the ToolStripContainer control is that you can maintain a fixed toolbar, menu bar and status bar in the form, and can automatically adapt to changes in the form size. At the same time, the relationship between these controls is fixed, and there is no need to manually adjust the position and size between them.

At design time, you can find the ToolStripContainer control in Visual Studio's toolbox and drag it to the design surface. Then, you can set its properties and add controls such as ToolStrip, MenuStrip, and StatusStrip in the Properties window.

When using the ToolStripContainer control, you need to pay attention to the following issues:

  1. The ToolStripContainer control will automatically place ToolStrip, MenuStrip, StatusStrip, ToolStripStatusLabel, ToolStripProgressBar and other controls in the appropriate location.

  2. The Dock property of the ToolStripContainer control should be set to Fill so that the entire form is filled.

  3. The ToolStripPanel control can place the ToolStrip control in other container controls, such as the scalable SplitContainer control.

  4. Multiple ToolStripPanel controls can be nested in the ToolStripContainer control, and each ToolStripPanel control can contain multiple ToolStrip controls.

1. Introduction to attributes

1.1 Common attributes

  1. BottomToolStripPanel: This property gets or sets the ToolStripPanel control located at the bottom of ToolStripContainer. You can specify its position using the ToolStripPanel's Dock property.

  2. ContentPanel: This property gets or sets the center position of ToolStripContainer, where other controls can be added.

  3. LeftToolStripPanel: This property gets or sets the ToolStripPanel control located on the left side of ToolStripContainer. You can specify its position using the ToolStripPanel's Dock property.

  4. RightToolStripPanel: This property gets or sets the ToolStripPanel control located on the right side of ToolStripContainer. You can specify its position using the ToolStripPanel's Dock property.

  5. TopToolStripPanel: This property gets or sets the ToolStripPanel control located at the top of ToolStripContainer. You can specify its position using the ToolStripPanel's Dock property.

1.2 Use

private void Form1_Load(object sender, EventArgs e)
{
    
    
    // 创建一个新的MenuStrip控件并设置一些菜单项
    MenuStrip menuStrip = new MenuStrip();
    menuStrip.Items.AddRange(new ToolStripItem[] {
    
    
        new ToolStripMenuItem("文件"),
        new ToolStripMenuItem("编辑"),
        new ToolStripMenuItem("查看"),
        new ToolStripMenuItem("工具"),
        new ToolStripMenuItem("帮助")
    });

    // 创建一个新的ToolStrip控件并设置一些工具按钮
    ToolStrip toolStrip = new ToolStrip();
    toolStrip.Items.AddRange(new ToolStripItem[] {
    
    
        new ToolStripButton("新建"),
        new ToolStripButton("打开"),
        new ToolStripButton("保存"),
        new ToolStripButton("剪切"),
        new ToolStripButton("复制"),
        new ToolStripButton("粘贴"),
        new ToolStripSeparator(),
        new ToolStripButton("撤销"),
        new ToolStripButton("重做")
    });

    // 创建一个新的StatusStrip控件并设置一个状态标签
    StatusStrip statusStrip = new StatusStrip();
    statusStrip.Items.Add(new ToolStripStatusLabel("就绪"));

    // 将控件添加到ToolStripContainer控件中的三个ToolStripPanel中
    this.toolStripContainer1.TopToolStripPanel.Controls.Add(menuStrip);
    this.toolStripContainer1.BottomToolStripPanel.Controls.Add(statusStrip);
    this.toolStripContainer1.ContentPanel.Controls.Add(new TextBox());

    // 设置各个控件的Dock属性
    menuStrip.Dock = DockStyle.Fill;
    toolStrip.Dock = DockStyle.Fill;
    statusStrip.Dock = DockStyle.Fill;

    // 设置ToolStripContainer控件的Dock属性
    this.toolStripContainer1.Dock = DockStyle.Fill;
}

Insert image description here

2. Common scenarios

The ToolStripContainer control is usually used to layout the toolbar and status bar of the Winform interface. Common scenarios are as follows:

  1. Combination of toolbar and status bar: The ToolStripContainer control allows the toolbar and status bar to be merged into a container, providing a better user experience.

  2. Movable toolbar: The toolbar of the ToolStripContainer control supports user-defined layout. The position and size of the toolbar can be changed by dragging the toolbar items to meet the user's needs.

  3. Multi-form applications: The ToolStripContainer control can be used in multi-form applications, allowing multiple forms to share the same toolbar and status bar, thereby improving user efficiency and operability.

  4. Interface beautification: The ToolStripContainer control supports customization of appearance and style, which can make the application interface more beautiful and easier to use.

  5. Unified management of commands and operations: The ToolStripContainer control provides a centralized location to manage commands and operations in the application, thereby avoiding duplication of code and errors.

3. Specific cases

The following is a complete example of the ToolStripContainer control in Winform, including event handling. In this case, we added a button that when clicked by the user displays a message in the status bar:

public partial class MainForm : Form
{
    
    
    private ToolStripStatusLabel statusLabel;

    public MainForm()
    {
    
    
        InitializeComponent();
        InitializeToolStrip();
    }
    statusLabel = new ToolStripStatusLabel("就绪");
    private void InitializeToolStrip()
    {
    
    
        // 创建工具栏
        ToolStrip toolStrip = new ToolStrip();
        toolStrip.Items.Add(new ToolStripButton("新建"));
        toolStrip.Items.Add(new ToolStripButton("打开"));
        toolStrip.Items.Add(new ToolStripButton("保存"));

        // 创建状态栏
        StatusStrip statusStrip = new StatusStrip();

        statusStrip.Items.Add(statusLabel);

        // 创建ToolStripContainer控件
        ToolStripContainer toolStripContainer = new ToolStripContainer();
        toolStripContainer.Dock = DockStyle.Fill;
        toolStripContainer.ContentPanel.BackColor = Color.White;
        toolStripContainer.TopToolStripPanel.Controls.Add(toolStrip);
        toolStripContainer.BottomToolStripPanel.Controls.Add(statusStrip);

        // 将ToolStripContainer控件添加到窗体
        Controls.Add(toolStripContainer);

        // 添加按钮事件处理
        toolStrip.Items.Add(new ToolStripButton("显示消息", null, ShowMessage_Click));
    }

    private void ShowMessage_Click(object sender, EventArgs e)
    {
    
    
        statusLabel.Text = "您点击了“显示消息”按钮!";
        statusLabel.ForeColor = Color.Green;
    }
}

Insert image description here

In this case, we define a statusLabelfield to hold the label control in the status bar. In InitializeToolStripthe method, we created the toolbar and status bar and added them to ToolStripContainerthe corresponding panels of the control. At the same time, we also added a button called "Show Message" and assigned an event handling method to it ShowMessage_Click. When the user clicks the button, ShowMessage_Clickthe method sets the label text in the status bar to a message and sets the text color to green.

Note that within the event handling method, we need to call Textthe properties and ForeColorproperties of the label control in the status bar to modify the display content and color of the status bar.

Guess you like

Origin blog.csdn.net/aa2528877987/article/details/133071483