Hello,
You cannot use
ContentAlignment
with
DrawString
, you need to create a
StringFormat
object and then apply the operation like so:
Private Sub label1_Paint(ByVal sender As Object, ByVal e As System.Windows.Forms.PaintEventArgs) Handles label1.Paint
Using sFormat As New StringFormat
sFormat.Alignment = StringAlignment.Center
sFormat.LineAlignment = StringAlignment.Center
e.Graphics.DrawString(label1.Text, label1.Font, Brushes.Yellow, label1.ClientRectangle, sFormat)
End Using
End Sub
I hope this helps,
Keith
NOTE: I created this extension to allow conversion from the ContentAlignment object.
<Extension()>
Public Function ToStringFormat(ByRef cAlignment As ContentAlignment) As StringFormat
Dim rFormat As New StringFormat(StringFormat.GenericDefault)
With rFormat
Select Case cAlignment
Case ContentAlignment.TopLeft, ContentAlignment.TopCenter, ContentAlignment.TopRight
.LineAlignment = StringAlignment.Near
Case ContentAlignment.BottomLeft, ContentAlignment.BottomCenter, ContentAlignment.BottomRight
.LineAlignment = StringAlignment.Far
Case Else
.LineAlignment = StringAlignment.Center
End Select
Select Case cAlignment
Case ContentAlignment.TopLeft, ContentAlignment.MiddleLeft, ContentAlignment.BottomLeft
.Alignment = StringAlignment.Near
Case ContentAlignment.TopRight, ContentAlignment.MiddleRight, ContentAlignment.BottomRight
.Alignment = StringAlignment.Far
Case Else
.Alignment = StringAlignment.Center
End Select
End With
Return rFormat
End Function
With it you could just call:
Dim MyAlignment as ContentAlignment = ContentAlignment.MiddleCenter
e.Graphics.DrawString(label1.Text, label1.Font, Brushes.Yellow, label1.ClientRectangle, MyAlignment.ToStringFormat)