1

I am very new to C#, and currently, I am required to add a dynamic font size changing feature to a Windows Form, where there are two buttons, one for increasing font size, the other one for decreasing font size.

The closest solution I can find for calling all controls is this post, How to get ALL child controls of a Windows Forms form of a specific type (Button/Textbox)?, but this post is about getting a specific type of control, and I hope to call all controls and change font size no matter what type they are.

Is this thought even feasible? Any thought would be super helpful. Thank you in advance!

Emory Lu
  • 77
  • 1
  • 9
  • 1
    You usually add Controls to a Container, let the child Controls inherit the Font from the Parent (which is the default behavior), then just change the Font of the Parent Container. If you need to change the Font of some of the Controls, when possible group these Controls in specific containers (think about the MenuItems, for example). – Jimi Apr 19 '22 at 13:47

2 Answers2

3

I figured out it later that I can just use this.Controls to refer to all controls within the same winForm

Emory Lu
  • 77
  • 1
  • 9
2

If you want to change font size by click button, you can refer to the following code:

private void increaseBtn_Click(object sender, EventArgs e)
{
    foreach (Control c in this.Controls)
    {
        int size = (int)c.Font.Size;
        c.Font = new Font("Microsoft Sans Serif", ++size);
    }
}
private void disincreaseBtn_Click(object sender, EventArgs e)
{
    foreach (Control c in this.Controls)
    {
        int size = (int)c.Font.Size;
        c.Font = new Font("Microsoft Sans Serif", --size);
    }
}

Here is the test result: enter image description here

Jingmiao Xu-MSFT
  • 2,076
  • 1
  • 3
  • 10