Home and Learn: Intermediate Programming


Adding the Text to the Picture Box

Stick Figure App - Custom Font Dialog Box

 

We wrote the code for our custom font dialog box in the previous lesson. Whatever font attributes a user selected was handed to the main form. In this lesson, we'll use the font information we grabbed and use it to display some text on the Picture Box.

Go back to your MouseDown event for Form1. We have this if statement in VB Net:

If textForm.ShowDialog() = DialogResult.OK Then

End If

And this in C#:

if (textForm.ShowDialog() == DialogResult.OK)
{
}

Inside of this if statement, you can first copy and paste the two lines for your Undo code. These in VB Net:

UndoStack.Push(bitmapObject.Clone)
RedoStack.Clear()

And these in C#:

UndoStack.Push((Bitmap)bitmapObject.Clone())
RedoStack.Clear()

Next, we can set up a new Font object. Between the round brackets of this new font object, we can put our three variables. Here it is in VB Net:

Dim chosenFont As Font = New Font(theFont.FontFamily, theFont.Size, theFont.Style)

And here it is in C#:

Font chosenFont = new Font(theFont.FontFamily, theFont.Size, theFont.Style);

Notice that we can just say theFont.FontFamily, theFont.Size and theFont.Style. This is because theFont is a Shared or static variable at the top of the code. In our second form, we had this:

New Font(fontFamily, fontSize, style)

These values are now stored in the variable theFont, as a read only property. Which is why you can say theFont.FontFamily, etc.

Next, we need a brush to draw the text. We're going to be using the DrawString routine of the graphics object. The DrawString routine needs a Brush. Add this in VB Net:

Dim chosenBrush As SolidBrush = New SolidBrush(theFontColor)

And this in C#:

Brush chosenBrush = new SolidBrush(theFontColor);

This sets up a new SolidBrush object. In between the round brackets of SolidBrush, we can use the font color we got from the second form.

The next line adds anti-alias to the string we're about to draw. Add this line in VB Net:

graphicsObject.TextRenderingHint = Drawing.Text.TextRenderingHint.AntiAlias

And add this line in C#:

graphicsObject.TextRenderingHint = System.Drawing.Text.TextRenderingHint.AntiAlias;

If you didn't add the anti-alias, the text on your Picture Box will be all ragged and jaggy.

Now we can draw the string of text. Add this line next (with a semicolon on the end in C#):

graphicsObject.DrawString(theText, chosenFont, chosenBrush, rightClickX, rightClickY)

Our call to DrawString passes 5 arguments over:

theText, chosenFont, chosenBrush, rightClickX, rightClickY

The first one is the text you want to draw, which we got from the second form. Then we have our chosenFont with its font family, font size and font style. The last arguments are the position for the text. We got these from the right click.

Now call the Invalidate command (with a semicolon on the end in C#):

PbSurface.Invalidate()

And finally, dispose of your form object. Do this outside of the If Statement:

VB Net:

FormText.Dispose()

C#:

textForm.Dispose();

Here's what the whole of your MouseDown Event should look like in VB Net:

VB Net code to add text to a picture Box

And here it is in C#:

C# code to add text to a picture Box

You can try it out now. Run your program. Right-click on the Picture Box and the second form will pop up. Type some text. Choose a font and a font color. Click OK and you should see your text appear on the white Picture Box:

Winodws form showing a stick figure drawing program with text

Pretty good, huh?

The only thing left to do now is the Save Image button and the Clear Surface button. We'll write the code to save an image in the next lesson.

Save Images >>

<< Back to the Intermediate Contents Page


Email us: enquiry at homeandlearn.co.uk