ExcelBanter

ExcelBanter (https://www.excelbanter.com/)
-   Excel Programming (https://www.excelbanter.com/excel-programming/)
-   -   UserForm - Disable the 'x' button (https://www.excelbanter.com/excel-programming/392437-userform-disable-x-button.html)

Jim Burton

UserForm - Disable the 'x' button
 
How can I disable the little 'x' button in the top right corner ?

TIA



RB Smissaert

UserForm - Disable the 'x' button
 
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


"Jim Burton" wrote in message
...
How can I disable the little 'x' button in the top right corner ?

TIA



Mike H

UserForm - Disable the 'x' button
 
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
"Jim Burton" wrote:

How can I disable the little 'x' button in the top right corner ?

TIA




Jim Burton

UserForm - Disable the 'x' button
 
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

"Jim Burton" wrote in message
...
How can I disable the little 'x' button in the top right corner ?

TIA




RB Smissaert

UserForm - Disable the 'x' button
 
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

"Jim Burton" wrote in message
...
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

"Jim Burton" wrote in message
...
How can I disable the little 'x' button in the top right corner ?

TIA





Dave Peterson

UserForm - Disable the 'x' button
 
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.





RB Smissaert wrote:

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

"Jim Burton" wrote in message
...
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

"Jim Burton" wrote in message
...
How can I disable the little 'x' button in the top right corner ?

TIA




--

Dave Peterson

RB Smissaert

UserForm - Disable the 'x' button
 
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

"Dave Peterson" wrote in message
...
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.





RB Smissaert wrote:

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

"Jim Burton" wrote in message
...
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

"Jim Burton" wrote in message
...
How can I disable the little 'x' button in the top right corner ?

TIA




--

Dave Peterson



Peter T

UserForm - Disable the 'x' button
 
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


"Dave Peterson" wrote in message
...
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.




RB Smissaert

UserForm - Disable the 'x' button
 
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

"Peter T" <peter_t@discussions wrote in message
...
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


"Dave Peterson" wrote in message
...
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.





Dave Peterson

UserForm - Disable the 'x' button
 
I agree that a msgbox would be irritating, too.

How about a beep <vbg?

RB Smissaert wrote:

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

"Peter T" <peter_t@discussions wrote in message
...
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


"Dave Peterson" wrote in message
...
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.




--

Dave Peterson

RB Smissaert

UserForm - Disable the 'x' button
 
Shut down the PC that will teach them ...

RBS

"Dave Peterson" wrote in message
...
I agree that a msgbox would be irritating, too.

How about a beep <vbg?

RB Smissaert wrote:

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

"Peter T" <peter_t@discussions wrote in message
...
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


"Dave Peterson" wrote in message
...
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.




--

Dave Peterson



Jon Peltier

UserForm - Disable the 'x' button
 
Instead of ****ing 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
-------
Jon Peltier, Microsoft Excel MVP
Tutorials and Custom Solutions
Peltier Technical Services, Inc. - http://PeltierTech.com
_______


"Mike H" wrote in message
...
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
"Jim Burton" wrote:

How can I disable the little 'x' button in the top right corner ?

TIA







All times are GMT +1. The time now is 05:27 PM.

Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
ExcelBanter.com