Solved

Disabling close box in VB6

Posted on 2000-03-09
6
470 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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 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

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!

Question has a verified solution.

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

There are many ways to remove duplicate entries in an SQL or Access database. Most make you temporarily insert an ID field, make a temp table and copy data back and forth, and/or are slow. Here is an easy way in VB6 using ADO to remove duplicate row…
This article describes how to use a set of graphical playing cards to create a Draw Poker game in Excel or VB6.
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…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…

717 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