开发者

Change a font programmatically

开发者 https://www.devze.com 2023-01-05 12:13 出处:网络
C# doesn\'t like the following code: private void b开发者_运维技巧tnSizeRandom_Click(object sender, EventArgs e)

C# doesn't like the following code:

private void b开发者_运维技巧tnSizeRandom_Click(object sender, EventArgs e)
{
  btnSizeRandom.Font.Bold = true;
  btnother.Font.Bold = false;
}

Is there a way to do this programatically?


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.


    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);
    }
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号