Solved

Disabling close box in VB6

Posted on 2000-03-09
6
466 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
IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 
LVL 7

Accepted Solution

by:
Vbmaster earned 80 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

What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

The debugging module of the VB 6 IDE can be accessed by way of the Debug menu item. That menu item can normally be found in the IDE's main menu line as shown in this picture.   There is also a companion Debug Toolbar that looks like the followin…
If you have ever used Microsoft Word then you know that it has a good spell checker and it may have occurred to you that the ability to check spelling might be a nice piece of functionality to add to certain applications of yours. Well the code that…
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…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…

743 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

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now