Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Disabling close box in VB6

Posted on 2000-03-09
6
Medium Priority
?
477 Views
Last Modified: 2010-05-02
Hi,

I have VB6 and I want to disable the close box (top right corner of form) on a form, so the user can only exit the program by pressing the exit button. Is there an easy way of doing this.

Alternatively, is there a way of attaching code to the close box, so if the user does click it to close, it will execute some code, i.e. to save data, etc..
0
Comment
Question by:mari_carmen
  • 3
  • 2
6 Comments
 
LVL 7

Expert Comment

by:Vbmaster
ID: 2602037
Add this to a module if you want it to work with more than one form, otherwhise just replace all 'Public' strings to 'Private'...


Public Declare Function GetSystemMenu Lib "user32" (ByVal hwnd As Long, ByVal bRevert As Long) As Long
Public Declare Function RemoveMenu Lib "user32" (ByVal hMenu As Long, ByVal nPosition As Long, ByVal wFlags As Long) As Long
Public Const MF_BYPOSITION = &H400&

Public Sub DisableCloseWindowButton(frm As Form)

    Dim hSysMenu As Long

    'Get the handle to this windows
    'system menu
    hSysMenu = GetSystemMenu(frm.hwnd, 0)

    'Remove the Close menu item
    'This will also disable the close button
    RemoveMenu hSysMenu, 6, MF_BYPOSITION

    'Lastly, we remove the seperator bar
    RemoveMenu hSysMenu, 5, MF_BYPOSITION

End Sub



Now use the code...

    Call DisableCloseWindowButton(Me)

....in the Form_Load event.
0
 
LVL 7

Expert Comment

by:Vbmaster
ID: 2602049
For the other solution to do this (without disabling the close button) you can use the form's QueryUnload event just check the value of the UnloadMode parameter, if its vbFormControlMenu then the user has used either the Close item in the system menu (the popupmenu shown when you click the upper left corner of the form) and the 'X' button in the top right of the form.

Private Sub Form_QueryUnload(Cancel As Integer, UnloadMode As Integer)

   If (UnloadMode = vbFormControlMenu) Then
      'Save data etc here
   End If
   
End Sub
0
 
LVL 32

Expert Comment

by:Erick37
ID: 2602050
Example using a prompt:

Private Sub Form_QueryUnload(Cancel As Integer, UnloadMode As Integer)
    If (UnloadMode = vbFormControlMenu) Then 'User hit [X}
        Cancel = (MsgBox("Really Quit?", vbYesNo) = vbNo)
    End If
End Sub
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 7

Accepted Solution

by:
Vbmaster earned 320 total points
ID: 2602051
Above.
0
 
LVL 32

Expert Comment

by:Erick37
ID: 2602090
If you need to disable the [X] button, here is code which will work with both SDI and MDI forms.  The code above does not work for MDI child forms.

"VBnet System Services: Killing Any Form's Close Menu and 'X' Button - SDI, MDI"
http://www.mvps.org/vbnet/code/system/killclosemdi.htm
0
 

Author Comment

by:mari_carmen
ID: 2602135
VBMaster

Excellent, Thanks very much.
0

Featured Post

Important Lessons on Recovering from Petya

In their most recent webinar, Skyport Systems explores ways to isolate and protect critical databases to keep the core of your company safe from harm.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Since upgrading to Office 2013 or higher installing the Smart Indenter addin will fail. This article will explain how to install it so it will work regardless of the Office version installed.
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…
Suggested Courses

926 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question