[WinForm Detailed Tutorial 5] MenuStrip, ContextMenuStrip, ToolStrip, and StatusStrip controls in WinForm

1.MenuStrip

MenuStrip can contain multiple menu items as a container. Important properties of MenuStrip include:

  • Name:Menu name
  • Dock: The docking position of the menu
  • Items: Collection of menu items

ToolStripMenuItem

ToolStripMenuItem is a menu item in MenuStrip and can have the following attributes and functions:

  • ShortcutKeys: Set shortcut keys for menu items, such as Alt+F, Ctrl+N, etc.
  • DropDownItems: A collection of submenus for menu items
  • Icon: Menu items can display icons by associating ImageList controls

menu item response

Menu items can register Click events in response to user clicks. There are two ways to add responsiveness to menu items:

  1. Manual addition: Manually add menu items on the design interface and register Click events for each menu item
  2. Code addition: Add menu items through code in the Load event and register it for the Click event

code example

using System;
using System.Windows.Forms;

namespace WFFormUse
{
    public partial class FrmMenuStrip : Form
    {
        public FrmMenuStrip()
        {
            InitializeComponent();
        }

        private void FrmMenuStrip_Load(object sender, EventArgs e)
        {
            //代码添加菜单项
            ToolStripMenuItem miStudent = new ToolStripMenuItem();
            miStudent.Name = "miStudent";
            miStudent.Text = "学生管理(&M)";
            //它下面还有子菜单 
            ToolStripMenuItem miAddStudent = new ToolStripMenuItem();
            miAddStudent.Name = "miAddStudent";
            miAddStudent.Text = "新增学生";
            miAddStudent.Click += MiAddStudent_Click;
            miStudent.DropDownItems.Add(miAddStudent);//添加子菜单

            StudentMenus.Items.Add(miStudent); //添加主菜单


        }

        private void MiAddStudent_Click(object sender, EventArgs e)
        {
            MForms.FrmAddStudent fAddStudent = new MForms.FrmAddStudent();
            fAddStudent.MdiParent = this;//设置当前窗体的父窗体
            fAddStudent.Show();//Mdi容器不支持ShowDialog()
        }

        //退出系统
        private void miExit_Click(object sender, EventArgs e)
        {
            Application.Exit();
        }
        //新增学生
        private void miAddStudent_Click(object sender, EventArgs e)
        {
            MForms.FrmAddStudent fAddStudent = new MForms.FrmAddStudent();
            fAddStudent.MdiParent = this;//设置当前窗体的父窗体
            fAddStudent.Show();//Mdi容器不支持ShowDialog()
        }

        //新增班级
        private void miAddClass_Click(object sender, EventArgs e)
        {
            MForms.FrmAddClass fAddClass = new MForms.FrmAddClass();
            fAddClass.MdiParent = this;
            fAddClass.Show();
        }

        private void StudentMenus_ItemClicked(object sender, ToolStripItemClickedEventArgs e)
        {

        }
    }
}

Please add image description

2.ContextMenuStrip

ContextMenuStripUsed to create and display context menus, also known as right-click menus. It is usually associated with a form or other control, and when the user right-clicks on the associated control, the context menu appears.

Attributes

ContextMenuStripSome important properties of include:

  • Items: A collection of menu items, used to store menu items.
  • Name: The name of the menu.

In addition to these, ContextMenuStrip also has other attributes, such as BackColor, ForeColor, Font etc., used to set the background color, foreground color, font, etc. of the menu.

method

  • Show: Display context menu. This method has multiple overloaded versions, and you can specify parameters such as display position.
  • Hide:Hide context menu.
  • Dispose: Release the resources used by the ContextMenuStrip control.

Official documentation: https://learn.microsoft.com/zh-cn/dotnet/api/system.windows.forms.contextmenustrip?view=windowsdesktop-7.0&viewFallbackFrom=net-7.0

Example:

namespace WinFormsTest
{
    public partial class frmContextMenuStrip : Form
    {
        public frmContextMenuStrip()
        {
            InitializeComponent();
        }

        private void ContextMenuStrip_Load(object sender, EventArgs e)
        {

        }

        private void loginToolStripMenuItem_Click(object sender, EventArgs e)
        {
            FrmUser fAddStudent = new FrmUser();
            fAddStudent.MdiParent = this;//设置当前窗体的父窗体
            fAddStudent.Show();//Mdi容器不支持ShowDialog()
        }

        private void redToolStripMenuItem_Click(object sender, EventArgs e)
        {
            button2.BackColor = Color.Red;
        }

        private void greenToolStripMenuItem_Click(object sender, EventArgs e)
        {
            button2.BackColor = Color.Green;
        }

        private void 改字体颜色ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            button1.BackColor = Color.Green;
        }
    }
}

Please add image description

3.ToolStrip

ToolStrip is typically used to create toolbars that provide quick access to frequently used commands or functions. ToolStrip can contain multiple items of different types, such as buttons, labels, drop-down buttons, etc.

Attributes

ToolStripImportant properties include:

  • Items: A collection of items, used to store various items in ToolStrip.
  • DisplayStyle: Used to set the display mode of pictures and text in items.

Items in ToolStrip

ToolStripcan contain the following types of items:

  • ToolStripButton: Button, you can register Click event to respond to the user's click operation.
  • ToolStripLabel: Tag, you can also register Click event.
  • ToolStripSplitButton: Split button, including a button part and a drop-down part, you can register ButtonClick event to respond to the click operation of the button part, you can also register Click event to respond to the click operation of the drop-down item.
  • ToolStripDropDownButton: The drop-down button can contain multiple drop-down items, and the DropDownItemClicked event can be registered to respond to the click operation of the drop-down items.
  • ToolStripComboBox: Drop-down combo box, you can register the SelectedIndexChanged event to respond to the change of the selected item.
  • ToolStripTextBox: Text input box, you can register TextChanged event to respond to text changes.
  • ToolStripProgressBar: Progress bar, usually no need to register events.

Example

Please add image description

4.StatusStrip

StatusStripControls are usually used to display status information, progress bars, etc. at the bottom of the window.

Common properties:

  • Dock: Controls the docking position of StatusStrip. The default value is Bottom, which means it is docked at the bottom of the window.
  • Items: Represents the collection of all sub-controls contained inStatusStrip.

use:

  • Display account information.
  • Display character information.
  • Displays operating location information.
  • Show progress bar.
  • Display version information.

Example:

namespace WinFormsTest
{
    public partial class frmStatusStrip : Form
    {
        public frmStatusStrip()
        {
            InitializeComponent();
        }
        private void frmStatusStrip_Load(object sender, EventArgs e)
        {
            // 创建StatusStrip控件
            StatusStrip statusStrip = new StatusStrip();
            statusStrip.Dock = DockStyle.Bottom;
            // 添加账号信息
            ToolStripStatusLabel accountLabel = new ToolStripStatusLabel();
            accountLabel.Text = "账号: user1";
            statusStrip.Items.Add(accountLabel);
            // 添加角色信息
            ToolStripStatusLabel roleLabel = new ToolStripStatusLabel();
            roleLabel.Text = "角色: 管理员";
            statusStrip.Items.Add(roleLabel);
            // 添加操作位置信息
            ToolStripStatusLabel locationLabel = new ToolStripStatusLabel();
            locationLabel.Text = "操作位置: 主界面";
            statusStrip.Items.Add(locationLabel);
            // 添加进度条
            ToolStripProgressBar progressBar = new ToolStripProgressBar();
            progressBar.Value = 50;
            statusStrip.Items.Add(progressBar);
            // 添加版本信息
            ToolStripStatusLabel versionLabel = new ToolStripStatusLabel();
            versionLabel.Text = "版本: v1.0";
            statusStrip.Items.Add(versionLabel);
            // 将StatusStrip控件添加到窗口的Controls集合中
            this.Controls.Add(statusStrip);
        }
    }
}

Please add image description

[WinForm Detailed Tutorial] How to obtain source code

Insert image description here

Excellent recommendations:
[C# Advanced 1] Array (Array), collection (ArrayList, Queue, Stack, HashList), List
[C# Advanced 8] Serialization and deserialization in C# (binary serialization, XML serialization and JSON serialization) a>

[Advanced C#] Summary of some common knowledge points in C# syntax
[WinForm detailed tutorial 1] Form, Label, TextBox and Button controls, RadioButton and CheckBox, ListBox
[WinForm detailed tutorial three] NumericUpDown, PictureBox, RichTextBox and three Timer controls in WinForm
[WinForm detailed tutorial four] ProgressBar and ImageList in WinForm and ListView control

[C# Advanced] Delegates, events, callback functions, anonymous functions and lambda expressions in C#
I hope it helps, and you are welcome to follow me. I will update more later related information!

Guess you like

Origin blog.csdn.net/QH2107/article/details/134192251