You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何禁用KryptonPage的关闭按钮及右键菜单关闭选项

Got it, let's fix this problem step by step. You're already partway there with clearing the DockingAllowClose flag, but we need a couple more adjustments to fully block page closures and remove all related close options.

Solution

1. Lock Down Individual KryptonPage Closure Permissions

First, make sure you're explicitly disabling the close flag for all pages you don't want to be closed:

  • For your tools pages: Keep using ClearFlags(KryptonPageFlags.DockingAllowClose) — this hides the docked page's close button and removes the "Close" option from the right-click context menu automatically.
  • For your main workspace page (NewWinControl), if you also want to block its closure, add KryptonPageFlags.DockingAllowClose to the list of flags you clear.

2. Intercept Close Requests at the Docking Manager Level

Even with flags set, there might be edge cases where a close request is triggered (like keyboard shortcuts or programmatic calls). To fully block this, subscribe to the KryptonDockingManager's PageCloseRequest event and cancel all close attempts (or filter based on page if you want some pages to remain closable).

3. Verify UI Elements Are Hidden

When you clear the DockingAllowClose flag, the Krypton control automatically handles hiding the close button and removing the right-click close option. No extra steps are needed here if the flag is set correctly.


Modified Code for Your frmMain

Here's how to update your existing code to implement all these fixes:

public partial class frmMain : KryptonForm
{
    public frmMain()
    {
        InitializeComponent();
        kryptonPanel1.Dock = DockStyle.Fill;
        kryptonDockableWorkspace.Dock = DockStyle.Fill;
        kryptonPanel.Dock = DockStyle.Fill;
    }

    private void Form1_Load(object sender, EventArgs e)
    {
        KryptonDockingWorkspace w = kryptonDockingManager.ManageWorkspace(kryptonDockableWorkspace);
        kryptonDockingManager.ManageControl(kryptonPanel, w);
        kryptonDockingManager.ManageFloating(this);
        kryptonDockingManager.AddDockspace("Control", DockingEdge.Left, new KryptonPage[] { NewWinTools() });
        kryptonDockingManager.AddDockspace("Control", DockingEdge.Bottom, new KryptonPage[] { NewWinTools(), NewWinTools(), NewWinTools(), NewWinTools() , NewWinTools() });
        kryptonDockingManager.AddToWorkspace("Workspace", new KryptonPage[] { NewWinControl() });

        // Subscribe to the close request event to block all closure attempts
        kryptonDockingManager.PageCloseRequest += KryptonDockingManager_PageCloseRequest;
    }

    private void KryptonDockingManager_PageCloseRequest(object sender, PageCloseRequestEventArgs e)
    {
        // Cancel the close request - remove this check if you want specific pages to remain closable
        e.Cancel = true;
    }

    private KryptonPage NewPage(string name, int image, Control content)
    {
        KryptonPage p = new KryptonPage();
        p.Text = name;
        p.TextTitle = name;
        p.TextDescription = name;
        p.ImageSmall = imageListSmall.Images[image];
        content.Dock = DockStyle.Fill;
        p.Controls.Add(content);
        return p;
    }

    private KryptonPage NewWinControl()
    {
        UserControl1 br = new UserControl1();
        KryptonPage page = NewPage("New Tab", 0, br);
        // Add DockingAllowClose to block closure of this main workspace page
        page.ClearFlags(KryptonPageFlags.DockingAllowAutoHidden | KryptonPageFlags.DockingAllowDocked | KryptonPageFlags.DockingAllowClose);
        return page;
    }

    private KryptonPage NewWinTools()
    {
        UserControl1 br = new UserControl1();
        KryptonPage page = NewPage("Tools 1 ", 2, br);
        // Keep this to disable close button and right-click close option
        page.ClearFlags(KryptonPageFlags.DockingAllowClose);
        return page;
    }
}

Notes

  • If you want some pages to remain closable, modify the PageCloseRequest event handler to check e.Page properties (like Text or a custom tag) and only cancel for specific pages.
  • The ClearFlags method removes the specified flag from the page's settings, which tells the Krypton control not to render close UI elements or allow close operations.

内容的提问来源于stack exchange,提问作者Rajesh Smartwebtech

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.14 08:57:06