Re-orient text direction

T Hoecherl
T Hoecherl used Ask the Experts™
on
Is there a way in vb.net to change the direction of a label or a text box so it is vertical and reads bottom to top, rather than being horizontal and reading left to right?
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Software Architect
Commented:
Something like below:
1> Create a new class that inherits from Label called Vertical Label, you can right click on your project, say add, and add a new class and copy/paste the below code in it.
Public Class VerticalLabel

    Inherits System.Windows.Forms.Label
    Private _caption As String
    Public Overrides Property Text As String
        Get
            Return _caption
        End Get
        Set(value As String)
            _caption = value
            reDraw()
        End Set
    End Property   

    Private _rotatedSize As Size
    Private Property rotatedSize() As Size
        Get
            Return _rotatedSize
        End Get
        Set(ByVal value As Size)
            _rotatedSize = value
        End Set
    End Property

    Private _upToDown As Boolean = False
    Public Property upToDown() As Boolean
        Get
            Return _upToDown
        End Get
        Set(ByVal value As Boolean)
            _upToDown = value
            MyBase.Invalidate()
        End Set
    End Property

    Public Overrides Property Font() As Font
        Get
            Return MyBase.Font
        End Get
        Set(ByVal value As Font)
            MyBase.Font = value
            reDraw()
        End Set
    End Property

    Public Overrides Property ForeColor() As Color
        Get
            Return MyBase.ForeColor
        End Get
        Set(ByVal value As Color)
            MyBase.ForeColor = value
            reDraw()
        End Set
    End Property

    Public Sub New()

    End Sub

    Private Sub reDraw()
        Dim s As Size = Me.CreateGraphics.MeasureString(Text, Me.Font).ToSize
        rotatedSize = New Size(s.Height, s.Width)
        Me.MinimumSize = rotatedSize
        Me.MaximumSize = rotatedSize
        MyBase.Invalidate()
    End Sub

    Protected Overrides Sub OnCreateControl()
        MyBase.OnCreateControl()
        Text = If(Text = "", Me.Name, Text)
        reDraw()
    End Sub

    Protected Overrides Sub OnPaint(ByVal e As System.Windows.Forms.PaintEventArgs)

        If upToDown Then
            e.Graphics.RotateTransform(90)
            e.Graphics.DrawString(Text, Me.Font, New SolidBrush(MyBase.ForeColor), 0, -rotatedSize.Width)
        Else
            e.Graphics.RotateTransform(270)
            e.Graphics.DrawString(Text, Me.Font, New SolidBrush(MyBase.ForeColor), -rotatedSize.Height, 0)
        End If

        e.Graphics.ResetTransform()
        
    End Sub

End Class

Open in new window

2> Then compile your code, so that when you go to the design mode of your form, you can see your vertical label in the tool box as shown below.
1-9-2016-11-52-06-AM.png3> Then drag drop the vertical label to your form.
4> In form load, you can set the text you want and also set the angle either as 90 or 270 as shown below.
Private Sub Form1_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
        VerticalLabel1.Text = "I am vertical"
        VerticalLabel1.upToDown = True 'False Means 270, if true then 90

End Sub

Open in new window

5> Output is something like below.
1-9-2016-11-48-48-AM.png
T HoecherlDeveloper

Author

Commented:
Brilliant, Karrtik lyer.  You are a genius.  Thanks for teaching me this.  There is one small problem though and perhaps you can help me solve it.

When I drag the VerticalLabel object onto the form, I get this message:

"Failed to load toolbox item 'VerticalLabel'.  It will be removed from the toolbox."

Then it disappears from the toolbox until I recompile.  Any idea why this is happening?

T
Karrtik IyerSoftware Architect

Commented:
Make sure the build configuration of your project or the projects in question (that contains this vertical label and the one using it) is set to any cpu. Please refer link below which Microsoft acknowledges.
https://support.microsoft.com/en-us/kb/963017
C++ 11 Fundamentals

This course will introduce you to C++ 11 and teach you about syntax fundamentals.

T HoecherlDeveloper

Author

Commented:
Thank you, Karrtik.  That setting made it possible to add the VerticalLabel to the form.  I can run the program and see the vertical label.  An image is attached called VerticalLabelImage.docx.

VerticalLabelImage.docx

However, when I try to build the program to create a new msi file, I get the error shown in this attachment called VerticalLabelError.docx.

VerticalLabelError.docx

Do you know why I am getting this error?

T
Karrtik IyerSoftware Architect

Commented:
Can you press on call stack in the error and paste that? Also how are your projects set up, I mean have you added the vertical label to your exe/app as a class, or have you added the vertical label to a DLL and that's is referred in the exe?
Just seems some set up or configuration issue in getting the msi built.
T HoecherlDeveloper

Author

Commented:
Here's the call stack:

1.   Hide Call Stack
 
at System.ComponentModel.Design.Serialization.CodeDomSerializerBase.Error(IDesignerSerializationManager manager, String exceptionText, String helpLink)
at System.ComponentModel.Design.Serialization.CodeDomSerializerBase.DeserializeExpression(IDesignerSerializationManager manager, String name, CodeExpression expression)
at System.ComponentModel.Design.Serialization.CodeDomSerializerBase.DeserializeExpression(IDesignerSerializationManager manager, String name, CodeExpression expression)
at System.ComponentModel.Design.Serialization.CodeDomSerializerBase.DeserializeStatement(IDesignerSerializationManager manager, CodeStatement statement)  
   
I added the vertical label to my application as a class.

Thanks,
Karrtik IyerSoftware Architect

Commented:
Strange, I suggest you open another question for this issue so that more people can help you. Are you getting this as a compilation error, or this error only pops up when form is opened in designer mode?
T HoecherlDeveloper

Author

Commented:
The error occurs when I try to open the form in designer mode, but it first occurred after I added the Vertical Label and then attempted to build the project.
T HoecherlDeveloper

Author

Commented:
Thanks for your help, Karrtik.

T

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial