Visual Studio,窗体,标签禁用但保留ForeColor(Visual Basic)

时间:2017-04-05 11:14:54

标签: vb.net winforms visual-studio-2015 label

我在Visual Studio中有一个表单,需要禁用表单中的一个标签,(在行为类别下,Enabled设置为False)。

当Label Behavior设置为" Enabled = False"时,当我运行应用程序时,标签的ForeColor会变为灰色。如果我想保留原始颜色,我该怎么做呢? 我想使用的ForeColor是白色。

1 个答案:

答案 0 :(得分:1)

解决方案是继承Label类,然后隐藏基础Enabled属性:

Public Class MyLabel : Inherits Label

    ' https://referencesource.microsoft.com/#System.Windows.Forms/winforms/Managed/System/WinForms/Control.cs,eb7c3ce1cd1cb1d3
    Public Overridable Shadows Property Enabled As Boolean
        Get
            If (MyBase.Parent IsNot Nothing) Then
                Return MyBase.Parent.Enabled
            Else
                Return Me.enabledB
            End If
        End Get
        Set(value As Boolean)
            If (value <> Me.enabledB) Then
                Me.enabledB = value
                MyBase.OnEnabledChanged(EventArgs.Empty)
            End If
        End Set
    End Property
    Private enabledB As Boolean ' Backing field

    Public Sub New()
        Me.enabledB = True
    End Sub

End Class