Understand how and why a form resizes when its font resizes in C#

With an aging user population, you may want to allow users to increase a form's font size so they can see it more easily. If you change the form's font size, however, things won't fit any more.

To make this sort of thing easier, Microsoft gave the Form class an AutoScaleMode property. If this property is AutoScaleMode.Font, which it is by default, then the form resizes itself when its font changes size.

This example uses the following code to switch between font sizes when you check and uncheck the Bog Font box.

// Make the fonts.
private Font SmallFont;
private Font BigFont;
private void Form1_Load(object sender, EventArgs e)
{
    // Save the original font.
    SmallFont = this.Font;

    // Make the big font.
    BigFont = new Font("Times New Roman", 20);
}

// Change the form's font size.
private void chkBigFont_CheckedChanged(object sender, EventArgs e)
{
    if (chkBigFont.Checked)
    {
        this.Font = BigFont;
    }
    else
    {
        this.Font = SmallFont;
    }
}

When the form loads, the program saves the form's initial font in the variable SmallFont. It also creates a larger font and saves it in the variable BigFont.

When the Big Font checkbox is checked or unchecked, the program sets the form's font to either the big or little font. Because the form's AutoScaleMode property is set to AutoScaleMode.Font, the form automatically resizes itself.

Note that this isn't the only way you might want to handle larger fonts. For example, if the form is very large, then auto scaling might make it too big to fit on the screen.

In that case you might be better off not allowing the form to scale and displaying information at a larger font size inside some sort of scrolling control. For example, ListBoxes and RichTextBoxes can provide scrollbars as needed so they can display larger fonts even if the form doesn't resize.

   

 

What did you think of this article?




Trackbacks
  • No trackbacks exist for this post.
Comments

Leave a comment

Submitted comments are subject to moderation before being displayed.

 Name

 Email (will not be published)

 Website

Your comment is 0 characters limited to 3000 characters.