Changing TextBox font color in disabled mode

G

Guest

Using the winform TextBox in disabled mode, the background changes to the
Control color (gray) and the foreground to another shade of gray.
What I want is a TextBox with only one of the two changing, i.e. a gray
background with black text in disabled mode.
I tried overriding the OnPaint(), which looks ok as long as I do not use the
TextBox in enabled mode. If I select the text in the TextBox it seems that
this text is not painted by the OnPaint(), it uses another font!

Isn't there another way to just change the foreground color in disabled mode?

Thanks,
Bert Heesbeen
 
G

Guest

Thank you Eric,

I already played with the UserPaint, but setting this style in the
OnEnabledChanged property was new for me.

At first it looked very good and I thought it solved my problem. But when I
changed a previously disabled TextBox on a visible form to enabled, I noticed
that it was still working as UserPaint=true, which was wrong.

So, not solved yet. I am looking forward to new suggestions.

Bert
 
E

Eric Moreau

This class is working well for me:

Option Strict On

Imports System.Drawing
Imports System.Windows.Forms
Imports System.ComponentModel

Public Class cTextboxEx
Inherits System.Windows.Forms.TextBox
Implements IDisabled

'Tous les événements interceptés doivent être relancés
Public Shadows Event MouseDown As EventHandler
Public Shadows Event TextChanged As EventHandler

Private mblnClicked As Boolean
Private mclrBackColorDisabled As Color = Color.Gainsboro
Private mclrForeColorDisabled As Color = SystemColors.WindowText

Public Property BackColorDisabled() As System.Drawing.Color Implements
IDisabled.BackColorDisabled
Get
Return mclrBackColorDisabled
End Get
Set(ByVal Value As System.Drawing.Color)
mclrBackColorDisabled = Value
End Set
End Property

Public Property ForeColorDisabled() As System.Drawing.Color Implements
IDisabled.ForeColorDisabled
Get
Return mclrForeColorDisabled
End Get
Set(ByVal Value As System.Drawing.Color)
mclrForeColorDisabled = Value
End Set
End Property

Protected Overrides Sub OnPaint(ByVal e As
System.Windows.Forms.PaintEventArgs)
MyBase.OnPaint(e)
Dim TextBrush As SolidBrush
Dim sf As New StringFormat

'ref: http://www.dotnet247.com/247reference/msgs/54/271869.aspx
Select Case Me.TextAlign
Case HorizontalAlignment.Center
sf.Alignment = StringAlignment.Center
Case HorizontalAlignment.Left
sf.Alignment = StringAlignment.Near
Case HorizontalAlignment.Right
sf.Alignment = StringAlignment.Far
End Select

Dim rDraw As RectangleF = RectangleF.op_Implicit(Me.ClientRectangle)
rDraw.Inflate(0, 0)

If Me.Enabled Then
TextBrush = New SolidBrush(Me.ForeColor)
Else
TextBrush = New SolidBrush(Me.ForeColorDisabled)
Dim BackBrush As New SolidBrush(Me.BackColorDisabled)
e.Graphics.FillRectangle(BackBrush, 0.0F, 0.0F, Me.Width, Me.Height)
BackBrush.Dispose()
BackBrush = Nothing
End If
e.Graphics.DrawString(Me.Text, Me.Font, TextBrush, rDraw, sf)
TextBrush.Dispose()
TextBrush = Nothing

sf.Dispose()
sf = Nothing
End Sub

Protected Overrides Sub OnEnabledChanged(ByVal e As System.EventArgs)
MyBase.OnEnabledChanged(e)
If Not Me.Enabled Then
Me.SetStyle(ControlStyles.UserPaint, True)
Else
Me.SetStyle(ControlStyles.UserPaint, False)
End If

Me.Invalidate()
End Sub

Protected Overrides Sub OnEnter(ByVal e As System.EventArgs)
MyBase.OnEnter(e)
Me.SelectAll()
End Sub

Protected Overrides Sub OnMouseDown(ByVal e As
System.Windows.Forms.MouseEventArgs)
If Not mblnClicked Then
Me.SelectAll()
mblnClicked = True
End If
RaiseEvent MouseDown(Me, e)
End Sub

Protected Overrides Sub OnTextChanged(ByVal e As System.EventArgs)
If Me.TextLength > Me.MaxLength Then
Me.Text = Me.Text.Substring(0, Me.MaxLength)
End If

RaiseEvent TextChanged(Me, e)
End Sub

Protected Overrides Sub CreateHandle()
'Pour mettre le contenu du textbox vide à la création
If Me.DesignMode Then
Me.Text = ""
End If
MyBase.CreateHandle()
End Sub

End Class


--


HTH

Éric Moreau, MCSD, Visual Developer - Visual Basic MVP
(http://aspnet2.com/mvp.ashx?EricMoreau)
Conseiller Principal / Senior Consultant
Concept S2i inc. (www.s2i.com)
http://pages.videotron.com/emoreau/
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top