?
Solved

Disable the Query Unload event

Posted on 2003-03-21
9
Medium Priority
?
202 Views
Last Modified: 2010-04-07
how can i disable a form's cross button at the upper right of a VB form/MDI Form so that user cannot use this button to close the form?
0
Comment
Question by:tayleeming
[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
9 Comments
 
LVL 6

Expert Comment

by:marconovaro
ID: 8179494

Copy this code:

Private Const MF_BYCOMMAND As Long = &H0
Private Const SWP_DRAWFRAME As Long = &H20
Private Const SWP_NOMOVE As Long = &H2
Private Const SWP_NOSIZE As Long = &H1
Private Const SWP_NOZORDER As Long = &H4
Private Const SWP_FLAGS = SWP_NOZORDER Or SWP_NOSIZE Or _
                         SWP_NOMOVE Or SWP_DRAWFRAME

Private Declare Function GetSystemMenu Lib "user32" _
   (ByVal hwnd As Long, _
    ByVal bRevert As Long) As Long

Private Declare Function RemoveMenu Lib "user32" _
   (ByVal hMenu As Long, _
    ByVal nPosition As Long, _
    ByVal wFlags As Long) As Long

Private Declare Function DrawMenuBar Lib "user32" _
   (ByVal hwnd As Long) As Long

Private Declare Function SetWindowPos Lib "user32" _
    (ByVal hwnd As Long, ByVal hWndInsertAfter As Long, _
     ByVal x As Long, ByVal Y As Long, _
     ByVal cx As Long, ByVal cy As Long, _
     ByVal wFlags As Long) As Long


Private Sub RemoveMenuByCmd(m_hWnd as Long, itemId As Long)
    Dim hMenu As Long

    'get the system menu handle
    hMenu = GetSystemMenu(m_hWnd, 0)

    RemoveMenu hMenu, itemId, MF_BYCOMMAND

    'force a redraw of the non-client
    'area of the form to cause the
    'disabled X to paint correctly
    Call SetWindowPos(m_hWnd, 0, 0, 0, 0, 0, SWP_FLAGS)
    Call DrawMenuBar(m_hWnd)

End Sub

And use it like this:

private const SYS_CLOSE = &HF060&


(inside Form_Load)
RemoveMenuByCmd( me.hwnd, SYS_CLOSE )


Hope this helps.
M

0
 
LVL 6

Expert Comment

by:marconovaro
ID: 8179497


For more control on system menu, have a look to my code here:

http://www.planet-source-code.com/vb/scripts/ShowCode.asp?txtCodeId=36243&lngWId=1

M
0
 
LVL 1

Expert Comment

by:Fischermaen
ID: 8179559
Hi,

there is a pretty much easier way:

Private Sub Form_QueryUnload(Cancel As Integer, UnloadMode As Integer)
   If (UnloadMode = vbFormControlMenu) Then
      Call MsgBox("I don't want you to close me in that way!")
      Cancel = True
   endif
End Sub

If you want it in silent mode, just leave the MsgBox(... away.

Greetings

Andy
0
Industry Leaders: 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 43

Accepted Solution

by:
TimCottee earned 280 total points
ID: 8179595
Hi tayleeming,

Of course there is a simple way as well as this.

Syntax

Private Sub Form_QueryUnload(cancel As Integer, unloadmode As Integer)

Private Sub MDIForm_QueryUnload(cancel As Integer, unloadmode As Integer)

The QueryUnload event syntax has these parts:

Part Description
cancel An integer. Setting this argument to any value other than 0 stops the QueryUnload event in all loaded forms and stops the form and application from closing.
unloadmode A value or constant indicating the cause of the QueryUnload event, as described in Return Values.


Return Values

The unloadmode argument returns the following values:

Constant Value Description
vbFormControlMenu 0 The user chose the Close command from the Control menu on the form.
vbFormCode 1 The Unload statement is invoked from code.
vbAppWindows 2 The current Microsoft Windows operating environment session is ending.
vbAppTaskManager 3 The Microsoft Windows Task Manager is closing the application.
vbFormMDIForm 4 An MDI child form is closing because the MDI form is closing.
vbFormOwner 5 A form is closing because its owner is closing.


If you want to check for the "X" then:

Private Sub Form_QueryUnload(cancel As Integer, unloadmode As Integer)
  If UnloadMode = vbFormControlMenu Then
    Cancel = True
  End If
End Sub

This will prevent the form from being unloaded that way but will allow all other methods for closing the form.

Tim Cottee MCSD, MCDBA, CPIM
http://www.timcottee.tk 

Brainbench MVP for Visual Basic
http://www.brainbench.com

Experts-Exchange Advisory Board Member

0
 
LVL 7

Expert Comment

by:fluglash
ID: 8179627
Or simply setting
ControlBox property of the form to false in design time
0
 
LVL 1

Expert Comment

by:Piter
ID: 8179682
hi,
 Wht Mr.fluglash  said is absolutely correct and simplest answer.
0
 
LVL 1

Expert Comment

by:Fischermaen
ID: 8179748
@fluglash

but there is one problem, if you put ControlBox to False, you disable the minimize, maximize, a.s.o. If that is what tayleeming wants to have, you've posted the simplest way (much simpler than my simple way mentioned above)

Greeting Andy
0
 
LVL 1

Expert Comment

by:Fischermaen
ID: 8179760
@fluglash

but there is one problem, if you put ControlBox to False, you disable the minimize, maximize, a.s.o. If that is what tayleeming wants to have, you've posted the simplest way (much simpler than my simple way mentioned above)

Greeting Andy
0
 

Author Comment

by:tayleeming
ID: 8184865
hi,
  thanks for everyone help, all of the answer are great but TimCottee's answer also provide some concept as well..

Thanks a lot
0

Featured Post

Industry Leaders: 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

Enums (shorthand for ‘enumerations’) are not often used by programmers but they can be quite valuable when they are.  What are they? An Enum is just a type of variable like a string or an Integer, but in this case one that you create that contains…
You can of course define an array to hold data that is of a particular type like an array of Strings to hold customer names or an array of Doubles to hold customer sales, but what do you do if you want to coordinate that data? This article describes…
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…
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…
Suggested Courses
Course of the Month14 days, 9 hours left to enroll

770 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