Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations bkrike on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Disable function of cross on form ??

Status
Not open for further replies.

Xkarlos77

Programmer
Jan 19, 2003
28
CZ
Is there any way that the window will not close when I click on the cross at the top right corner?

thank you
 
Please read thread:

thread222-343200 explains form events, and how to cancel if the user clicks on the X box on the form.

Robert
 
In the Form_QueryUnload Event place the following Code

Private Sub Form_QueryUnload(Cancel As Integer, UnloadMode As Integer)
Cancel = True
End Sub

Rob
 
rdavis,

Have you tried this code? Take Care

Matt
If at first you don't succeed, skydiving is not for you.
 
I'm just interested in how it works...

as whatever you do to unload the form...
it gets cancelled

which leaves you with a resource leak, or an app that won't ever close...

how about...
Code:
Private Sub Form_QueryUnload(Cancel As Integer, UnloadMode As Integer)
Select Case UnloadMode
  Case vbFormCode, vbAppWindows, vbAppTaskManager, _
       vbAppTaskManager
    Cancel = False
  Case vbFormControlMenu, vbFormOwner
    Cancel = True
end select
End Sub

of course, there may be syntactical errors, but I am sure you get the general structure...

Take Care

Matt
If at first you don't succeed, skydiving is not for you.
 
Oh, yes

Follow TheVampire's link Take Care

Matt
If at first you don't succeed, skydiving is not for you.
 
Well, you went a little further. I just posted a simple snippet.

Rob
 
No, you still see the cross, it just ignores when you click on it. ( as per matt's code )

A little easier way of looking at it is:

Private Sub Form_QueryUnload(Cancel As Integer, UnloadMode As Integer)

If UnloadMode = 0 Then Cancel = True ' user clicked the X box

End Sub

Robert
 
If you don't need either the minimize or maximize buttons, then you can set the ControlBox property of the form to false.

If you need the min and max buttons, you can disable the close button by using a few API's, specifically, the following four
Code:
Public Declare Function GetSystemMenu Lib "user32" (ByVal hWnd As Long, ByVal bRevert As Long) As Long
Public Declare Function GetMenuItemCount Lib "user32" (ByVal hMenu 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 Declare Function DrawMenuBar Lib "user32" (ByVal hWnd As Long) As Long

Public Const MF_BYPOSITION = &H400&
Public Const MF_REMOVE = &H1000&

Dim lSysMenu As Long
Dim lMenuCount As Long

lSysMenu = GetSystemMenu(Me.hWnd, False)
If (lSysMenu <> 0) Then
   lMenuCount = GetMenuItemCount(lSysMenu)
   If (lMenuCount > 1) Then
      RemoveMenu lSysMenu, lMenuCount - 1, (MF_BYPOSITION Or MF_REMOVE)
      DrawMenuBar Me.hWnd
      DoEvents
   End If
End If
In either case, you should then have a button or some other method available to close the form. Good Luck
--------------
As a circle of light increases so does the circumference of darkness around it. - Albert Einstein
 
strongm pointed out in another thread .. you might want to trap alt-f4 also :)

Transcend
[gorgeous]
 
I'm probably missing something here, but why go to so much trouble when you can trap the UnloadMode value in the QueryUnload event, and cancel it there?

ALT-F$ will give an unloadmode of 0 ( closed by user ) so you can cancel it. The same goes for selecting &quot;Close&quot; from the system menu.

Robert
 
In reality, it probably doesn't matter, because the end result is the same.

One could argue that from a performance standpoint, its better to disallow the event rather than handle the event due to the overhead having the handle the event every time is occurs. The word neglible does come to mind here.

From a purely philosophical standpoint, I prefer prevention, rather than reaction. Good Luck
--------------
As a circle of light increases so does the circumference of darkness around it. - Albert Einstein
 
I see. It's a philosophical matter! [yinyang] :)

Oh well, if everyone was the same, the world would be an awfully boring place!

Robert
 
Well, according to the old interface guidelines, you shouldn't offer an option that isn't currently available to the user (hence graying out of buttons, etc). Under those terms there seems to be a strong argument for the removal or visibly disablement of the close button, system menu, etc if the functionality has been disabled.

The philosphical argument then comes down to whether complete removal or just graying them out is more appropriate.
 

>you shouldn't offer an option that isn't currently available to the user

If looking at the way MS does it determines any kind of guidlines then I guess it's alright.[bugeyed] MS does it all the time...
(such as dialogs with [always]inactive control boxes)

>The philosphical argument then comes down to whether complete removal or just graying them out is more appropriate.

If the option is available sometimes (i.e. the user can perform some action(s) that will eventually lead to the option becoming avaiable), then it's not a bad idea to just grey it out.
[/b][/i][/u]*******************************************************
General remarks:
If this post contains any suggestions for the use or distribution of code, components or files of any sort, it is still your responsibility to assure that you have the proper license and distribution rights to do so!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top