Change a font programmatically
Asked Answered
A

2

7

C# doesn't like the following code:

private void btnSizeRandom_Click(object sender, EventArgs e)
{
  btnSizeRandom.Font.Bold = true;
  btnother.Font.Bold = false;
}

Is there a way to do this programatically?

Agora answered 30/6, 2010 at 20:36 Comment(0)
M
18

Instances of Font are immutable. You need to construct a new Font and assign it to the Font property. The Font class has various constructors for this purpose; they copy another instance and change the style in the process.

Myriagram answered 30/6, 2010 at 20:38 Comment(2)
+1 And just to round out the great answer: btnSizeRandom.Font = new Font(btnSizeRandom.Font, FontStyle.Bold);Denn
@Denn Additionally, you need: new system.Drawing.Font(btnSizeRandom.Font, FontStyle.Regular);Mellissamellitz
P
11
    private static Font ChangeBoldStyle(Font org, bool bold) {
        FontStyle style = org.Style;
        if (bold) style |= FontStyle.Bold;
        else style &= ~FontStyle.Bold;
        return new Font(org, style);
    }
Pleuro answered 30/6, 2010 at 21:22 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.