Possible to code the 'X' close button?

johncassell

Well-known member
Joined
Jun 10, 2007
Messages
120
Location
Redcar, England
Programming Experience
Beginner
Hi there,

Can someone tell me if it is possible to write some code which is actioned upon a user pressing the X close button at the top of a form please?

I've heard that it is possible to disable it (Although I would like to know how to do that as well) but I would prefer to code it.

Thanks

John
 
Not sure about coding it as such, but to disable you simply add 1 line to your form closing event.

If this is an MDI application, you only need to do this on your Main Menu

VB.NET:
Private Sub Form1_Closing (...) Handles MyBase.Closing
  e.Cancel = True
End Sub
 
Last edited:
Hi Arg,

thanks for the reply, however I have cracked it..

VB.NET:
 Private Sub MainForm_FormClosed(ByVal sender As Object, ByVal e As System.Windows.Forms.FormClosedEventArgs) Handles Me.FormClosed
        MsgBox("How dare you click on the X button!")
    End Sub

Cheers

John
 
I just tried what you did - on my form when I click my "close" button (which calls Application.Exit) , it does close but I get the error first!

Also, if I click on the X , the form still closes after the user clicks on the X !!!


PS - the reason the code I showed works is because it disables anything that uses the Form_Closing event. Application.Exit() doesn't use this event, but something like me.close() does. So if you use the code I provided on an MDI application, for your exit button, you must make sure it calls Application.Exit() to close.
 
Hi Arg, not sure how it works but it does.

not sure if it makes a difference but my messagebox asks a question vbyesnocancel - if user says YES then it actions my specified code and then closes the form (without me having to specify me.close) if they do NO or CANCEL then it simply acts as if the close button wasn't pressed. This seems to be to be the easiest way to achive it with minimal effort. Also no error or anything.

Can i just ask what is wrong with using 'msgbox' - I am still too new to be aware of any implications of old coding.

Thanks

John
 
Use MsgBox, nothing wrong with that, Visual Basic Coding Conventions recommends it.

Yes you are right, This link http://msdn2.microsoft.com/en-us/library/aa289509(vs.71).aspx recommends the usage of MsgBox under the heading MsgBox versus MessageBox.Show

But these links recommends the usage of MessageBox and also they explains why http://www.nervoustych.com/blog/PermaLink,guid,0c1e89e2-4fb0-4474-b231-b2401bf8fcb6.aspx and http://www.gringod.com/2005/01/26/vbnet-msgbox-or-messageboxshow/. But I think it is better to prefer to MSDN.
 
Yes you are right, This link http://msdn2.microsoft.com/en-us/library/aa289509(vs.71).aspx recommends the usage of MsgBox under the heading MsgBox versus MessageBox.Show

But these links recommends the usage of MessageBox and also they explains why http://www.nervoustych.com/blog/PermaLink,guid,0c1e89e2-4fb0-4474-b231-b2401bf8fcb6.aspx and http://www.gringod.com/2005/01/26/vbnet-msgbox-or-messageboxshow/. But I think it is better to prefer to MSDN.

I only use msgbox for myself to see output. I use messagebox.show for user-output.
 
Disable the close button:

VB.NET:
Option Explicit

Private Declare Function GetSystemMenu Lib "user32" _
    (ByVal hwnd As Long, _
     ByVal bRevert As Long) As Long

Private Declare Function RemoveMenu Lib "user32" _
    (ByVal hMenu As Long, _
     ByVal nPosition As Long, _
     ByVal wFlags As Long) As Long
     
Private Const MF_BYPOSITION = &H400&

Public Function DisableCloseButton(frm As Form) As Boolean

'PURPOSE: Removes X button from a form
'EXAMPLE: DisableCloseButton Me
'RETURNS: True if successful, false otherwise
'NOTES:   Also removes Exit Item from
'         Control Box Menu


    Dim lHndSysMenu As Long
    Dim lAns1 As Long, lAns2 As Long
    
    
    lHndSysMenu = GetSystemMenu(frm.hwnd, 0)

    'remove close button
    lAns1 = RemoveMenu(lHndSysMenu, 6, MF_BYPOSITION)

   'Remove seperator bar
    lAns2 = RemoveMenu(lHndSysMenu, 5, MF_BYPOSITION)
    
    'Return True if both calls were successful
    DisableCloseButton = (lAns1 <> 0 And lAns2 <> 0)

End Function
 
Disable the close button:

VB.NET:
Option Explicit

Private Declare Function GetSystemMenu Lib "user32" _
    (ByVal hwnd As Long, _
     ByVal bRevert As Long) As Long

Private Declare Function RemoveMenu Lib "user32" _
    (ByVal hMenu As Long, _
     ByVal nPosition As Long, _
     ByVal wFlags As Long) As Long
     
Private Const MF_BYPOSITION = &H400&

Public Function DisableCloseButton(frm As Form) As Boolean

'PURPOSE: Removes X button from a form
'EXAMPLE: DisableCloseButton Me
'RETURNS: True if successful, false otherwise
'NOTES:   Also removes Exit Item from
'         Control Box Menu


    Dim lHndSysMenu As Long
    Dim lAns1 As Long, lAns2 As Long
    
    
    lHndSysMenu = GetSystemMenu(frm.hwnd, 0)

    'remove close button
    lAns1 = RemoveMenu(lHndSysMenu, 6, MF_BYPOSITION)

   'Remove seperator bar
    lAns2 = RemoveMenu(lHndSysMenu, 5, MF_BYPOSITION)
    
    'Return True if both calls were successful
    DisableCloseButton = (lAns1 <> 0 And lAns2 <> 0)

End Function

Initially I thought you were trying to display a confirmation dialog to the user when he/she clicks on the close button. Now I think you are trying to remove the buttons in the titlebar. If so, the only thing you have to do is to set the ControlBox Property to false.
VB.NET:
 Me.ControlBox = False
 
Me.ControlBox = False

But that also takes the maximise and minimise buttons away. I'd use that if I didn't want to display any buttons, but in an older app I wanted the min and max buttons to work, but not the close. That's when I used the e.canceled = true way.
 
PS - the reason the code I showed works is because it disables anything that uses the Form_Closing event. Application.Exit() doesn't use this event, but something like me.close() does.
With .Net 2.0 also Application.Exit raise FormClosing. Note that Closing/Closed is obsolete, FormClosing/FormClosed is to be used.

But these links recommends the usage of MessageBox and also they explains why

But I think it is better to prefer to MSDN.
Knowing the difference enables you to make a qualified choice. The document I linked are recommendations you don't have to follow or agree with. I agree with most of them but not the one about using VB runtime functions. About the converting between .Net laguages argument, one would think a converter would handle translating MsgBox.
I only use msgbox for myself to see output. I use messagebox.show for user-output.
Me too, lazy old habit, MsgBox is faster written, but MessageBox is more readable.
Disable the close button:
Why would you want to? You can handle the FormClosing event and cancel it depending on the e.CloseReason (for example UserClosing). Also this was not what OP wanted, he just wanted to do some code when form closed, which was done correctly in FormClosed event.
interesting. I think it was an MS press book I self taught from, and it taught messagebox.show !! Each to their own ;)
True.
 
Wow, didn't think this post would generate that much interest!

I don't need to disable the X button but I was being nosey to find out how it would be done (If i ever needed it) so I appreciate the input on that. I've not tried it yet but if I need to (Not sure if I ever will) I would try Davy G's code. Seems an awful lof of code for something that should have been a "true/false" option in the form properties.

But thanks none the less.
 
Back
Top