Re-orient text direction

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?
T HoecherlDeveloperAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Karrtik IyerSoftware ArchitectCommented:
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

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
T HoecherlDeveloperAuthor 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 ArchitectCommented:
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
Rowby Goren Makes an Impact on Screen and Online

Learn about longtime user Rowby Goren and his great contributions to the site. We explore his method for posing questions that are likely to yield a solution, and take a look at how his career transformed from a Hollywood writer to a website entrepreneur.

T HoecherlDeveloperAuthor 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 ArchitectCommented:
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 HoecherlDeveloperAuthor 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 ArchitectCommented:
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 HoecherlDeveloperAuthor 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 HoecherlDeveloperAuthor Commented:
Thanks for your help, Karrtik.

T
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Visual Basic.NET

From novice to tech pro — start learning today.