How do I rotate a label in Vb net?

12,929

so after checking a little, this line

e.Graphics.TranslateTransform(MetroLabel50.ClientSize.Width, MetroLabel50.ClientSize.Height)

should be

e.Graphics.TranslateTransform(csng(MetroLabel50.ClientSize.Width/2), csng(MetroLabel50.ClientSize.Height/2))

you have to set it in the middle

also change Handles Me.Paint by Handles label1.Paint

sample code;

step 1, new project
step 2, drop a label in middle of the form
step 3, put that code

Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
    Label1.AutoSize = False
    Label1.Text = ""
    Label1.Width = 75
    Label1.Height = 75
    Label1.Refresh()
End Sub

Private Sub Label1_Paint(sender As Object, e As PaintEventArgs) Handles Label1.Paint
    e.Graphics.TranslateTransform(CSng(Label1.Width / 2), CSng(Label1.Height / 2))
    e.Graphics.RotateTransform(90)
    e.Graphics.DrawString("Hello", Label1.Font, Brushes.Black, New Point(0, 0))
    e.Graphics.ResetTransform()
End Sub

step 4, run the application

Share:
12,929
user6641274
Author by

user6641274

Updated on June 14, 2022

Comments

  • user6641274
    user6641274 almost 2 years

    I'm trying to rotate a label 90 degrees in Vb net and cannot get it working. My code is as follows. Any help would be appreciated.


        Private Sub Label1_Paint(ByVal sender As Object, ByVal e As System.Windows.Forms.PaintEventArgs) Handles Me.Paint
    
        Dim sf As New StringFormat
    
        sf.Alignment = StringAlignment.Center
    
        sf.LineAlignment = StringAlignment.Center
    
        MetroLabel50.Text = ""
    
        e.Graphics.TranslateTransform(MetroLabel50.ClientSize.Width, MetroLabel50.ClientSize.Height)
    
        e.Graphics.RotateTransform(90)
    
        e.Graphics.DrawString("Label", MetroLabel50.Font, Brushes.Black, RectangleF.op_Implicit(MetroLabel50.ClientRectangle), sf)
    
        e.Graphics.ResetTransform()
    
    End Sub
    
  • user6641274
    user6641274 almost 8 years
    Still getting nowhere. I've put in test label text and it doesn't even blank it on load so I'm assuming the sun hasn't even started
  • mrnakumar
    mrnakumar almost 8 years
    @user6641274 make sure autosize is false, clearing the label will make it very small
  • user6641274
    user6641274 almost 8 years
    Having manually typed "test" in the properties text window when the application loads I still see the word "test" . Would this not mean that the line metrolabel50.text="" has not been successful and that the sub has just not started?
  • mrnakumar
    mrnakumar almost 8 years
    @user6641274, your paint is bind to the form, do it on the label
  • user6641274
    user6641274 almost 8 years
    I noticed that one a bit earlier and swapped handles me.paint to metrolabel50.paint but still no change. Do I have to invoke the sub in the load sub?
  • user6641274
    user6641274 almost 8 years
    thanks for your help. It actually looks like it's something to do with using metro ui labels rather than normal ones. Once swapped for a normal label it worked perfect. Thanks for your help!