Friday, June 1, 2007

Marking madatory textboxes/fields with style

Now everyone in VS2003/2005 who likes graphics has done some code in the onPaint event to draw nice little things, but have you ever tried drawing on a textbox, or any other control for that matter. Wemay also be tired of just filling madatory fields with a plain backcolor fill also. So what I set out to do was draw a nice little glyph in the corner to signify that a filed was mandatory

First off we must subclass the good old textbox, but hopefully you aer doing this already in your component lib. Next it's a matter of hooking up to the windows message and drawing accordingly.


Public Class Component1
Inherits TextBox
Private Const WM_PAINT As Integer = &HF

Private bMandatory

Public Property Mandatory() As Boolean
Get
Return bMandatory
End Get
Set(ByVal value As Boolean)
bMandatory = value
Me.Invalidate()
End Set
End Property

Protected Overrides Sub WndProc(ByRef m As Message)
Try
MyBase.WndProc(m)
If m.Msg = WM_PAINT And bMandatory Then
Dim g As Graphics = Me.CreateGraphics
Dim pts As PointF() = {New PointF(0, 0), New PointF(5, 0), _
New PointF(0, 5)}
g.FillPolygon(Brushes.Red, pts)
End If
Catch ex As Exception
' Call error handler
End Try

End Sub

End Class
You should end up with this ...

No comments: