Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 159
Default Removing a control

How do I remove the close control from the upper right corner of my user form?
  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 638
Default Removing a control

On Oct 8, 6:45 pm, Mekinnik
wrote:
How do I remove the close control from the upper right corner of my user form?


While it is possible to remove the control completely via API calls
and such, it is much easier to just capture the event when the user
clicks on the close button and handle it.
Private Sub UserForm_QueryClose _
(Cancel As Integer, CloseMode As Integer)
If CloseMode = vbFormControlMenu Then
MsgBox "Hey, don't do that!!!!"
Cancel = True
End If
End Sub

  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 128
Default Removing a control

On Oct 8, 4:50 pm, JW wrote:
On Oct 8, 6:45 pm, Mekinnik
wrote:

How do I remove the close control from the upper right corner of my user form?


While it is possible to remove the control completely via API calls
and such, it is much easier to just capture the event when the user
clicks on the close button and handle it.
Private Sub UserForm_QueryClose _
(Cancel As Integer, CloseMode As Integer)
If CloseMode = vbFormControlMenu Then
MsgBox "Hey, don't do that!!!!"
Cancel = True
End If
End Sub


Hello Mekinnik,

Here is the API code if your are interested. Add this to Standard VBA
module. Just add the statement "DisableX" to your UserForm's Activate
event procedure.

'Start of Macro
'================================================= =======================
'Written: October 08, 2007
'Author: Leith Ross
'Summary: Disables the Close Box on the UserForm. Add the statement
' DisableX to the UserForm_Activate ent procedure.

' Define API Constants
Public Const MF_BYPOSITION = &H400&
Public Const MF_DISABLED = &H2&

' Define the API calls needed
Private Declare Function GetSystemMenu _
Lib "user32.dll" _
(ByVal hWnd As Long, _
ByVal bRevert As Long) As Long

Private Declare Function GetMenuItemCount _
Lib "user32.dll" _
(ByVal hMenu As Long) As Long

Private Declare Function RemoveMenu _
Lib "user32.dll" _
(ByVal hMenu As Long, _
ByVal nPosition As Long, _
ByVal wFlags As Long) As Long

Private Declare Function DrawMenuBar _
Lib "user32.dll" _
(ByVal hWnd As Long) As Long

' Returns the Window Handle of the Window accepting input
Private Declare Function GetForegroundWindow _
Lib "user32.dll" () As Long

Public Sub DisableX()

Dim hWnd As Long
Dim hMenu As Long, nCount As Long
Dim Ret As Long

' Get the Window Handle of the active UserForm
hWnd = GetForegroundWindow()

' Get handle to system menu
hMenu = GetSystemMenu(hWnd, 0)

' Get number of items in menu
nCount = GetMenuItemCount(hMenu)

' Remove last item from system menu (last item is 'Close')
Ret = RemoveMenu(hMenu, nCount - 1, MF_DISABLED Or
MF_BYPOSITION)

' Redraw menu
Ret = DrawMenuBar(hWnd)

End Sub
'================================================= =======================
'End of Macro

Sincerely,
Leith Ross

  #4   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 128
Default Removing a control

On Oct 8, 4:50 pm, JW wrote:
On Oct 8, 6:45 pm, Mekinnik
wrote:

How do I remove the close control from the upper right corner of my user form?


While it is possible to remove the control completely via API calls
and such, it is much easier to just capture the event when the user
clicks on the close button and handle it.
Private Sub UserForm_QueryClose _
(Cancel As Integer, CloseMode As Integer)
If CloseMode = vbFormControlMenu Then
MsgBox "Hey, don't do that!!!!"
Cancel = True
End If
End Sub


Hello Mekinnik,

Here is the API version to disable the "Close Box". Just add the
statement "DisableX" to your UserForm's Activate event. Copy the macro
code add paste it into a Standard VBA module.

'Start of Macro Code
'Written: October 08, 2007
'Author: Leith Ross
'Summary: Disables the Close Box on the UserForm. Add the statement
' DisableX to the UserForm_Activate ent procedure.

' Define API Constants
Public Const MF_BYPOSITION = &H400&
Public Const MF_DISABLED = &H2&

' Define the API calls needed
Private Declare Function GetSystemMenu _
Lib "user32.dll" _
(ByVal hWnd As Long, _
ByVal bRevert As Long) As Long

Private Declare Function GetMenuItemCount _
Lib "user32.dll" _
(ByVal hMenu As Long) As Long

Private Declare Function RemoveMenu _
Lib "user32.dll" _
(ByVal hMenu As Long, _
ByVal nPosition As Long, _
ByVal wFlags As Long) As Long

Private Declare Function DrawMenuBar _
Lib "user32.dll" _
(ByVal hWnd As Long) As Long

' Returns the Window Handle of the Window accepting input
Private Declare Function GetForegroundWindow _
Lib "user32.dll" () As Long

Public Sub DisableX()

Dim hWnd As Long
Dim hMenu As Long, nCount As Long
Dim Ret As Long

' Get the Window Handle of the active UserForm
hWnd = GetForegroundWindow()

' Get handle to system menu
hMenu = GetSystemMenu(hWnd, 0)

' Get number of items in menu
nCount = GetMenuItemCount(hMenu)

' Remove last item from system menu (last item is 'Close')
Ret = RemoveMenu(hMenu, nCount - 1, MF_DISABLED Or
MF_BYPOSITION)

' Redraw menu
Ret = DrawMenuBar(hWnd)

End Sub
'End of Macro Code

Sincerely,
Leith Ross

Reply
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules

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


Similar Threads
Thread Thread Starter Forum Replies Last Post
Add-In Toolbar Control - Removing The Toolbar Question Dreiding Excel Programming 10 October 5th 07 06:39 PM
Removing hyperlink without removing the font settings /border sett San Excel Programming 2 September 27th 07 04:37 PM
Removing patterns without removing gridlines pennyb9 Excel Discussion (Misc queries) 1 July 11th 07 02:43 AM
Removing calendar control Rob Excel Discussion (Misc queries) 0 August 4th 06 01:57 AM
Calendar Control: Can't exit design mode because control can't be created Rone Excel Programming 0 May 24th 04 04:01 PM


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

Powered by vBulletin® Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
Copyright ©2004-2024 ExcelBanter.
The comments are property of their posters.
 

About Us

"It's about Microsoft Excel"