Solved

Disabling close box in VB6

Posted on 2000-03-09
6
467 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
Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

 
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

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Problem to skip loop 6 56
Put text in a picture ASP.NET C# 2 56
Macro Excel - Multiple If conditions 2 71
VB6 - Scroll Mouse wheel on Picturebox 13 42
When trying to find the cause of a problem in VBA or VB6 it's often valuable to know what procedures were executed prior to the error. You can use the Call Stack for that but it is often inadequate because it may show procedures you aren't intereste…
I was working on a PowerPoint add-in the other day and a client asked me "can you implement a feature which processes a chart when it's pasted into a slide from another deck?". It got me wondering how to hook into built-in ribbon events in Office.
Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…
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…

813 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

9 Experts available now in Live!

Get 1:1 Help Now