继承System.Windows.Forms.TextBox,想更改其Readonly改变时的背景色.
我用了以下两个方法,可是都不好用
1.重写OnReadOnlyChanged,导致重绘控件。
Protected Overrides Sub OnReadOnlyChanged(ByVal e As System.EventArgs)
MyBase.OnReadOnlyChanged(e)
If Not Me.ReadOnly Then
Me.SetStyle(ControlStyles.UserPaint, True)
Else
Me.SetStyle(ControlStyles.UserPaint, False)
End If
Me.Invalidate()
End Sub
Protected Overrides Sub OnPaint(ByVal e As System.Windows.Forms.PaintEventArgs)
MyBase.OnPaint(e)
Dim TextBrush As SolidBrush
Dim sf As New StringFormat
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 Not Me.RReadOnly Then
TextBrush = New SolidBrush(Me.ForeColor)
Else
TextBrush = New SolidBrush(Me.ForeColor)
Dim BackBrush As New SolidBrush(Me.LockColor)
e.Graphics.FillRectangle(BackBrush, 0.0F, 0.0F, Me.Width, Me.Height)
End If
e.Graphics.DrawString(Me.Text, Me.Font, TextBrush, rDraw, sf)
End Sub
2.重写ReadOnly属性,调用PropertyChanged事件
Implements INotifyPropertyChanged
Private me_ReadOnly As Boolean = False
Public Shadows Event PropertyChanged(ByVal sender As Object, ByVal e As System.ComponentModel.PropertyChangedEventArgs) Implements System.ComponentModel.INotifyPropertyChanged.PropertyChanged
Private Sub NotifyPropertyChanged(ByVal info As String)
RaiseEvent PropertyChanged(Me, New PropertyChangedEventArgs(info))
End Sub
Public Overloads Property [ReadOnly]() As Boolean
Get
Return me_ReadOnly
End Get
Set(ByVal Value As Boolean)
me_ReadOnly = Value
If Not (Value = me_ReadOnly) Then
me_ReadOnly = Value
NotifyPropertyChanged( "ReadOnly ")
End If
End Set
End Property
Public Sub ReadOnly_OnChanged(ByVal sender As Object, ByVal e As PropertyChangedEventArgs) Handles Me.PropertyChanged
If Not me_ReadOnly Then
Me.BackColor = Color.White
Else
Me.BackColor = Me.LockColor
End If
Me.Invalidate()
End Sub
还望高人指点,如何不让TextBox设置ReadOnly时,背景色变成灰色,而变成自己自定义的颜色。(LockColor)
[解决办法]
你可以这样来写你的类:
Public Class ReadOnlyTextBox
Inherits TextBox
Private m_BackColor As Color
Public Sub New()
Me.m_BackColor = Color.Red
End Sub
Protected Overrides Sub OnReadOnlyChanged(ByVal e As EventArgs)
MyBase.OnReadOnlyChanged(e)
Me.BackColor = Me.m_BackColor
End Sub
Public Property MyBackColor As Color
Get
Return Me.m_BackColor
End Get
Set(ByVal value As Color)
Me.m_BackColor = value
Me.BackColor = Me.m_BackColor
End Set
End Property
End Class
[解决办法]
Public Class ReadOnlyTextBox
Inherits TextBox
Private _ReadOnlyColor As Color=Color.Red
Private _BackColor As Color
Public Sub New()
_BackColor = BackColor
End Sub
Protected Overrides Sub OnReadOnlyChanged(ByVal e As EventArgs)
MyBase.OnReadOnlyChanged(e)
Me.BackColor=iif(ReadOnly, _ReadOnlyColor, _BackColor)
End Sub
Public Overrides Property BackColor As Color
Get
Return MyBase.BackColor
End Get
Set(ByVal value As Color)
MyBase.BackColor = value
_BackColor = value
End Set
End Property
End Class