Drawing Text

Graphics Tutorials: For C# and VB NET Students

 

You can also draw text directly to a form, or to a form object like a picture box.

Drawing text is not as straightforward a process as you'd think. You need to set up a font object, a solid brush to draw the text, as well as setting up any optional string formats you may require.

Add another button to your form, and double click to get at the coding window. At the top of the coding window, along with all your other variables, add the following:

VB

Dim TextToDraw As String
Dim chosenFont As Font
Dim solidBrush As SolidBrush
Dim stringFormat As StringFormat

C#

String TextToDraw;
Font chosenFont;
SolidBrush solidBrush;
StringFormat stringFormat;

So we're setting up four new variables. The type of variables we've set up are String, Font, SolidBrush, and StringFormat.

In your button code, set some text to draw (add the semicolon at the end in C#):

TextToDraw = "Home and Learn"

Of course, you can change the text to anything you want. But the next thing to do is to specify the font you want to use. This is done by creating a new Font object:

VB

chosenFont = New Font("Arial", 20, FontStyle.Bold)

C#

chosenFont = new Font("Arial", 20, FontStyle.Bold);

Note the arguments, here. The first one is for the font. If you are using a font that you have on your system, then just enclose it in quotation marks, just like a string. The second argument, 20, is the size you want your font. The third argument sets the style of the font, Bold in our example. If you don't want a style, just have two arguments instead of three.

The brush for the font is easy enough to set up. Add this line to your button code:

VB

solidBrush = New SolidBrush( Color.Black )

C#

solidBrush = new SolidBrush( Color.Black );

So you just create a New SolidBrush, and then specify a colour for it between the round brackets.

The string format class allows you to set up things like vertical text:

VB

stringFormat = New StringFormat( StringFormatFlags.DirectionVertical )

C#

stringFormat = new StringFormat( StringFormatFlags.DirectionVertical );

In between the round brackets of StringFormat, you set up a string format flag. Type a dot and you'll see the available options:

Context menu showing the DirectionVertical option

We've gone for Direction Vertical.

Finally for the button code, add a new draw option and an Invalidate command (again, semicolons on the end in C#):

drawOptions = 5
Invalidate( )

Now, in your Paint event, add a Case 5 option to your Select Case or switch statement:

VB

Case 5

e.Graphics.DrawString( TextToDraw, chosenFont, solidBrush, 100, 10, stringFormat )

C#

case 5:

e.Graphics.DrawString( TextToDraw, chosenFont, solidBrush, 100, 10, stringFormat );
break;

We're now accessing the DrawString Subroutine of the Graphics object. In between the round brackets of DrawString, we first have the text we want to draw. After specifying a font and a brush, we have the numbers 100, 10. This is the X and Y coordinates for your string. In other words, where on your form you want to draw the string. The final argument we've specified is for the string format. This is optional, though, and you can miss it out. If you do, you get horizontal text.

Your code should now look like this in VB:

Visual Basic code showing how to draw text on a form

And here's the C# code:

C# code showing how to draw text on a form

Run your programme and test it out. Click your "Draw Text" button and your form should look something like ours:

Visual Studio user form with text drawn on it

 

And that's it for the section on Graphics. There's loads you can do the various shapes. So have fun and explore!

Back to the C# NET Contents Page

Back to the VB NET Contents Page