Reputation: 141
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
Upvotes: 2
Views: 17516
Reputation: 1
Here is an easy way to display rotated text on your vb.net program's form. When the form is loaded, this code is executed to display the rotated label. Put this code in the forms code section.
Private Sub Form1_Paint(sender As Object, e As PaintEventArgs) Handles MyBase.Paint
Dim myText As String = "Vertical text"
Dim fontFamily As New FontFamily("Lucida Console")
Dim font As New Font(fontFamily, 14, FontStyle.Regular, GraphicsUnit.Point)
Dim pointF As New PointF(40, 10)
Dim stringFormat As New StringFormat()
Dim solidBrush As New SolidBrush(Color.FromArgb(255, 0, 0, 255))
' Rotate the text -90 degrees
e.Graphics.TranslateTransform(pointF.X, pointF.Y)
e.Graphics.RotateTransform(-90)
e.Graphics.DrawString(myText, font, solidBrush, 0, 0, stringFormat)
' Reset the transformation matrix
e.Graphics.ResetTransform()
End Sub
Upvotes: 0
Reputation: 1
In VB.NET, the Label control doesn't have a built-in property to set the text vertically. However, you can achieve this effect by creating a custom control or by manipulating the label's appearance using graphics transformations. One common approach is to rotate the text 90 degrees to achieve a vertical effect.
Imports System.Drawing
Public Class VerticalLabel
Inherits Label
Protected Overrides Sub OnPaint(e As PaintEventArgs)
Dim g As Graphics = e.Graphics
g.TranslateTransform(0, Me.Height)
g.RotateTransform(-90)
g.DrawString(Me.Text, Me.Font, New SolidBrush(Me.ForeColor), New PointF(0, 0))
End Sub
End Class
After building your project, you should see the VerticalLabel control in your toolbox. You can then add it to your form and set its properties like a regular label. Keep in mind that this implementation might need further adjustments depending on your specific use case.
This is the simplest way that i got, but unfortunately it does lack a lot of tweaks.
Upvotes: 0
Reputation: 1
I have searched a lot to figure this out so I am sharing my simple code here I have created a PictureBox to place the text in you can change the PicBox Size after populating it to keep aspect ratio
Dim bmp As New Bitmap(TextToRotateLabel.Width, TextToRotateLabel.Height)
TextToRotateLabel.DrawToBitmap(bmp, New Rectangle(0, 0,
TextToRotateLabel.Width, TextToRotateLabel.Height))
bmp.RotateFlip(RotateFlipType.Rotate270FlipNone)
bmp.Save("c:\Path\text.bmp")
PicBox.Image = bmp
Upvotes: 0
Reputation: 20140
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
Upvotes: 3