Solved

How to disable the Close Button at Runtime.......??

Posted on 2004-09-26
8
225 Views
Last Modified: 2010-05-02
Hi all,
         I want to disable the Close button of my form during some function call in my code......
Something like this.....

call DisableCloseButton
call MyFunction
call EnableCloseButton

I have tried to use the enable property but it freezes the whole form which is not desireable.........

Imran Arshad
0
Comment
Question by:imarshad
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 3
  • 2
8 Comments
 
LVL 55

Expert Comment

by:Jaime Olivares
ID: 12155639
Try with:

yourform.Controlbox=true
0
 
LVL 55

Expert Comment

by:Jaime Olivares
ID: 12155649
Last example is to enable. To disable

yourform.Controlbox=false
0
 
LVL 32

Expert Comment

by:Erick37
ID: 12155661
Hi,

Here is a code sample from MS which allows you to disable the close button during runtime:

How To Disable the Close Option on Control Menu of a VB Form
http://support.microsoft.com/support/kb/articles/Q184/6/86.asp

Hope it helps!
0
MS Dynamics Made Instantly Simpler

Make Your Microsoft Dynamics Investment Count  & Drastically Decrease Training Time by Providing Intuitive Step-By-Step WalkThru Tutorials.

 
LVL 13

Author Comment

by:imarshad
ID: 12155663
Compilation Error.....
Function or interface marked as restricted, or the function uses an automation type not supported in VB....
0
 
LVL 32

Expert Comment

by:Erick37
ID: 12155716
You cannot set the ControlBox property of a form in code.  This is only available in design time.
0
 
LVL 13

Author Comment

by:imarshad
ID: 12155751
Erick37,
            What you have supplied is exactly what I want........Isn't there a simple way of doing it ? If none then I will give the points to you...........I am trying to write the DisableCloseButton and EnableCloseButton from the code you have suggested.....
I will ideally like to have these functions in a Module.......Can you help me in this regard ? My main form is Form1......

Imran Arshad
0
 
LVL 32

Accepted Solution

by:
Erick37 earned 125 total points
ID: 12155776
Here is a better example from CodeGuru:

http://www.codeguru.com/vb/gen/vb_forms/article.php/c3031/

The following code should be blaced in a standard code Module (Project->Add Module)
From your form code, you call it like this:

'Disable
EnableCloseButton Me.hWnd, False

'Enable
EnableCloseButton Me.hWnd, True

'The Module code
Option Explicit

Private Const SC_CLOSE As Long = &HF060&
Private Const MIIM_STATE As Long = &H1&
Private Const MIIM_ID As Long = &H2&
Private Const MFS_GRAYED As Long = &H3&
Private Const WM_NCACTIVATE As Long = &H86

Private Type MENUITEMINFO
    cbSize As Long
    fMask As Long
    fType As Long
    fState As Long
    wID As Long
    hSubMenu As Long
    hbmpChecked As Long
    hbmpUnchecked As Long
    dwItemData As Long
    dwTypeData As String
    cch As Long
End Type

Private Declare Function GetSystemMenu Lib "user32" ( _
    ByVal hWnd As Long, ByVal bRevert As Long) As Long

Private Declare Function GetMenuItemInfo Lib "user32" Alias _
    "GetMenuItemInfoA" (ByVal hMenu As Long, ByVal un As Long, _
    ByVal b As Boolean, lpMenuItemInfo As MENUITEMINFO) As Long

Private Declare Function SetMenuItemInfo Lib "user32" Alias _
    "SetMenuItemInfoA" (ByVal hMenu As Long, ByVal un As Long, _
    ByVal bool As Boolean, lpcMenuItemInfo As MENUITEMINFO) As Long

Private Declare Function SendMessage Lib "user32" Alias _
    "SendMessageA" (ByVal hWnd As Long, ByVal wMsg As Long, _
    ByVal wParam As Long, lParam As Any) As Long

Private Declare Function IsWindow Lib "user32" _
    (ByVal hWnd As Long) As Long

'*******************************************************************************
' Enables / Disables the close button on the titlebar and in the system menu
' of the form window passed.
'-------------------------------------------------------------------------------
' Return Values:
'
'    0  Close button state changed succesfully / nothing to do.
'   -1  Invalid Window Handle (hWnd argument) Passed to the function
'   -2  Failed to switch command ID of Close menu item in system menu
'   -3  Failed to switch enabled state of Close menu item in system menu
'
'-------------------------------------------------------------------------------
' Parameters:
'
'   hWnd    The window handle of the form whose close button is to be enabled/
'           disabled / greyed out.
'
'   Enable  True if the close button is to be enabled, or False if it is to
'           be disabled / greyed out.
'
'-------------------------------------------------------------------------------
' Example:
'
' Add a form window to your project, and place a button on the form. Add the
' following in the form's code window:
'
'    Option Explicit
'
'    Private m_blnCloseEnabled As Boolean
'
'    Private Sub Form_Load()
'        m_blnCloseEnabled = True
'        Command1.Caption = "Disable"
'    End Sub
'
'    Private Sub Command1_Click()
'        m_blnCloseEnabled = Not m_blnCloseEnabled
'        EnableCloseButton Me.hwnd, m_blnCloseEnabled
'
'        If m_blnCloseEnabled Then
'            Command1.Caption = "Disable"
'        Else
'            Command1.Caption = "Enable"
'        End If
'    End Sub
'
'-------------------------------------------------------------------------------

Public Function EnableCloseButton(ByVal hWnd As Long, Enable As Boolean) _
                                                                As Integer
    Const xSC_CLOSE As Long = -10

    ' Check that the window handle passed is valid
   
    EnableCloseButton = -1
    If IsWindow(hWnd) = 0 Then Exit Function
   
    ' Retrieve a handle to the window's system menu
   
    Dim hMenu As Long
    hMenu = GetSystemMenu(hWnd, 0)
   
    ' Retrieve the menu item information for the close menu item/button
   
    Dim MII As MENUITEMINFO
    MII.cbSize = Len(MII)
    MII.dwTypeData = String(80, 0)
    MII.cch = Len(MII.dwTypeData)
    MII.fMask = MIIM_STATE
   
    If Enable Then
        MII.wID = xSC_CLOSE
    Else
        MII.wID = SC_CLOSE
    End If
   
    EnableCloseButton = -0
    If GetMenuItemInfo(hMenu, MII.wID, False, MII) = 0 Then Exit Function
   
    ' Switch the ID of the menu item so that VB can not undo the action itself
   
    Dim lngMenuID As Long
    lngMenuID = MII.wID
   
    If Enable Then
        MII.wID = SC_CLOSE
    Else
        MII.wID = xSC_CLOSE
    End If
   
    MII.fMask = MIIM_ID
    EnableCloseButton = -2
    If SetMenuItemInfo(hMenu, lngMenuID, False, MII) = 0 Then Exit Function
   
    ' Set the enabled / disabled state of the menu item
   
    If Enable Then
        MII.fState = (MII.fState Or MFS_GRAYED)
        MII.fState = MII.fState - MFS_GRAYED
    Else
        MII.fState = (MII.fState Or MFS_GRAYED)
    End If
   
    MII.fMask = MIIM_STATE
    EnableCloseButton = -3
    If SetMenuItemInfo(hMenu, MII.wID, False, MII) = 0 Then Exit Function
   
    ' Activate the non-client area of the window to update the titlebar, and
    ' draw the close button in its new state.
   
    SendMessage hWnd, WM_NCACTIVATE, True, 0
   
    EnableCloseButton = 0
   
End Function

'*******************************************************************************
'
'-------------------------------------------------------------------------------
0
 
LVL 13

Author Comment

by:imarshad
ID: 12155898
Thanks for the help......

Imran Arshad
0

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Enums (shorthand for ‘enumerations’) are not often used by programmers but they can be quite valuable when they are.  What are they? An Enum is just a type of variable like a string or an Integer, but in this case one that you create that contains…
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…
Suggested Courses
Course of the Month7 days, 1 hour left to enroll

623 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question