How to disable close button in userform?

Y

yajiv.vijay

I have a form inside my vba project. I want to disable the close
button. How to do it?
 
N

Nigel

You have 2 choices that I know of ...

1. Simple: intercept the UserForm close event.

add the following to your UserForm

Private Sub UserForm_QueryClose(Cancel As Integer, CloseMode As Integer)
If CloseMode = 0 Then
MsgBox "This option will not close the form"
Cancel = 1
End If
End Sub


2. Include a API to remove the UserForm caption.
More complex but add this code.......

At top of UserForm module add......

'heading for caption remover
Private Declare Function FindWindow Lib "user32" Alias _
"FindWindowA" (ByVal lpClassName As String, _
ByVal lpWindowName As String) As Long
Private Declare Function GetWindowLong Lib "user32" Alias _
"GetWindowLongA" (ByVal hWnd As Long, _
ByVal nIndex As Long) As Long
Private Declare Function SetWindowLong Lib "user32" Alias _
"SetWindowLongA" (ByVal hWnd As Long, ByVal nIndex As Long, _
ByVal dwNewLong As Long) As Long
Private Declare Function DrawMenuBar Lib "user32" ( _
ByVal hWnd As Long) As Long

-------------------------------------
Add the following function to the userform code.....

Private Function HideCaptionBar()
Dim lngHnd As Long
Dim lngStyle As Long
Dim lngH(1) As Long
Const GWL_STYLE = (-16)
Const WS_CAPTION = &HC00000
lngH(0) = Me.Height - Me.InsideHeight
If Val(Application.Version) > 8 Then
lngHnd = FindWindow("ThunderDFrame", Me.Caption)
Else
lngHnd = FindWindow("ThunderXFrame", Me.Caption)
End If
lngStyle = GetWindowLong(lngHnd, GWL_STYLE) And Not WS_CAPTION
SetWindowLong lngHnd, GWL_STYLE, lngStyle
DrawMenuBar lngHnd
lngH(1) = Me.Height - Me.InsideHeight
Me.Height = Me.Height + lngH(1) - lngH(0)
End Function
 
Y

yajiv.vijay

You have 2 choices that I know of ...

1. Simple: intercept the UserForm close event.

add the following to your UserForm

Private Sub UserForm_QueryClose(Cancel As Integer, CloseMode As Integer)
   If CloseMode = 0 Then
     MsgBox "This option will not close the form"
     Cancel = 1
   End If
End Sub

2. Include a API to remove the UserForm caption.
More complex but add this code.......

At top of UserForm module add......

'heading for caption remover
Private Declare Function FindWindow Lib "user32" Alias _
  "FindWindowA" (ByVal lpClassName As String, _
  ByVal lpWindowName As String) As Long
Private Declare Function GetWindowLong Lib "user32" Alias _
  "GetWindowLongA" (ByVal hWnd As Long, _
  ByVal nIndex As Long) As Long
Private Declare Function SetWindowLong Lib "user32" Alias _
  "SetWindowLongA" (ByVal hWnd As Long, ByVal nIndex As Long, _
  ByVal dwNewLong As Long) As Long
Private Declare Function DrawMenuBar Lib "user32" ( _
  ByVal hWnd As Long) As Long

-------------------------------------
Add the following function to the userform code.....

Private Function HideCaptionBar()
  Dim lngHnd    As Long
  Dim lngStyle  As Long
  Dim lngH(1)   As Long
  Const GWL_STYLE = (-16)
  Const WS_CAPTION = &HC00000
  lngH(0) = Me.Height - Me.InsideHeight
  If Val(Application.Version) > 8 Then
    lngHnd = FindWindow("ThunderDFrame", Me.Caption)
  Else
    lngHnd = FindWindow("ThunderXFrame", Me.Caption)
  End If
  lngStyle = GetWindowLong(lngHnd, GWL_STYLE) And Not WS_CAPTION
  SetWindowLong lngHnd, GWL_STYLE, lngStyle
  DrawMenuBar lngHnd
  lngH(1) = Me.Height - Me.InsideHeight
  Me.Height = Me.Height + lngH(1) - lngH(0)
End Function

-------------------------------------------------------
In the userform initialize code add......

Call HideCaptionBar

--

Regards,
Nigel
(e-mail address removed)






- Show quoted text -

Wow! Done!
This is the fastest and working reply i've ever got.
Thanks a lot!
 

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