Results 1 to 7 of 7

Thread: Controlling the Close (x) button on a form in VB5

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Jan 2000
    Location
    UK
    Posts
    66

    Post

    I have an app with a timer that won't allow the user to close it until the timer has finished. (when the countdown is over, a command button becomes enabled to close the form).

    But how can I disable the close (x) in the top right hand corner whilst keeping the rest of the toolbar?

    I've tried the code in KB article Q110393 but it doesn't work.

    Any ideas?

    Simon

  2. #2
    Frenzied Member Buzby's Avatar
    Join Date
    Jan 1999
    Location
    UK
    Posts
    1,670

    Post

    Why not change the ControlBox property of the form to FALSE?



    ------------------
    Mark "Buzby" Beeton
    VB Developer
    [email protected]



  3. #3

    Thread Starter
    Lively Member
    Join Date
    Jan 2000
    Location
    UK
    Posts
    66

    Post

    That takes away the icon in the top-left corner and I'd like to keep that

    Simon

  4. #4
    Member
    Join Date
    Apr 1999
    Posts
    38

    Post

    Write code in the Unload-event of the form

    there you can check if your timer has finished.

    If not, set the Cancel-variable to true (or is it false ? -> if it doesn't work with true, it must be false )

  5. #5
    Former Admin/Moderator MartinLiss's Avatar
    Join Date
    Sep 1999
    Location
    San Jose, CA
    Posts
    33,431

    Post

    Code that queries the state of the ControlBox is much better done in the QueryUnload event.

    Code:
    Private Sub Form_QueryUnload(Cancel As Integer, UnloadMode As Integer)
    
        If UnloadMode = vbFormControlMenu Then
            ' g_bProcessFinished is a (perhaps) global variable
            ' that you would set to true when your long-running
            ' process is complete.
            If Not g_bProcessFinished Then
                Beep
                MsgBox "Please wait. Process is not finished"
                Cancel = True
            End If
        End If
        
    End Sub
    ------------------
    Marty
    What did the fish say when it hit the concrete wall?
    > > > > > "Dam!"



    [This message has been edited by MartinLiss (edited 02-10-2000).]

  6. #6
    Hyperactive Member
    Join Date
    Feb 2000
    Location
    the UK
    Posts
    265

    Post

    Thanks Azzmodan! That did the job - how would I go about enabling the (x) again after 5 seconds?

    Would this be possible?

    Simon

  7. #7
    Guest

    Post

    This will remove the Close command and the Separator bar from the system menu, redraws the menubar(wich will figure, hee wait a sec, the close is gone, lets gray out that freakn' X thingie)

    Add this to a module:
    Code:
    Option Explicit
    Private Const MF_BYPOSITION = &H400
    Private Const MF_REMOVE = &H1000
    Private Declare Function DrawMenuBar Lib "user32" (ByVal hWnd As Long) As Long
    Private Declare Function GetMenuItemCount Lib "user32" (ByVal hMenu As Long) As Long
    Private Declare Function GetSystemMenu Lib "user32" (ByVal hWnd As Long, ByVal bRevert As Long) As Long
    Private Declare Function RemoveMenu Lib "user32" (ByVal hMenu As Long, ByVal nPosition As Long, ByVal wFlags As Long) As Long
    
    Public Sub X_Disable(ByVal hWnd As Long)
        Dim lhMenu As Long
        Dim lMenuItemCount As Long
        
        lhMenu = GetSystemMenu(hWnd, 0)
        If lhMenu Then
             lMenuItemCount = GetMenuItemCount(lhMenu)
             Call RemoveMenu(lhMenu, lMenuItemCount - 1, MF_REMOVE Or MF_BYPOSITION)
             Call RemoveMenu(lhMenu, lMenuItemCount - 2, MF_REMOVE Or MF_BYPOSITION)
             Call DrawMenuBar(hWnd)
        End If
    End Sub
    Call it by placing the following code somewhere

    Call X_Disable(MyForm.hWnd)

    Hope it helps.



    ------------------

    Vincent van den Braken
    EMail: [email protected]
    ICQ: 15440110
    Homepage: http://www.azzmodan.demon.nl




Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width