10 Replies - 1678 Views - Last Post: 17 August 2011 - 11:43 AM Rate Topic: ***** 1 Votes

#1 NY Andrew  Icon User is offline

  • D.I.C Head

Reputation: 3
  • View blog
  • Posts: 79
  • Joined: 25-May 11

How can I turn CODE properties and Parrams, on and off..

Posted 16 August 2011 - 11:24 AM

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):
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.

Is This A Good Question/Topic? 0
  • +

Replies To: How can I turn CODE properties and Parrams, on and off..

#2 _HAWK_  Icon User is online

  • Master(Of Foo)
  • member icon

Reputation: 1046
  • View blog
  • Posts: 4,072
  • Joined: 02-July 08

Re: How can I turn CODE properties and Parrams, on and off..

Posted 16 August 2011 - 01:17 PM

Why not handle the closing of the form in the form closing event with the e. Cancel property?
Was This Post Helpful? 0
  • +
  • -

#3 NY Andrew  Icon User is offline

  • D.I.C Head

Reputation: 3
  • View blog
  • Posts: 79
  • Joined: 25-May 11

Re: How can I turn CODE properties and Parrams, on and off..

Posted 16 August 2011 - 01:23 PM

I am not exactly sure how I can merge that with this code..
Was This Post Helpful? 0
  • +
  • -

#4 _HAWK_  Icon User is online

  • Master(Of Foo)
  • member icon

Reputation: 1046
  • View blog
  • Posts: 4,072
  • Joined: 02-July 08

Re: How can I turn CODE properties and Parrams, on and off..

Posted 16 August 2011 - 02:36 PM

It's not about merging, it about a diff angle. You can add the form_closing event to your form. In your button event where you want to change whether they can close the form you toggle a boolean to False - meaning when you declare the variable you set it to True and in the closing event you have:
Private closeForm As Boolean = True

Private Sub Form_Closing(ByVal sender As Object, ByVal e As System.Windows.Forms.FormClosingEventArgs) Handles Me.FormClosing
  e.Cancel = closeForm
End Sub

Private Sub button_click() Handles button.Click
  'other code
  closeForm = False
End Sub

This post has been edited by hawkvalley1: 16 August 2011 - 02:43 PM

Was This Post Helpful? 1
  • +
  • -

#5 NY Andrew  Icon User is offline

  • D.I.C Head

Reputation: 3
  • View blog
  • Posts: 79
  • Joined: 25-May 11

Re: How can I turn CODE properties and Parrams, on and off..

Posted 16 August 2011 - 02:58 PM

Well I understand that, but I am unsure of how to change the:
    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
to Boolean..
Let me clarify, I like how youur example works, but I am trying to implement my code above to dim out that close button.

This post has been edited by NY Andrew: 16 August 2011 - 03:00 PM

Was This Post Helpful? 0
  • +
  • -

#6 trevster344  Icon User is offline

  • The Peasant
  • member icon

Reputation: 224
  • View blog
  • Posts: 1,505
  • Joined: 16-March 11

Re: How can I turn CODE properties and Parrams, on and off..

Posted 16 August 2011 - 07:56 PM

Click the events drop down in your forms code. Select your *Form name* Events, then proceed to the second drop down and click form closing. It is going to create a method for you, with e set to the form closing arguments, and handle that whole deal for you instead of your code. This means if you take this route you can scrap your code, cause it's a different angle as hawk said. A new method.

This post has been edited by trevster344: 16 August 2011 - 08:04 PM

Was This Post Helpful? 0
  • +
  • -

#7 NY Andrew  Icon User is offline

  • D.I.C Head

Reputation: 3
  • View blog
  • Posts: 79
  • Joined: 25-May 11

Re: How can I turn CODE properties and Parrams, on and off..

Posted 16 August 2011 - 08:04 PM

Thank you all for your responses. I have successfully managed to get my program to work the way I had wanted.
Was This Post Helpful? 0
  • +
  • -

#8 _HAWK_  Icon User is online

  • Master(Of Foo)
  • member icon

Reputation: 1046
  • View blog
  • Posts: 4,072
  • Joined: 02-July 08

Re: How can I turn CODE properties and Parrams, on and off..

Posted 17 August 2011 - 03:23 AM

Sharing the answer from Code Project:
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

Was This Post Helpful? 1
  • +
  • -

#9 NY Andrew  Icon User is offline

  • D.I.C Head

Reputation: 3
  • View blog
  • Posts: 79
  • Joined: 25-May 11

Re: How can I turn CODE properties and Parrams, on and off..

Posted 17 August 2011 - 11:14 AM

I see you managed to find that from my question there also. :rolleyes2:
Was This Post Helpful? 0
  • +
  • -

#10 _HAWK_  Icon User is online

  • Master(Of Foo)
  • member icon

Reputation: 1046
  • View blog
  • Posts: 4,072
  • Joined: 02-July 08

Re: How can I turn CODE properties and Parrams, on and off..

Posted 17 August 2011 - 11:38 AM

I like to complete threads so they are not dead ends.
Was This Post Helpful? 0
  • +
  • -

#11 NY Andrew  Icon User is offline

  • D.I.C Head

Reputation: 3
  • View blog
  • Posts: 79
  • Joined: 25-May 11

Re: How can I turn CODE properties and Parrams, on and off..

Posted 17 August 2011 - 11:43 AM

Yea sorry about that. I normally do share the way I fixed a problem but it was just one of those days when nothing works out and you don't feel like talking to anyone so I didn't bother sharing. My apologize Hawk.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1