Click here to Skip to main content
15,890,512 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
See more:
Sorry about the title, I am not exactly sure what I am looking for here.. So I have this code to disable the close button on a form (Red X):

VB
Protected Overrides ReadOnly Property CreateParams() As CreateParams
    Get
        Dim cp As CreateParams = MyBase.CreateParams
        Const CS_NOCLOSE As Integer = &H200
        cp.ClassStyle = cp.ClassStyle Or CS_NOCLOSE
        Return cp
    End Get
End Property


Now that disables the close button as soon as the form shows, but how can I have it not disable the close button until a user presses a button for example?
Thanks.
Posted
Updated 16-Aug-11 8:28am
v2

1 solution

The class style can be modified after window creation via the Windows API functions Get/SetClassLong (or Get/SetClassLongPtr for 64 bit compatibility).

VB
Private Const GCL_STYLE As Integer = -26
Private Const CS_NOCLOSE As Integer = &H200
<DllImport("user32.dll", EntryPoint := "GetClassLong")> _
Private Shared Function GetClassLong(hWnd As IntPtr, nIndex As Integer) As UInteger
<DllImport("user32.dll", EntryPoint := "SetClassLong")> _
Private Shared Function SetClassLong(hWnd As IntPtr, nIndex As Integer, dwNewLong As UInteger) As UInteger

Private Sub ToggleStyleBtn_Click(sender As Object, e As EventArgs)
    Dim currentStyle As UInteger = GetClassLong(Me.Handle, GCL_STYLE)
    REM flip the CS_NOCLOSE bit
    SetClassLong(Me.Handle, GCL_STYLE, currentStyle Xor CS_NOCLOSE)
    REM force a redraw with the new style
    UpdateStyles()
End Sub


Alan.
 
Share this answer
 
v2
Comments
NY Andrew 16-Aug-11 19:56pm    
WOW! THANK YOU SO MUCH ALAN!!! This worked! I never in a million years could have put this together! It worked perfectly after tweaking it a little bit, but it was no biggy at all. Once again thanks! :)
Sergey Alexandrovich Kryukov 16-Aug-11 23:27pm    
Exactly. My 5.
--SA

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month


CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900