rawing text in Visual Basic 6 was all very simpleboth the process and the results. First, you would set a form or PictureBox's CurrentX
properties and then call the Print
In contrast, .NET provides more powerful tools for drawing text. As is so often the case however, the extra power comes with extra complexity. While you can do amazing things with text in .NET, there's a lot more to learn. Still, you can attack the complexity in easily-digestible chunks, beginning with simple text display, then gradually adding new "tricks" such as changing fonts and alignment, clipping, wrapping, shading, and transforming text, until you're thoroughly conversant with .NET's text-display features.
The sample code
for this article consists of a set of small applications that illustrate all these techniques. You can download the code and follow along, or use it as template or starter code for your own applications. Although I've used VB.NET for the examples in this article, the downloadable code provides both VB.NET and C# versions.
.NET Text Basics
Whenever you draw text in a .NET application, you need a Graphics object. This object provides the tools you need to draw lines, polygons, curves, images, and of course text. The DrawString
method draws text.
One of the really nice things about .NET graphics is that you use a Graphics object to draw on anything
, whether that's a form, PictureBox, owner-drawn menu, printer, or something even more exotic. That means you need to learn how to draw only once; after that, the process is similar for any other drawing operation on any surface.
The examples in this article draw during a form's Paint
event handler. That event handler's e.Graphics
parameter gives you the graphics object that you need to draw.
While .NET's text drawing tools can be complicated, drawing text isn't too hard in its simplest form. For example, you can place the following code inside a form's Paint
event handler. This code draws the text "BasicText" at the point (10, 10
) measured in pixels from the upper left corner of the form. The text is blue and is drawn in the form's current font. This code is in VB.NET but the downloadable sample code
includes both VB.NET and C# versions.
e.Graphics.DrawString("BasicText", Me.Font, Brushes.Blue, 10, 10)
This code is extremely easy to use. By changing the form's font at design time, you can draw text in whatever font you like. The only catch is that all of the text you draw will use the same font.
To work around that problem, the following slightly more complicated version builds its own font for just one piece of text. It starts with a Using
statement that creates a bold 24-point Comic Sans MS font. The Using
statement makes VB.NET call the font's Dispose
method automatically when it reaches the End Using
statement. After creating the font, the code calls DrawString
much as the previous code doe except this time it passes in the new font.
Using the_font As New Font("Comic Sans MS", 24, FontStyle.Bold)
e.Graphics.DrawString("BasicText", the_font, _
Brushes.Blue, 10, 20)
shows the results of these two calls to DrawString.
|Figure 1. Standard and Custom Fonts: The Graphics object's DrawString method draws text; you can use the form's font, or create a custom font.||
|Figure 2. Drawing Special Characters: DrawString starts a new line for carriage return/line feed combinations and adds horizontal space for tabs.||
handles special characters such as carriage returns and tabs well. Figure 2
shows the results of executing drawing code similar to the previous examples, but that draws text containing a carriage return/line feed pair and a tab. The VB.NET example draws the string, "The quick brown fox" & vbCrLf & "jumps over" & vbTab & "the lazy dog."
The C# version draws the string "The quick brown fox\njumps over\tthe lazy dog."