Enable / Disable Forms Close Button

There are times when it is desirable to disable the close button on a form. EnableCloseButton() is a simple function that does just this, and can re-enable the button as well.

Ordinarily it is quite a simple matter to disable a menu item, but VB does not like us doing this to a form's close button. As a result more work is required to trick VB into thinking that the close option no longer exists if we are to disable the button. This is all hidden from the developer in one simple function.

Disable

Enable

For example, to disable a form's close button on loading:


    PrivateSub Form_Load()
        EnableCloseButton me.hWnd, false
    End Sub

And to re-enable it later:


    private Sub Command1_Click()
        EnableCloseButton me.hWnd, true
    End Sub

EnableCloseButton() was written in VB6

Download Zipped Project File (3k)



Comments

  • If you wnat disabled maximize button, here a page.

    Posted by Legacy on 06/13/2003 12:00am

    Originally posted by: Alejandro

    i hope may help you guys
    see u

    http://www.planet-source-code.com/vb/scripts/ShowCode.asp?lngWId=1&txtCodeId=4669

    Reply
  • helpful code

    Posted by Legacy on 02/27/2003 12:00am

    Originally posted by: roshan

    help ful code

    Reply
  • Slight update to previous post...

    Posted by Legacy on 02/20/2003 12:00am

    Originally posted by: Ahmed

    Slight update to the previous code. Apologize for inconvenience :(


    Private Sub Form_KeyDown(KeyCode As Integer, Shift As Integer)

    Dim AltDown

    AltDown = (Shift And vbAltMask) > 0

    If KeyCode = vbKeyF4 And AltDown Then
    Debug.Print "Alt+F4"
    KeyCode = Empty
    Shift = Empty
    End If
    End Sub

    Reply
  • Disablng Alt+F4

    Posted by Legacy on 02/20/2003 12:00am

    Originally posted by: Ahmed

    One way to disable Alt F4 will be to have the following code in the key down event for the form and the components on the form:

    Private Sub Form_KeyDown(KeyCode As Integer, Shift As Integer)

    Dim AltDown

    AltDown = (Shift + vbAltMask) > 0

    If KeyCode = vbKeyF4 And AltDown Then
    Debug.Print "Alt+F4"
    KeyCode = Empty
    Shift = Empty
    End If
    End Sub

    Reply
  • How to add a custom button?

    Posted by Legacy on 08/07/2001 12:00am

    Originally posted by: C Sluiter

    hi, any advise on how to add a custom button to the title bar would be muchly appreciated.

    Reply
  • Where to put the disable close function

    Posted by Legacy on 07/12/2001 12:00am

    Originally posted by: Hee Ann

    When I run the code, there is a compile error.Function not define.

    Reply
  • Disable restore button

    Posted by Legacy on 09/08/2000 12:00am

    Originally posted by: Keng Tung

    How do u disable the restore button for a MDI child
    form?

    Reply
  • Enable or disable

    Posted by Legacy on 08/11/2000 12:00am

    Originally posted by: meganathan

    where i passed enableclosebutton please tell me in mail

    Reply
  • Re: Disabling Maximise Button

    Posted by Legacy on 07/23/1999 12:00am

    Originally posted by: Pete Cozens

    I haven't tried this, but I would imagine that if you use the same code, but replace all occurances of SC_CLOSE with SC_MAXIMISE (or SC_MINIMISE if you feel so inclined) then it should work. Here are the extra constants that you'll need
    Private Const SC_MAXIMIZE As Long = &HF030&
    Private Const xSC_MAXIMIZE As Long = -11&
    Private Const SC_MINIMIZE As Long = &HF020&
    Private Const xSC_MINIMIZE As Long = -12&

    Pete
    pjc@milvus.com

    Reply
  • Re: Disabling ALT-F4

    Posted by Legacy on 07/23/1999 12:00am

    Originally posted by: Pete Cozens

    To prevent ALT-F4 from closeing your app, add a handler function in your app's main form to handle the Form (or MDIForm) QueryUnload event, and set Cancel to True

    Pete
    pjc@milvus.com

    Reply
  • Loading, Please Wait ...

Leave a Comment
  • Your email address will not be published. All fields are required.

Top White Papers and Webcasts

  • As all sorts of data becomes available for storage, analysis and retrieval - so called 'Big Data' - there are potentially huge benefits, but equally huge challenges...
  • The agile organization needs knowledge to act on, quickly and effectively. Though many organizations are clamouring for "Big Data", not nearly as many know what to do with it...
  • Cloud-based integration solutions can be confusing. Adding to the confusion are the multiple ways IT departments can deliver such integration...

Most Popular Programming Stories

More for Developers

RSS Feeds

Thanks for your registration, follow us on our social networks to keep up-to-date