Article Options
Recently Viewed
Premium Sponsor
Premium Sponsor

 »  Home  »  .NET Newbie  »  Text Techniques (1)  »  Vertical Text: Bottom to Top
 »  Home  »  Windows Development  »  Graphics  »  Text Techniques (1)  »  Vertical Text: Bottom to Top
Text Techniques (1)
by Ged Mead | Published  03/30/2005 | .NET Newbie Graphics | Rating:
Vertical Text: Bottom to Top

Problem:

   You want to want to write a text string on a label vertically, bottom to top (rotated 90 degrees anticlockwise

Solution

   Draw the String using the DrawString method of the Graphics Class.  Use Transformations to move the start point of the text downwards and to rotate the text from horizontal to vertical.


The Code

      Reading some of the textbooks and articles that deal with Graphics Transformations can seriously damage your sanity!   Luckily, the transformations we are going to apply to this label’s text are relatively easy to follow if, as before, we break the code down into manageable, understandable chunks.

   Here is the code which produces the label text shown in the graphic at the top of this page:

Imports

System.Drawing.Text

Private Sub Label2_Paint(ByVal sender As Object, ByVal e As System.Windows.Forms.PaintEventArgs) Handles Label2.Paint
  '  Draw text vertically, from bottom to top
  '   The text to be drawn on the label
   Dim strText As String = "Bottom to Top"
   '  The Graphics object for this label
   Dim g2 As Graphics = e.Graphics
  '  The font, size and style to draw text 
  Dim fnt As Font = New Font("Arial", 12, FontStyle.Bold)
  '   Move origin point of text down to bottom of label
  g2.TranslateTransform(13, Label2.Height - 25)
  '  Rotate the text the required number of degrees
  g2.RotateTransform(270)
  '  Optional TextRenderingHint
  g2.TextRenderingHint = TextRenderingHint.ClearTypeGridFit
  '  Finally we draw the string
  g2.DrawString(strText, fnt, Brushes.White, 0, 0)
 End Sub

  That's it!  Run this code and the text will appear in the label bottom to top.  There is a more detailed breakdown on the next page.

Comments    Submit Comment

Comment #1  (Posted by an unknown user on 03/30/2005)
Rating
Useful!
 
Comment #2  (Posted by an unknown user on 06/18/2005)
Rating
Verry good.
 
Comment #3  (Posted by luchun on 07/03/2005)
Rating
it's good method,but my label change nothing.
 
Comment #4  (Posted by shashi kant mehta on 11/22/2005)
Rating
we forget small things but later it irritate to after complication the projet.
so i think itis very good to know these small thigs but very valuable
 
Comment #5  (Posted by an unknown user on 11/26/2005)
Rating
Great example, Thanks
 
Comment #6  (Posted by james on 01/22/2006)
Rating
is a very usefull tip.
 
Comment #7  (Posted by an unknown user on 03/14/2006)
Rating
Great. Simply and usefull.
 
Comment #8  (Posted by an unknown user on 11/11/2006)
Rating
Good to help me. Thanks
 
Comment #9  (Posted by an unknown user on 11/11/2006)
Rating
Good to help me. Thanks
 
Comment #10  (Posted by Bobby on 11/12/2006)
Rating
Great, just what was needed :-) Even though i write in C#.
 
Comment #11  (Posted by Louise Beasley on 11/13/2008)
Rating
0vb3xdevx61lkabr
 
Sponsored Links