1

I'm making a custom form for my library

Public Class MycustomForm : Inherits Form

    Private Const CP_NOCLOSE_BUTTON As Integer = &H200
    Private _CloseBox As Boolean = True

    Public Sub New()
        MyBase.New()
    End Sub
    <Category("WindowStyle")> _
    <DefaultValue(True)> _
    <Description("This property enables or disables the close button")> _
    Public Property CloseBox As Boolean
        Get
            Return _CloseBox
        End Get
        Set(value As Boolean)
            If value <> _CloseBox Then
                value = _CloseBox
                Me.RecreateHandle()
            End If
        End Set
    End Property

    Protected Overrides ReadOnly Property CreateParams As CreateParams
        Get
            Dim CustomParams As CreateParams = MyBase.CreateParams
            If Not _CloseBox Then
                CustomParams.ClassStyle = CustomParams.ClassStyle Or CP_NOCLOSE_BUTTON
            End If
            Return CustomParams
        End Get
    End Property

End Class

I created a property to offer developers the possibility of disable the close button

When I test my form in the designer, I changed the MyForm.Designer:

Partial Class MyForm
    Inherits MycustomForm

After, the property has been added, when I try to change the property to False, I'm unable to change it, because the property not changes

What am I doing wrong?

2 Answers2

1

Note how your property is only used in the CreateParams property getter. This getter is used only at a very specific time, when the window is created. So if you want your property to have an affect then it is necessary to recreate the window.

That sounds very hard to do, but isn't. Winforms often has a need to recreate the window on the fly, there are several existing properties that have the same wrinkle. Like ShowInTaskbar, Opacity, FormBorderStyle, etcetera. Make your setter look like this:

    Set(value As Boolean)
        If value <> _CloseBox Then
            _CloseBox = value
            If Me.IsHandleCreated Then RecreateHandle()
        End If
    End Set

The RecreateHandle() method gets the job done. It flickers a bit btw, inevitably of course.

Hans Passant
  • 922,412
  • 146
  • 1,693
  • 2,536
0

You have made a simple mistake.

Change value = _CloseBox to _CloseBox = value

Yacoub Massad
  • 27,509
  • 2
  • 36
  • 62