Click here to Skip to main content
15,892,005 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi guys ...

I have a label control on a form that gets different sized words in it.

I just want to get the text to be middle center all the time.

I set the control label1 itself to middle center in the textalign property of the control.

I also tried programatically if it is the proper command I am not a hundred percent sure.

my prob is that it always paints left alligned.

the code is below

Thanks

What I have tried:

Private Sub label1_Paint(ByVal sender As Object, ByVal e As System.Windows.Forms.PaintEventArgs) Handles label1.Paint
label1.BringToFront()
label1.Text = label1.Text
e.Graphics.DrawString(label1.Text, label1.Font, Brushes.Yellow, RectangleF.op_Implicit(label1.ClientRectangle))
label1.TextAlign = ContentAlignment.MiddleCenter
End Sub
Posted
Updated 3-Mar-16 21:58pm

1 solution

Hello,

You cannot use ContentAlignment with DrawString, you need to create a StringFormat object and then apply the operation like so:

VB
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.

VB
''' <summary>
''' Converts current ContentAlignment to a new StringFormat using StringFormat.GenericDefault
''' </summary>
''' <returns>New StringFormat.GenricDefault with current alignment settings</returns>
<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:
VB
Dim MyAlignment as ContentAlignment = ContentAlignment.MiddleCenter
e.Graphics.DrawString(label1.Text, label1.Font, Brushes.Yellow, label1.ClientRectangle, MyAlignment.ToStringFormat)
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900