Reputation: 2986
I am using windows forms. The default forecolor of a textbox1.enabled = false is gray, how can I change it to white?
Upvotes: 0
Views: 5823
Reputation: 39142
From my article at Experts-Exchange:
A New Approach for Custom Colors in a Disabled VB.Net WinForms TextBox
Here's the DisTextBox
class:
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
Compile and you should get the new control at the top of your ToolBox. Place a DisTextBox control and your form and set the BackColorDisabled
property to White. Now the BackColor should stay white when you disable it:
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
Screenshots...regular TextBox
on top; the DisTextBox
on the bottom.
Enabled:
Disabled:
Upvotes: 3
Reputation: 1542
If WinForms do this:
Protected Sub TextBox1_EnabledChanged(sender As Object, e As EventArgs)
TextBox1.ForeColor = Color.White
End Sub
and then on Form_Load do this:
TextBox1.Enabled = false;
Upvotes: 0