UserForm - Disable the 'x' button

R

RB Smissaert

In the userform code module have this code:

Option Explicit
Private Declare Function GetActiveWindow Lib "user32" () As Long

Private Sub UserForm_Activate()
DisableCloseButton GetActiveWindow()
End Sub

Private Sub CommandButton1_Click()
Unload Me
End Sub


In a normal code module have this code:

Option Explicit
Private Declare Function DrawMenuBar Lib "user32" _
(ByVal hWnd As Long) As Long
Private Declare Function GetMenuItemCount Lib "user32" _
(ByVal hMenu As Long) As Long
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 Const MF_BYPOSITION = &H400
Private Const MF_REMOVE = &H1000

Sub DisableCloseButton(hWnd As Long)

Dim hMenu As Long
Dim menuItemCount As Long

hMenu = GetSystemMenu(hWnd, 0)

If hMenu Then
menuItemCount = GetMenuItemCount(hMenu)
RemoveMenu hMenu, _
menuItemCount - 1, _
MF_REMOVE Or MF_BYPOSITION
DrawMenuBar hWnd
End If

End Sub

Sub LoadForm()
Load UserForm1
UserForm1.Show
End Sub


RBS
 
G

Guest

Try this:-

Private Sub UserForm_QueryClose(Cancel As Integer, CloseMode As Integer)
If CloseMode = vbFormControlMenu Then
MsgBox "I didn't put a close button on this form for you to click
that !!"
Cancel = True
End If

End Sub

Mike
 
J

Jim Burton

Not sure what the relative merits of these two solutions are but I'll use
Mike's 'cos its shorter

Thanks to you both for the replies
 
R

RB Smissaert

Well the merits are obvious for both methods.
The API method saves the user clicking the X and
reacting to a message and the other method is a bit
simpler and shorter.
I always go for the API method as I think it is more logical.

RBS
 
D

Dave Peterson

Just to be disagreeable <bg>.

I've never seen the need to disable that X button.

It always made more sense to me to allow the userform to close. And if I have a
Cancel routine that runs if the user hits a cancel button, then just execute
that routine.

Option Explicit
Private Sub CommandButton2_Click()
MsgBox "closing"
Unload Me
End Sub
Private Sub UserForm_QueryClose(Cancel As Integer, CloseMode As Integer)
If CloseMode = vbFormControlMenu Then
Call CommandButton2_Click
End If
End Sub

It seems like that would be more consistent with the way most dialogs work in
most windows applications.
 
R

RB Smissaert

I suppose you are right there in that there always will be a way to allow
the
close with the X and still get the desired result, but if for some reason
(wrong reason maybe yes) you decide you want to disable the X close then
it makes sense to disable it with the API.

RBS
 
P

Peter T

In the spirit of being disagreeable <vbg>

If MsgBox("Is the little x disabled", vbYesNo) = vbNo Then
MsgBox "Oh yes it is" & vbCr & "and so's this one "
End If

FWIW, I never disable the little x for the very reason you say !

Regards,
Peter T
 
R

RB Smissaert

OK, I can see that probably there is never a good enough reason to disable
the X, but if
the solution would be to show a msgbox on pressing the X and not close the
form then
it makes sense to disable the X with the API.
Now if this doesn't settle it then ...

RBS
 
J

Jon Peltier

Instead of pissing off your users, replace the message box with a line that
executes the close button code. That's what they wanted to do anyway, right?

- Jon
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top