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) has been around computers since the 1980's when the first affordable home computers came on the market. His journey from that very first Dragon 32 to the present has taken him through many different facets of the IT Industry. These include formal training as a Systems Analyst, employment in a mainframe software development environment, and a short time spent demonstrating rugged military IT systems in the days when it took two strong men to carry a 'mobile' system.

His most rewarding challenge was the creation of a financial management system for a large organisation.

Now based in an idyllic lochside location in the West of Scotland, he is currently involved in a range of development projects, whenever he can drag his gaze away from the stunning surrounding views, that is!

Ged is a Microsoft MVP, Senior Editor for DevCity.NET, vbCity Developer Community Leader and Admin, Helper of the Month competition winner and DevCity.NET newsletter Editor.

 

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#.
 
Sponsored Links