To gray out the close button (X) of a WinForms form, override CreateParams and add the CS_NOCLOSE class style. To remove the buttons from the title bar entirely, set ControlBox = false. Neither one stops the form from being closed in other ways, so the last section shows how to do that properly.
This is Windows Forms, so Windows-only. The project targets net8.0-windows with UseWindowsForms, as dotnet new winforms creates it:
<PropertyGroup>
<OutputType>WinExe</OutputType>
<TargetFramework>net8.0-windows</TargetFramework>
<UseWindowsForms>true</UseWindowsForms>
<Nullable>enable</Nullable>
<ImplicitUsings>enable</ImplicitUsings>
</PropertyGroup>
Disable the close button
public partial class MainForm : Form
{
private const int CS_NOCLOSE = 0x0200;
public MainForm()
{
InitializeComponent();
}
protected override CreateParams CreateParams
{
get
{
CreateParams cp = base.CreateParams;
cp.ClassStyle |= CS_NOCLOSE;
return cp;
}
}
}
CreateParams is read when Windows creates the form’s window. CS_NOCLOSE is a window class style that grays out the X button and removes Close from the window’s system menu (the menu you get by right-clicking the title bar). Minimize and maximize keep working.

Because the style is applied when the window is created, changing it later has no effect until the handle is recreated. This approach suits forms that should never show an active X, such as a progress window or a kiosk screen.
Hide all the title bar buttons
Set ControlBox to false, in the designer or in code:
public MainForm()
{
InitializeComponent();
ControlBox = false;
}
This removes the window’s system menu, which takes the icon and the minimize, maximize and close buttons with it. The title bar stays. If you see older code that clears the WS_SYSMENU style in CreateParams, it does the same thing:
private const int WS_SYSMENU = 0x00080000;
protected override CreateParams CreateParams
{
get
{
CreateParams cp = base.CreateParams;
cp.Style &= ~WS_SYSMENU;
return cp;
}
}
Use the property. It is supported, visible in the designer, and can be changed at runtime.

To hide the whole title bar, set FormBorderStyle = FormBorderStyle.None. The user then cannot move or resize the window unless you implement it yourself.
Actually prevent closing
A grayed-out button is only a hint. The form can still be closed from code, by Application.Exit(), from Task Manager, or when Windows shuts down. If closing at the wrong moment would lose data, guard the close itself:
private bool allowClose;
protected override void OnFormClosing(FormClosingEventArgs e)
{
if (e.CloseReason == CloseReason.UserClosing && !allowClose)
{
e.Cancel = true;
}
base.OnFormClosing(e);
}
private void FinishButton_Click(object? sender, EventArgs e)
{
allowClose = true;
Close();
}
CloseReason.UserClosing covers the X button, Alt+F4, the system menu and calls to Close() from your own code, which is why the flag is needed to let your own button through. Other reasons, like WindowsShutDown or TaskManagerClosing, are left alone on purpose: blocking a Windows shutdown only gets your app killed after a timeout.
Should you do this at all?
Users expect the X to work, and a window that refuses to close feels broken. Usually the better design is to let them click it and ask in OnFormClosing whether to save, discard or cancel. For a short, uninterruptible state such as a database migration, the allowClose flag is easier to switch on and off than the class style, and you can show a message explaining why the window can’t close yet.
Thanks for sharing…
If you keep the system menu but remove the close item then the close button remains but is disabled.
thank you worked for me
thank for your post,
How to use this code with C++ winform.
Thank you very Much
Good! I have been seeking for this for over an hour! Works just fine, thank you!