Public Class TextBoxEx
Inherits System.Windows.Forms.TextBox
' ... other code ...
Protected Overrides Sub OnEnabledChanged(ByVal e As System.EventArgs)
MyBase.OnEnabledChanged(e)
' *We only UserPaint when in a DISABLED state*
Me.SetStyle(ControlStyles.UserPaint, Not Me.Enabled)
Me.Refresh()
End Sub
Protected Overrides Sub OnPaint(ByVal e As System.Windows.Forms.PaintEventArgs)
MyBase.OnPaint(e)
' *We only UserPaint when in a DISABLED state*
Using BackBrush As New SolidBrush(Me.BackColorDisabled)
e.Graphics.FillRectangle(BackBrush, Me.ClientRectangle)
End Using
Using ForeBrush As New SolidBrush(Me.ForeColorDisabled)
Dim sf As New StringFormat
Select Case Me.TextAlign
Case HorizontalAlignment.Left
sf.Alignment = StringAlignment.Near
Case HorizontalAlignment.Center
sf.Alignment = StringAlignment.Center
Case HorizontalAlignment.Right
sf.Alignment = StringAlignment.Far
End Select
e.Graphics.DrawString(Me.Text, Me.Font, ForeBrush, Me.ClientRectangle, sf)
End Using
End Sub
' ... other code ...
End Class
"Enables or disables mouse and keyboard input to the specified window or control. When input is disabled, the window does not receive input such as mouse clicks and key presses. When input is enabled, the window receives all input."
"When the WM_CANCELMODE message is sent, the DefWindowProc function cancels internal processing of standard scroll bar input, cancels internal menu processing, and releases the mouse capture."
"A disabled window cannot receive input from the user."
"Sent when an application changes the enabled state of a window. It is sent to the window whose enabled state is changing. This message is sent before the EnableWindow function returns, but after the enabled state (WS_DISABLED style bit) of the window has changed. A window receives this message through its WindowProc function."
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
DisTextBox1.Enabled = False ' Disable the control (automatically switches to disabled colors)
DisTextBox1.Enabled = True ' Enable the control (automatically switches to enabled colors)
Have a question about something in this article? You can receive help directly from the article author. Sign up for a free trial to get started.
Comments (1)
Commented: