PC Review


Reply
Thread Tools Rate Thread

How to disable close button in userform?

 
 
yajiv.vijay@gmail.com
Guest
Posts: n/a
 
      20th Feb 2008
I have a form inside my vba project. I want to disable the close
button. How to do it?
 
Reply With Quote
 
 
 
 
Nigel
Guest
Posts: n/a
 
      20th Feb 2008
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 Removed)



<(E-Mail Removed)> wrote in message
news:aae3d8c8-f6da-48a1-a1f1-(E-Mail Removed)...
>I have a form inside my vba project. I want to disable the close
> button. How to do it?


 
Reply With Quote
 
yajiv.vijay@gmail.com
Guest
Posts: n/a
 
      20th Feb 2008
On Feb 20, 12:13*pm, "Nigel" <nigel-...@nosupanetspam.com> wrote:
> 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
> nigelnos...@9sw.co.uk
>
> <yajiv.vi...@gmail.com> wrote in message
>
> news:aae3d8c8-f6da-48a1-a1f1-(E-Mail Removed)...
>
>
>
> >I have a form inside my vba project. I want to disable the close
> > button. How to do it?- Hide quoted text -

>
> - Show quoted text -


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

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Disable UserForm 'X' Close Button dimm Microsoft Excel Programming 2 25th Mar 2008 03:26 AM
Disable the Close button on a Userform Moh Microsoft Excel Programming 7 14th Dec 2007 05:11 PM
Disable Close button and remove Close menu and then reactivate fpsoft Microsoft Dot NET Framework Forms 0 23rd Sep 2007 03:55 PM
excel vba - how to disable userform close button "x"? chief Microsoft Excel Misc 2 7th May 2004 02:05 PM
Disable close button of Modeless Userform RB Smissaert Microsoft Excel Programming 2 13th Sep 2003 09:46 PM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 08:06 PM.