Results 1 to 7 of 7

Thread: Close button of form

  1. #1

    Thread Starter
    Fanatic Member
    Join Date
    Nov 2003
    Posts
    784

    Question Close button of form

    I need to disable close ("X") button of my form . .how to do this? in VB6 I knew there is an API function for doing this .. how about in VB.NET? please advise

    many thanks in advance

    Regards
    Winan

  2. #2

  3. #3
    Frenzied Member Memnoch1207's Avatar
    Join Date
    Feb 2002
    Location
    DUH, Guess...Hint: It's really hot!
    Posts
    1,861
    Setting the ControlBox property of the form to false will remove all 3 buttons (Maximize, Minimize and Close)

    This code will just disable the close button
    VB Code:
    1. Public Declare Function GetSystemMenu Lib "user32" (ByVal hwnd As Integer, ByVal bRevert As Integer) As Integer
    2. Public Declare Function GetMenuItemCount Lib "user32" (ByVal hMenu As Integer) As Integer
    3. Public Declare Function RemoveMenu Lib "user32" (ByVal hMenu As Integer, ByVal nPosition As Integer, ByVal wFlags As Integer) As Integer
    4. Public Declare Function DrawMenuBar Lib "user32" (ByVal hwnd As Integer) As Integer
    5. Public Const MF_BYPOSITION = &H400&
    6. Public Const MF_DISABLED = &H2&
    7.  
    8. Dim hMenu As Integer, nCount As Integer
    9.  
    10. 'Get handle to system menu
    11. hMenu = GetSystemMenu(Me.Handle.ToInt32, 0)
    12.  
    13. 'Get number of items in menu
    14. nCount = GetMenuItemCount(hMenu)
    15.  
    16. 'Remove last item from system menu (last item is ’Close’)
    17. Call RemoveMenu(hMenu, nCount - 1, MF_DISABLED Or MF_BYPOSITION)
    18.  
    19. 'Redraw menu
    20. DrawMenuBar(Me.Handle.ToInt32)
    Last edited by Memnoch1207; Dec 6th, 2003 at 12:40 AM.
    Being educated does not make you intelligent.

    Need a weekend getaway??? Come Visit

  4. #4
    Sleep mode
    Join Date
    Aug 2002
    Location
    RUH
    Posts
    8,083
    Originally posted by Memnoch1207
    Set the ControlBox property of the form to false.
    This will hide it !

  5. #5
    Addicted Member
    Join Date
    Aug 2002
    Posts
    224

    to disable close ("X") button of my form

    Hi,

    Private Sub Form1_Closing(ByVal sender As Object, ByVal e As System.ComponentModel.CancelEventArgs) Handles MyBase.Closing

    e.Cancel = True

    End Sub


    Have a nice day

  6. #6
    Sleep mode
    Join Date
    Aug 2002
    Location
    RUH
    Posts
    8,083

    Re: to disable close ("X") button of my form

    Originally posted by yulyos
    Hi,

    Private Sub Form1_Closing(ByVal sender As Object, ByVal e As System.ComponentModel.CancelEventArgs) Handles MyBase.Closing

    e.Cancel = True

    End Sub


    Have a nice day
    I know this will work , but some people prefer seeing the "X" button grayed out rather than handling the event after triggered .

  7. #7
    Addicted Member
    Join Date
    Aug 2002
    Posts
    224

    to disable close ("X") button of my form

    Hi,

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

    MyBase.ControlBox = False

    End Sub

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