Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
?
Solved

Disable the Query Unload event

Posted on 2003-03-21
9
Medium Priority
?
203 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
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
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 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

Get your problem seen by more experts

Be seen. Boost your question’s priority for more expert views and faster solutions

Question has a verified solution.

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

Have you ever wanted to restrict the users input in a textbox to numbers, and while doing that make sure that they can't 'cheat' by pasting in non-numeric text? Of course you can do that with code you write yourself but it's tedious and error-prone …
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
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…
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…
Suggested Courses
Course of the Month10 days, 9 hours left to enroll

571 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