Vertical label

I would like to add a vertica label on my from, is there somebody can help me with that


Answer this question

Vertical label

  • wtrn

    I assumed you wanted the text vertical too. Add a new class to your project and paste the code shown below. I couldn't get it quite perfect, MeasureText is behaving oddly.

    Imports System.Text
    Public Class VerticalLabel
    Inherits Label
    Private mSize As Size

    Public Sub New()
    AutoSize = False
    End Sub

    Private Sub CalculateSize(ByVal value As String)
    mSize = TextRenderer.MeasureText(value, Me.Font)
    Me.Height = mSize.Width
    Me.Width = mSize.Height
    End Sub

    Public Overrides Property Text() As String
    Get
    Return MyBase.Text
    End Get
    Set(ByVal value As String)
    MyBase.Text = value
    CalculateSize(value)
    End Set
    End Property

    Public Overrides Property Font() As System.Drawing.Font
    Get
    Return MyBase.Font
    End Get
    Set(ByVal value As System.Drawing.Font)
    MyBase.Font = value
    CalculateSize(MyBase.Text)
    End Set
    End Property

    Protected Overrides Sub OnPaint(ByVal e As System.Windows.Forms.PaintEventArgs)
    Dim br As New SolidBrush(Me.ForeColor)
    e.Graphics.RotateTransform(-90)
    e.Graphics.DrawString(Text, Font, br, -mSize.Width, 0)
    br.Dispose()
    End Sub
    End Class



  • Jonathan MacCollum

    thank you very much, works a treat, however i was playing with the angle.... it only works in -90

    is there an easy way to rotate 90 degrees the other way, i would of thought 90 or +90 would of done the job but ut never :o(


  • jmarkmcdow

    Thanx all..

    Nobugz, it works very well for me, thanks a lot



  • Adam.Kahtava

    If you set the Label.AutoSize property to false and then size the label so that it is only wide enough for one character to be displayed but long enough to display all characters vertically you'll have a vertical label.

    If you need more control than that you'll probably want to create a custom control.



  • Jonathan Wong

    i have a little problem, i have created the class file... but how do i get the label to become vertical is there something im missing like linking that particular label with the class file
  • DaPosh

    Once you have added the above class to your project, you just need to build it.  Then, while viewing the form designer, the toolbox will contain a section called "ApplicationName Components" and under it you should see the "VerticalLabel" control.  You simply drag it from the toolbox to the form as normal.  You can also create new instances of the VerticalLabel in code and add them to the form's controls collection.

    Note that you'll need to add some code to the constructor of the VerticalLabel to set the AutoSize property to False, or do so manually after adding one to the form, before it will display correctly.

    -EDIT-

    I just noticed that there is code to set the AutoSize = False, but it doesn't have affect during design time.  This is because the designer is changing the property to True after it adds one to the form.  You'll probably want to force the AutoSize to False permanently since the control sizes itself anyway based on the text and font.  Example:

    Public Sub New()
         MyBase.New()
         MyBase.AutoSize = False
    End Sub

    Public Overrides Property AutoSize() As Boolean
         Get
              Return False
         End Get

         Set(ByVal value As Boolean)
              MyBase.AutoSize = False
         End Set
    End Property



  • ridvan

    That's only half of what you need to do...

    After rotating +90, you have to change the X,Y location of the text also. The modified OnPaint might be:

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

    Dim br As New SolidBrush(Me.ForeColor)

    e.Graphics.RotateTransform(90)

    e.Graphics.DrawString(Text, Font, br, 0, -mSize.Height)

    br.Dispose()

    End Sub

    Hope that helps!



  • Vertical label