Home and Learn: Intermediate Programming


OK and Cancel Buttons

Stick Figure App - Custom Font Dialog Box

 

We'll do the OK and Canel buttons for our font dialog box. The OK buttons is where all the action takes place.

In Design View, double-click your OK button to create a code stub. The first thing we can do here is to check the isFontColorSet Boolean variable. If it's false then we can display a message that no font was set. We can then bail out. If it's true then our dialog box can do its stuff and return us to the main form.

So, add this If statement in VB Net:

If isFontColorSet = False Then

MessageBox.Show("No Font color set")
Exit Sub

End If

And add this if statement in C#:

if (isFontColorSet == false)
{

MessageBox.Show("No font color set");
return;

}

Next, we can examine which Radio Button was clicked, regular, bold, or italic. Add this If … ElseIf statement in VB Net:

If RadioRegular.Checked Then

style = FontStyle.Regular

ElseIf RadioBold.Checked Then

style = FontStyle.Bold

ElseIf RadioItalic.Checked Then

style = FontStyle.Italic

End If

And add this if … else if statement in C#:

if (RadioRegular.Checked)
{

style = FontStyle.Regular;

}
else if (RadioBold.Checked)
{

style = FontStyle.Bold;

}
else if (RadioItalic.Checked)
{

style = FontStyle.Italic;

}

We're setting a value for the style variable here, depending on which Radio Button was checked.

Now we want to set up a font object. This can take the font family, the font size, and the style between its round brackets. We can then return this font object to the main form.

Add this line in VB Net:

Dim fontObject As New Font(fontFamily, fontSize, style)

And add this line in C#:

Font fontObject = new Font(fontFamily, (float)fontSize, style);

If you remember those Shared or static variable we set up in the main form, they can come into play now. We set up the following three variables in Form

theFont
theText
theFontColor

You can access these variables if you say Form1 first. Add these lines in VB Net:

Form1.theFont = fontObject
Form1.theText = TextboxStickFigure.Text
Form1.theFontColor = fontColor

And add these lines in C#:

Form1.theFont = fontObject;
Form1.theText = TextboxStickFigure.Text;
Form1.theFontColor = fontColor;

We're passing the fontObject to theFont variable we set up on Form1. The variable called theText on Form1 is handed the text from the text box. The variable called theFontColor on Form1 is handed the color that was chosen and placed in the fontColor variable.

The final line for the OK button is this (semicolon on the end in C#):

DialogResult = DialogResult.OK

Setting DialogResult to DialogResult.OK is all you need to close a dialog box. It will then return a value of 1 to the main form where we called it from.

Go back to Design View. Double-click your Cancel button to create a code stub. Now add this line (with semicolon on the end in C#):

DialogResult = DialogResult.Cancel

The code for your OK and Cancel buttons should look like this in VB Net:

VB Net code for a custom font dialog box

And this in C#:

C# code for a custom font dialog box

Now that we've handed the main form the text and font information, we can finish off the code MouseDown event. We'll do that in the next lesson

Adding the Text to the Picture Box >>

<< Back to the Intermediate Contents Page


Email us: enquiry at homeandlearn.co.uk