Friends, In VB.net Form properties, There are Maximize Box and Minimize Box.If we make them false, those prperties will be disabled.Likewise there is a Close Box at the right top corner of the form.How can we disable this? Is there any way at design time to disable this? If not,Can we disable it through code?

Dani AI

Generated

Nice additions from and . A few extra options, depending on the UX you want. If you only need to hide the title-bar buttons entirely, you can set the form’s ControlBox to False; that removes the caption box (the title-bar buttons) in one go. Note that going borderless changes how the caption is drawn and what buttons can appear, so plan for how the user will move the window or exit.

If you want to keep Minimize/Maximize but disable only Close, there is no built-in CloseBox property, but you can do it safely in two ways.

  1. Add CS_NOCLOSE to the window class style so the X is shown but disabled (grayed). This leaves Min/Max intact:
' In your form class
Protected Overrides ReadOnly Property CreateParams As CreateParams
    Get
        Const CS_NOCLOSE As Integer = &H200
        Dim cp = MyBase.CreateParams
        cp.ClassStyle = cp.ClassStyle Or CS_NOCLOSE
        Return cp
    End Get
End Property

CS_NOCLOSE disables Close on the window menu, which also disables the title-bar Close button.

  1. Disable the Close command in the system menu at runtime:
' In your form class
Imports System.Runtime.InteropServices

<DllImport("user32.dll")>
Private Shared Function GetSystemMenu(hWnd As IntPtr, bRevert As Boolean) As IntPtr
End Function

<DllImport("user32.dll")>
Private Shared Function EnableMenuItem(hMenu As IntPtr, uIDEnableItem As UInteger, uEnable As UInteger) As Boolean
End Function

Private Const SC_CLOSE As UInteger = &HF060UI
Private Const MF_BYCOMMAND As UInteger = &H0UI
Private Const MF_GRAYED As UInteger = &H1UI

Protected Overrides Sub OnShown(e As EventArgs)
    MyBase.OnShown(e)
    Dim hMenu = GetSystemMenu(Me.Handle, False)
    EnableMenuItem(hMenu, SC_CLOSE, MF_BYCOMMAND Or MF_GRAYED)
End Sub

This grays the Close option on the system menu (and the X).

Whichever route you choose, still gate shutdown paths in FormClosing and check e.CloseReason so you can allow Windows shutdown, your own Exit command, etc., while blocking user clicks of the X when needed (tie this to the flag mentioned).

Recommended Answers

All 3 Replies

You could set the FromBorderStyle property to None.

In the FormClosing you could always do

e.Cancel = True

But you should also give the user some other way to exit. For example, if the user selects Exit from a menu you could set a class flag as in

Private ReallyExit As Boolean = False

Private Sub Form1_FormClosing(sender As System.Object, e As System.Windows.Forms.FormClosingEventArgs) Handles MyBase.FormClosing
    e.Cancel = ReallyExit
End Sub

Just make sure that somewhere in your code you do

ReallyExit = True

Thank you all ..Making Formboarder style to None is easier and convenient..

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.