Emory Lu
Emory Lu

Reputation: 77

Dynamically Change Font Size of All Controls in One WinForm Use C#

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!

Upvotes: 1

Views: 3304

Answers (2)

Jingmiao Xu-MSFT
Jingmiao Xu-MSFT

Reputation: 2299

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

Upvotes: 2

Emory Lu
Emory Lu

Reputation: 77

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

Upvotes: 3

Related Questions