Solved

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

Posted on 2004-09-26
8
219 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
  • 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
 
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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
vba find the last empty column 10 88
VB error "Type mismatch" 2 49
to transfer string from C lanaguage to VBA 4 56
message box in access 4 41
There are many ways to remove duplicate entries in an SQL or Access database. Most make you temporarily insert an ID field, make a temp table and copy data back and forth, and/or are slow. Here is an easy way in VB6 using ADO to remove duplicate row…
If you have ever used Microsoft Word then you know that it has a good spell checker and it may have occurred to you that the ability to check spelling might be a nice piece of functionality to add to certain applications of yours. Well the code that…
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…

910 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

Need Help in Real-Time?

Connect with top rated Experts

24 Experts available now in Live!

Get 1:1 Help Now