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.

Sponsored Links