1

我正在为我的图书馆制作一个自定义表单

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

我创建了一个属性来为开发人员提供禁用关闭按钮的可能性

当我在设计器中测试我的表单时,我更改了MyForm.Designer

Partial Class MyForm
    Inherits MycustomForm

之后,属性已添加,当我尝试将属性更改为 时False,我无法更改它,因为属性没有更改

我究竟做错了什么?

4

2 回答 2

1

请注意您的属性如何仅在 CreateParams 属性获取器中使用。此 getter 仅在创建窗口时的特定时间使用。因此,如果您希望您的财产产生影响,则有必要重新创建窗口。

这听起来很难做到,但事实并非如此。Winforms 经常需要在运行中重新创建窗口,有几个现有的属性具有相同的皱纹。像 ShowInTaskbar、Opacity、FormBorderStyle 等等。让你的二传手看起来像这样:

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

RecreateHandle() 方法完成了工作。顺便说一句,它不可避免地闪烁了一下。

于 2015-11-15T19:45:27.127 回答
0

你犯了一个简单的错误。

更改value = _CloseBox_CloseBox = value

于 2015-11-15T19:40:25.893 回答