Solved

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

Posted on 2004-09-26
8
216 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
IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 
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

What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

When designing a form there are several BorderStyles to choose from, all of which can be classified as either 'Fixed' or 'Sizable' and I'd guess that 'Fixed Single' or one of the other fixed types is the most popular choice. I assume it's the most p…
I was working on a PowerPoint add-in the other day and a client asked me "can you implement a feature which processes a chart when it's pasted into a slide from another deck?". It got me wondering how to hook into built-in ribbon events in Office.
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…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…

757 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

20 Experts available now in Live!

Get 1:1 Help Now