Article Options
Premium Sponsor
Premium Sponsor

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

Ged Mead (XTab) is a Microsoft Visual Basic MVP who has been working on computer software and design for more than 25 years. His journey has taken him through many different facets of IT. These include training as a Systems Analyst, working in a mainframe software development environment, creating financial management systems and a short time spent on military laptop systems in the days when it took two strong men to carry a 'mobile' system.

Based in an idyllic lochside location in the West of Scotland, he is currently involved in an ever-widening range of VB.NET, WPF and Silverlight development projects. Now working in a consultancy environment, his passion however still remains helping students and professional developers to take advantage of the ever increasing range of sophisticated tools available to them.

Ged is a regular contributor to forums on vbCity and authors articles for DevCity. He is a moderator on VBCity and the MSDN Tech Forums and spends a lot of time answering technical questions there and in several other VB forum sites. Senior Editor for DevCity.NET, vbCity Developer Community Leader and Admin, and DevCity.NET Newsletter Editor. He has written and continues to tutor a number of free online courses for VB.NET developers.

 

View all articles by Ged Mead...
Vertical Text: Top to Bottom

Problem:

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

 

Solution

   Draw the string on the label using the Graphics DrawString method.    Use the FormatFlags Property of the System.Drawing.StringFormat Class, selecting DirectionVertical from the StringFormatFlags Enumeration.

Code

   If that sounds technical, it’s only because there are several steps involved, but it isn’t really as complicated as it might seem.     Let’s start with the code, then we can break it down into understandable steps.

    Place a label named Label1 on a form.   Put this Imports statement at the top of the form:

Imports System.Drawing.Text

   And this code in the Label’s Paint event:

Private Sub Label1_Paint(ByVal sender As Object, ByVal e As System.Windows.Forms.PaintEventArgs) Handles Label1.Paint
        Dim strText As String = "Top to Bottom"
        Dim lbl1gfx As Graphics = e.Graphics
        Dim fnt As Font = New Font("Verdana", 12, FontStyle.Regular)
        Dim SF As New StringFormat
        SF.FormatFlags = StringFormatFlags.DirectionVertical
        lbl1gfx.Clear(Color.White)
        lbl1gfx.TextRenderingHint = _
          TextRenderingHint.AntiAlias
        lbl1gfx.DrawString(strText, fnt, Brushes.Black, 12, 12, SF)
    End Sub

Run the Project and your string will be displayed vertically.

If you want a step by step breakdown of this code, check out 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