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/