将textbox的enabled属性设置为false并将forecolor设置为white

时间:2015-09-17 02:47:36

标签: vb.net

我正在使用Windows窗体。 textbox1.enabled = false的默认前景色为灰色,如何将其更改为白色?

2 个答案:

答案 0 :(得分:2)

来自专家交流的文章:

security groups

这是DisTextBox班级:

Public Class DisTextBox
    Inherits System.Windows.Forms.TextBox

    Private _ForeColorBackup As Color
    Private _BackColorBackup As Color
    Private _ColorsSaved As Boolean = False
    Private _SettingColors As Boolean = False

    Private _BackColorDisabled As Color = SystemColors.Control
    Private _ForeColorDisabled As Color = SystemColors.WindowText

    Private Const WM_ENABLE As Integer = &HA

    Private Sub DisTextBox_VisibleChanged(sender As Object, e As System.EventArgs) Handles Me.VisibleChanged
        If Not Me._ColorsSaved AndAlso Me.Visible Then
            ' Save the ForeColor/BackColor so we can switch back to them later
            _ForeColorBackup = Me.ForeColor
            _BackColorBackup = Me.BackColor
            _ColorsSaved = True

            If Not Me.Enabled Then ' If the window starts out in a Disabled state...
                ' Force the TextBox to initialize properly in an Enabled state,
                ' then switch it back to a Disabled state
                Me.Enabled = True
                Me.Enabled = False
            End If

            SetColors() ' Change to the Enabled/Disabled colors specified by the user
        End If
    End Sub

    Protected Overrides Sub OnForeColorChanged(e As System.EventArgs)
        MyBase.OnForeColorChanged(e)

        ' If the color is being set from OUTSIDE our control,
        ' then save the current ForeColor and set the specified color
        If Not _SettingColors Then
            _ForeColorBackup = Me.ForeColor
            SetColors()
        End If
    End Sub

    Protected Overrides Sub OnBackColorChanged(e As System.EventArgs)
        MyBase.OnBackColorChanged(e)

        ' If the color is being set from OUTSIDE our control,
        ' then save the current BackColor and set the specified color
        If Not _SettingColors Then
            _BackColorBackup = Me.BackColor
            SetColors()
        End If
    End Sub

    Private Sub SetColors()
        ' Don't change colors until the original ones have been saved,
        ' since we would lose what the original Enabled colors are supposed to be
        If _ColorsSaved Then
            _SettingColors = True
            If Me.Enabled Then
                Me.ForeColor = Me._ForeColorBackup
                Me.BackColor = Me._BackColorBackup
            Else
                Me.ForeColor = Me.ForeColorDisabled
                Me.BackColor = Me.BackColorDisabled
            End If
            _SettingColors = False
        End If
    End Sub

    Protected Overrides Sub OnEnabledChanged(e As System.EventArgs)
        MyBase.OnEnabledChanged(e)

        SetColors() ' change colors whenever the Enabled() state changes
    End Sub

    Public Property BackColorDisabled() As System.Drawing.Color
        Get
            Return _BackColorDisabled
        End Get
        Set(ByVal Value As System.Drawing.Color)
            If Not Value.Equals(Color.Empty) Then
                _BackColorDisabled = Value
            End If
            SetColors()
        End Set
    End Property

    Public Property ForeColorDisabled() As System.Drawing.Color
        Get
            Return _ForeColorDisabled
        End Get
        Set(ByVal Value As System.Drawing.Color)
            If Not Value.Equals(Color.Empty) Then
                _ForeColorDisabled = Value
            End If
            SetColors()
        End Set
    End Property

    Protected Overrides ReadOnly Property CreateParams As System.Windows.Forms.CreateParams
        Get
            Dim cp As System.Windows.Forms.CreateParams
            If Not Me.Enabled Then ' If the window starts out in a disabled state...
                ' Prevent window being initialized in a disabled state:
                Me.Enabled = True ' temporary ENABLED state
                cp = MyBase.CreateParams ' create window in ENABLED state
                Me.Enabled = False ' toggle it back to DISABLED state 
            Else
                cp = MyBase.CreateParams
            End If
            Return cp
        End Get
    End Property

    Protected Overrides Sub WndProc(ByRef m As System.Windows.Forms.Message)
        Select Case m.Msg
            Case WM_ENABLE
                ' Prevent the message from reaching the control,
                ' so the colors don't get changed by the default procedure.
                Exit Sub ' <-- suppress WM_ENABLE message

        End Select

        MyBase.WndProc(m)
    End Sub

End Class

编译,您应该在ToolBox的顶部获得新控件。放置DisTextBox控件和表单,并将BackColorDisabled属性设置为白色。现在,当你禁用它时,BackColor应该保持白色:

Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
    TextBox1.Enabled = Not TextBox1.Enabled
    DisTextBox1.Enabled = Not DisTextBox1.Enabled

    Label1.Text = TextBox1.Enabled.ToString
    Label2.Text = DisTextBox1.Enabled.ToString
End Sub

屏幕截图...常规TextBox位于顶部;底部的DisTextBox

启用:

A New Approach for Custom Colors in a Disabled VB.Net WinForms TextBox

禁用:

The DisTextBox while Enabled

答案 1 :(得分:0)

如果WinForms这样做:

Protected Sub TextBox1_EnabledChanged(sender As Object, e As EventArgs)
    TextBox1.ForeColor = Color.White
End Sub

然后在Form_Load上执行此操作:

TextBox1.Enabled = false;