Home and Learn: Intermediate Programming


Font Dialog Form

Stick Figure App - Custom Font Dialog Box

 

What we want to do is to add some text to the drawing. To do that, we'll use a right-click. When a user right-clicks the white drawing surface, we'll display a message box asking if they want to add text at this point (the X and Y points where they right-clicked). If they do, we'll then launch a second form. This form will allow a user to enter some text and set some font properties. It will have OK and Cancel buttons. Clicking OK will close the second form and add the text to the drawing surface. The form will look like this:

A custom font dialog box form in Visual Studio

Let's design this second form.

 

Adding a Second Form to the Project

To add a second form to your project, Select Project > Add Windows Form from the menu bars at the top of Visual Studio. Call it FormText. When you see your new form appear in the Design window, set a few properties for it. Set the following:

MaximizeBox: False
MinimizeBox: False
Size>Width: 400
Size>Height: 490
StartPosition: CenterScreen
Text: Set Text for Drawing
Icon: Select the same icon as you did for the main form

Now place a text box at the top of your form. Set the following properties for it:

Name: TextboxStickFigure
Font: 14 point
MultiLine: True
Size>Width: 315
Size>Height: 75
Text: Default Text

Now add three Group Boxes (in the Containers area of the Toolbox). Make them as wide as the text box at the top. Change the text properties to the following:

Text: Font Family and Size
Size>Width: 315
Size>Height: 70

Text: Font Style
Size>Width: 315
Size>Height: 70

Text: Font Color
Size>Width: 315
Size>Height: 90

Position them one after the other.

On your Font Family group box, add a Combo Box and a NumericUpDown control. The NumericUpDown control is the Common Controls category of the Toolbox. Set the following properties for the Combo Box:

Name: CmbFontFamily
Font: 12 points
Size>Width: 225
Size>Height: 28
Text: Arial

And set the following properties for the NumericUpDown control:

Name: SpinFontSize
Font: 14 points
Minimum: 8
Maximum: 36
Size>Width: 55
Size>Height: 29
Value: 14

The NumericUpDown control allows you to control the lowest and highest values. You don't want the font size to be too big or too small, so a spinner is a good way to control the value.

In the Font Style Group Box, add three radio buttons. Change the following properties for them:

Name: RadioRegular
Checked: True
Text: Regular

Name: RadioBold
Text: Bold

Name: RadioItalic
Text: Italic

In the Font Color Group Box, add a button and a label. Change the properties of the button to these:

Name: BtnFontColor
Size>Width: 165
Size>Height: 35
Text: Font Color

For the label, change the name to LabelColor. Then do the same as you did before. Delete the text and then hit the spacebar on your keyboard twice. Change the font size to 24 points. Set the BorderStyle to FixedSingle.

Finally, add two buttons at the bottom of your Form. Set the first button to these properties:

Name: BtnOK
Size>Width: 140
Size>Height: 40
Text: OK

Set the properties of the second button to these:

Name: BtnCancel
Size>Width: 140
Size>Height: 40
Text: Cancel

And that's all we need. Once you've added all the controls, we can start the coding. First up is launching a form on a right-click

Launch a Second Form on a Right Mouse Click >>

<< Back to the Intermediate Contents Page


Email us: enquiry at homeandlearn.co.uk