本页在同一专题内给出两套可运行路径:案例 A 用传统 Form + MenuStrip / ToolStrip / StatusStrip;案例 B 用现代化 KryptonForm + KryptonToolStrip / KryptonStatusStrip(菜单按 LCode 工具箱实际项说明)。面向 .NET Framework 4.8;凡有代码均同时给出 C# 5.0 与 VB.NET。设计器操作以中文界面为准,工具箱显示名见下文对照表。

概述:传统 vs 现代化(Krypton)

菜单栏放命令入口,工具栏放高频快捷按钮,状态栏显示就绪/进度等信息。LCode 工具箱(SharpDevelopControlLibrary.sdcl)对两类控件均有收录:

角色 传统控件(WinForms) 工具箱显示名 现代化控件(Krypton) 工具箱显示名
窗体基类 Form (模板默认) KryptonForm 窗体
菜单栏 MenuStrip 菜单条 无独立 KryptonMenuStrip
工具栏 ToolStrip 工具条 KryptonToolStrip 工具条
状态栏 StatusStrip 状态条 KryptonStatusStrip 状态条
右键菜单 ContextMenuStrip 上下文菜单条 KryptonContextMenu 上下文菜单
功能区(可选) KryptonRibbon Ribbon
选型建议:要经典「文件 / 编辑」顶栏菜单,用案例 A,或在 KryptonForm 上继续放传统 MenuStrip;要跟主题的工具栏/状态栏,用案例 B 的 KryptonToolStrip / KryptonStatusStrip;要 Office 风格功能区,用 KryptonRibbon(须放在 KryptonForm 上)。右键菜单现代化请用 KryptonContextMenu,它不能替代顶栏 MenuStrip

案例 A · 传统控件

目标:在默认 Form 上组合菜单栏、工具栏与状态栏,并实现「文件 → 退出」与状态文字刷新。假设已按 窗体应用入门 建好「Windows 应用程序」项目,主窗体为 MainForm

设计器步骤

  1. 在项目浏览器中双击 MainForm,底部切到设计页签。
  2. 打开工具箱,在「Windows Forms」相关分类中找到并拖放到设计面:
    • 菜单条MenuStrip)— 通常停靠顶部;拖入后窗体 MainMenuStrip 会指向它。
    • 工具条ToolStrip)— 停靠在菜单下方(Dock = Top)。
    • 状态条StatusStrip)— 停靠底部(Dock = Bottom)。
  3. 在菜单条上点「在此处键入」,建顶级项「文件(&F)」;再在其下添加「退出(&X)」。属性面板把退出项 Name 设为 exitToolStripMenuItem
  4. 选中工具条,用项集合编辑器(或智能标记)添加 ToolStripButtonName 设为 btnExitText 设为「退出」,按需设 DisplayStyle(仅文本 / 图像与文本)。
  5. 选中状态条,添加 ToolStripStatusLabelName 设为 statusLabelText 设为「就绪」,Spring = True 以便占满剩余宽度。
  6. 属性面板切到事件:为 exitToolStripMenuItembtnExit 的「单击」各生成处理程序(也可共用一个方法)。
提示:优先在设计器完成布局与命名,业务逻辑写在窗体主文件;不要手改 InitializeComponent。拖拽与源码/设计切换见 文件与编辑器

关键代码(C# / VB.NET)

下列示例假设设计器已生成 menuStrip1toolStrip1statusStrip1exitToolStripMenuItembtnExitstatusLabel。退出与工具栏按钮可绑到同一处理程序:

C#
using System;
using System.Windows.Forms;

public partial class MainForm : Form
{
    public MainForm()
    {
        InitializeComponent();
        // 菜单与工具栏共用退出逻辑
        exitToolStripMenuItem.Click += Exit_Click;
        btnExit.Click += Exit_Click;
        statusLabel.Text = "就绪";
    }

    private void Exit_Click(object sender, EventArgs e)
    {
        statusLabel.Text = "正在退出…";
        Close();
    }
}
VB.NET
Imports System
Imports System.Windows.Forms

Public Class MainForm
    Inherits Form

    Public Sub New()
        InitializeComponent()
        AddHandler exitToolStripMenuItem.Click, AddressOf Exit_Click
        AddHandler btnExit.Click, AddressOf Exit_Click
        statusLabel.Text = "就绪"
    End Sub

    Private Sub Exit_Click(sender As Object, e As EventArgs)
        statusLabel.Text = "正在退出…"
        Close()
    End Sub
End Class

若希望完全用代码搭建三栏(便于对照 Designer 结果),也可在构造函数中动态创建:

C#
using System;
using System.Windows.Forms;

public class MainForm : Form
{
    private MenuStrip menuStrip;
    private ToolStrip toolStrip;
    private StatusStrip statusStrip;
    private ToolStripStatusLabel statusLabel;

    public MainForm()
    {
        Text = "传统菜单 / 工具栏 / 状态栏";
        Width = 640;
        Height = 400;

        menuStrip = new MenuStrip();
        ToolStripMenuItem fileMenu = new ToolStripMenuItem("文件(&F)");
        ToolStripMenuItem exitItem = new ToolStripMenuItem("退出(&X)");
        exitItem.Click += Exit_Click;
        fileMenu.DropDownItems.Add(exitItem);
        menuStrip.Items.Add(fileMenu);
        MainMenuStrip = menuStrip;

        toolStrip = new ToolStrip();
        ToolStripButton btnExit = new ToolStripButton("退出");
        btnExit.Click += Exit_Click;
        toolStrip.Items.Add(btnExit);

        statusStrip = new StatusStrip();
        statusLabel = new ToolStripStatusLabel("就绪");
        statusLabel.Spring = true;
        statusStrip.Items.Add(statusLabel);

        Controls.Add(statusStrip);
        Controls.Add(toolStrip);
        Controls.Add(menuStrip);
    }

    private void Exit_Click(object sender, EventArgs e)
    {
        statusLabel.Text = "正在退出…";
        Close();
    }
}
VB.NET
Imports System
Imports System.Windows.Forms

Public Class MainForm
    Inherits Form

    Private menuStrip As MenuStrip
    Private toolStrip As ToolStrip
    Private statusStrip As StatusStrip
    Private statusLabel As ToolStripStatusLabel

    Public Sub New()
        Text = "传统菜单 / 工具栏 / 状态栏"
        Width = 640
        Height = 400

        menuStrip = New MenuStrip()
        Dim fileMenu As New ToolStripMenuItem("文件(&F)")
        Dim exitItem As New ToolStripMenuItem("退出(&X)")
        AddHandler exitItem.Click, AddressOf Exit_Click
        fileMenu.DropDownItems.Add(exitItem)
        menuStrip.Items.Add(fileMenu)
        MainMenuStrip = menuStrip

        toolStrip = New ToolStrip()
        Dim btnExit As New ToolStripButton("退出")
        AddHandler btnExit.Click, AddressOf Exit_Click
        toolStrip.Items.Add(btnExit)

        statusStrip = New StatusStrip()
        statusLabel = New ToolStripStatusLabel("就绪")
        statusLabel.Spring = True
        statusStrip.Items.Add(statusLabel)

        Controls.Add(statusStrip)
        Controls.Add(toolStrip)
        Controls.Add(menuStrip)
    End Sub

    Private Sub Exit_Click(sender As Object, e As EventArgs)
        statusLabel.Text = "正在退出…"
        Close()
    End Sub
End Class
提示:控件加入 Controls 的顺序会影响 Z 序;菜单条通常最后加入(或靠 Dock)才能正确贴顶。运行验证见 运行与编译

案例 B · 现代化控件(Krypton)

目标:在 KryptonForm 上使用跟随主题的工具栏与状态栏。LCode 工具箱没有 KryptonMenuStrip;顶栏菜单常用做法是:继续放传统 MenuStrip,或改用 KryptonRibbon 功能区。右键菜单用 KryptonContextMenu

设计器步骤

  1. 新建或打开窗体后,将基类改为 KryptonForm(需引用 Krypton.Toolkit)。也可通过工程 → 添加窗口创建已继承 KryptonForm 的窗体。详见 窗体应用入门「现代化窗口」。
  2. 打开设计面,从工具箱「LCode Components」拖入:
    • 工具条KryptonToolStrip)— Dock = Top
    • 状态条KryptonStatusStrip)— Dock = Bottom
  3. (菜单栏等价)任选其一:
    • 再拖传统菜单条MenuStrip)到同一 KryptonForm,建「文件 → 退出」——外观仍是系统菜单,但可与 Krypton 工具栏/状态栏混用。
    • 或拖 RibbonKryptonRibbon)到 KryptonForm(不可放在普通 Form 上),用功能区承载命令。Ribbon 分组与按钮细节见控件手册。
  4. KryptonToolStrip 上添加 ToolStripButton(子项类型与传统工具栏相同),NamebtnExit
  5. KryptonStatusStrip 上添加 ToolStripStatusLabelNamestatusLabel
  6. 需要右键菜单时,拖上下文菜单KryptonContextMenu)到组件托盘,配置项后赋给控件的 KryptonContextMenu 属性(不要当成顶栏菜单用)。

关键代码(KryptonForm + 工具栏/状态栏 + MenuStrip)

C#
using System;
using System.Windows.Forms;
using Krypton.Toolkit;

public class MainForm : KryptonForm
{
    private MenuStrip menuStrip;
    private KryptonToolStrip toolStrip;
    private KryptonStatusStrip statusStrip;
    private ToolStripStatusLabel statusLabel;

    public MainForm()
    {
        Text = "现代化工具栏 / 状态栏";
        Width = 640;
        Height = 400;

        menuStrip = new MenuStrip();
        ToolStripMenuItem fileMenu = new ToolStripMenuItem("文件(&F)");
        ToolStripMenuItem exitItem = new ToolStripMenuItem("退出(&X)");
        exitItem.Click += Exit_Click;
        fileMenu.DropDownItems.Add(exitItem);
        menuStrip.Items.Add(fileMenu);
        MainMenuStrip = menuStrip;

        toolStrip = new KryptonToolStrip();
        toolStrip.Dock = DockStyle.Top;
        toolStrip.GripStyle = ToolStripGripStyle.Hidden;
        ToolStripButton btnExit = new ToolStripButton("退出");
        btnExit.Click += Exit_Click;
        toolStrip.Items.Add(btnExit);

        statusStrip = new KryptonStatusStrip();
        statusStrip.Dock = DockStyle.Bottom;
        statusLabel = new ToolStripStatusLabel("就绪");
        statusLabel.Spring = true;
        statusStrip.Items.Add(statusLabel);

        Controls.Add(statusStrip);
        Controls.Add(toolStrip);
        Controls.Add(menuStrip);
    }

    private void Exit_Click(object sender, EventArgs e)
    {
        statusLabel.Text = "正在退出…";
        Close();
    }
}
VB.NET
Imports System
Imports System.Windows.Forms
Imports Krypton.Toolkit

Public Class MainForm
    Inherits KryptonForm

    Private menuStrip As MenuStrip
    Private toolStrip As KryptonToolStrip
    Private statusStrip As KryptonStatusStrip
    Private statusLabel As ToolStripStatusLabel

    Public Sub New()
        Text = "现代化工具栏 / 状态栏"
        Width = 640
        Height = 400

        menuStrip = New MenuStrip()
        Dim fileMenu As New ToolStripMenuItem("文件(&F)")
        Dim exitItem As New ToolStripMenuItem("退出(&X)")
        AddHandler exitItem.Click, AddressOf Exit_Click
        fileMenu.DropDownItems.Add(exitItem)
        menuStrip.Items.Add(fileMenu)
        MainMenuStrip = menuStrip

        toolStrip = New KryptonToolStrip()
        toolStrip.Dock = DockStyle.Top
        toolStrip.GripStyle = ToolStripGripStyle.Hidden
        Dim btnExit As New ToolStripButton("退出")
        AddHandler btnExit.Click, AddressOf Exit_Click
        toolStrip.Items.Add(btnExit)

        statusStrip = New KryptonStatusStrip()
        statusStrip.Dock = DockStyle.Bottom
        statusLabel = New ToolStripStatusLabel("就绪")
        statusLabel.Spring = True
        statusStrip.Items.Add(statusLabel)

        Controls.Add(statusStrip)
        Controls.Add(toolStrip)
        Controls.Add(menuStrip)
    End Sub

    Private Sub Exit_Click(sender As Object, e As EventArgs)
        statusLabel.Text = "正在退出…"
        Close()
    End Sub
End Class
扩展阅读:KryptonFormKryptonToolStripKryptonStatusStripKryptonContextMenuKryptonRibbon。属性大表以控件手册为准,本页只保留专题要点。

属性 / 事件要点

对象 属性 / 事件 说明
Form / KryptonForm MainMenuStrip 指定主菜单条;有 MenuStrip 时应赋值,否则 Alt 快捷键等行为可能异常
MenuStrip / ToolStrip / StatusStrip Dock 菜单/工具栏多为 Top,状态栏为 Bottom
ToolStrip / KryptonToolStrip Items 按钮、分隔线、下拉、组合框等子项集合
ToolStrip / KryptonToolStrip GripStyle Hidden 可隐藏左侧拖动手柄
ToolStripItem Click 菜单项与工具栏按钮的常用单击事件;可共用同一处理程序
ToolStripStatusLabel Text / Spring 状态文案;Spring = true 时标签伸展占满剩余空间
ToolStripMenuItem ShortcutKeys 快捷键(如 Ctrl+S);需与菜单文本中的 & 加速键区分开
KryptonContextMenu Items 右键菜单项集合;赋给控件的 KryptonContextMenu 属性后弹出
KryptonRibbon (分组 / 按钮) 仅可放在 KryptonForm;无独立 RibbonForm 类

常见问题与「暂不支持」项

相关阅读