Solved

Disabling close box in VB6

Posted on 2000-03-09
6
469 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
Online Training Solution

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action. Forget about retraining and skyrocket knowledge retention rates.

 
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

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
using Access 8 91
How to set the sa password in a vb6 code for sql connection 9 68
IF ELSE Statement in Excel Macro VBA 16 98
MS SQL Update query with connected table data 3 78
I’ve seen a number of people looking for examples of how to access web services from VB6.  I’ve been using a test harness I built in VB6 (using many resources I found online) that I use for small projects to work out how to communicate with web serv…
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 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…
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…

739 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