Visually remove / disable the close button in the title bar.

I was asked to remove or disable the close button from our VDI.NET 2005 MDI application. There are no built-in properties on the form that allow gray to close the close button so that the user cannot close it, and I don’t remember anything in the form class that allows me to do this.

Perhaps there is an API call or some magic property to set or a function to call in .NET 2005 or later?

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

Additional Information:

I need to support the minimize / maximize function

I need to keep the original title bar because the methods for drawing the form are already very complicated.

+7
titlebar mdi
source share
13 answers

Based on the latest information added to your question, skip to the end of my answer.


This is what you need to set to false: Form.ControlBox Property

BUT, you will lose the minimize and maximize buttons, as well as the application menu (top left).

Alternatively, override OnClose and set Cancel to true (C # example):

protected override void OnFormClosing(FormClosingEventArgs e) { if (e.CloseReason != CloseReason.WindowsShutDown && e.CloseReason != CloseReason.ApplicationExitCall) { e.Cancel = true; } base.OnFormClosing(e); } 

If none of these solutions is acceptable and you should only disable the close button, you can go to the pinvoke / createparams route:

How to disable the close button from a window form using a .NET application

This is the jdm code version of VB version:

 Private Const CP_NOCLOSE_BUTTON As Integer = &H200 Protected Overloads Overrides ReadOnly Property CreateParams() As CreateParams Get Dim myCp As CreateParams = MyBase.CreateParams myCp.ClassStyle = myCp.ClassStyle Or CP_NOCLOSE_BUTTON Return myCp End Get End Property 
+26
source share

You can disable the close button and the close menu item in the system menu by changing the "class style" in the window. Add the following code to the form:

 const int CS_NOCLOSE = 0x200; protected override CreateParams CreateParams { get { CreateParams cp = base.CreateParams; cp.ClassStyle |= CS_NOCLOSE; return cp; } } 

This will not only stop closing the window, but it will actually be a gray button. This is C #, but I think it should be easy to translate to VB.

+8
source share

Here is an easy way to remove the close button:
1. Select a form
2. Now go to Properties .
3. Locate the ControlBox and change the value to False .

This will delete all control buttons (for example, Minimize, Maximize, Exit), as well as the icon in the left corner in front of the title.

+6
source share

You can override the OnClose event of a form. This is usually when the application is minimized to the system tray when it is "closed."

+3
source share

When you click the X button in the form. First, Form1_Closing is Form1_Closing , then Form1_Closed is Form1_Closed .

e.Cancel = True in Form1_Closing - prevents the call of Form1_Closed , thereby maintaining the active form.

+2
source share

You can set the ControlBox property to False , but the entire title bar will disappear, but the title itself ...

+1
source share

Prevent closing the form, but hide it:

 Private Sub Form1_Closing(ByVal sender As System.Object, ByVal e As System.ComponentModel.CancelEventArgs) Handles MyBase.Closing Me.WindowState = FormWindowState.Minimized Me.Visible=false e.Cancel = True End Sub 
+1
source share

What jmweb said here is also good. The X button will not close if you cancel the event when you close the form. But at the same time you need to free the processes necessary for the form, and then close the form.

 Me.Dispose() Me.Close() 

This worked for me using the Strip menu.

0
source share

Select (or click) the form itself. Click on the events in the properties window (small lightning icon). Find Form.Closing and double-click on it. Then enter: e.cancel=true

0
source share

Creating a titleless form in Visual Basic.

Go to Form Properties and set both ControlBox and ShowIcon to false.

Then clear all text from the form property of text .

0
source share

go to properties and choose from a bored style since none

-one
source share

Just select the desired form and set controlBox = false in the properties section. It just worked for me :)

-2
source share
 Private Sub Form1_Closing(ByVal sender As System.Object, ByVal e As System.ComponentModel.CancelEventArgs) Handles MyBase.Closing Beep() e.Cancel = True End Sub 
-3
source share

All Articles