Remove FontStyle Bold from a Control's Font
Asked Answered
H

3

5

I feel like a real noob posting this, but I can't seem to find anything for this...

I have a control that I'm basically trying to toggle the fontstyle between bold and not bold. This should be simple...

However, you can't acccess the Control.Font.Bold property as it is read only, therefore, you need to change the Font property.

To make it bold, I just do this:

this.btn_buttonBolding.Font = new Font(this.btn_buttonBolding.Font, FontStyle.Bold);

Not ideal, but it works. However, how do I go about removing this bold style (once it is bold already)?

I looked hard for duplicates; closest I could find was this, but it doesn't quite answer my situation: Substract Flag From FontStyle (Toggling FontStyles) [C#]

And this which gives how to set it, but not remove it: Change a font programmatically

Am I missing a simple constructor for the font that could do this? Or am I just missing something easier?

Herrington answered 19/3, 2012 at 20:58 Comment(3)
FontStyle.Regular doesn't works?Chuch
LOL @Chuch Thanks. Amazing how I miss these things sometimes. Make this an answer and its yours.Herrington
Possible duplicate of Substract Flag From FontStyle (Toggling FontStyles) [C#]Trucking
C
4

The FontStyle enum contains 5 distinct values. The one that reset your previous set is FontStyle.Regular

Regular Normal text.
Bold Bold text.
Italic Italic text.
Underline Underlined text.
Strikeout Text with a line through the middle.

It's a bitwise enum where Regular is 0. So setting this value alone reset all other flags

Chuch answered 19/3, 2012 at 21:8 Comment(1)
This does answer the example in the question, but not the question: other than bit masking if another style is set, such as FontStyle.Italic, is there a way to remove the style without complete reversion to FontStyle.Regular?Gastrointestinal
S
13

I know this is a bit old, but I was faced with the exact same problem and came up with this:

Font opFont = this.btn_buttonBolding.Font;
if(value)
{
    this.btn_buttonBolding.Font = new Font(opFont, opFont.Style | FontStyle.Bold);
}
else 
{
    this.btn_buttonBolding.Font = new Font(opFont, opFont.Style & ~FontStyle.Bold);
}

The magic is in the "~" which is the bitwise NOT. (See the MSDN KB article "~Operator")

VB.NET version:

Dim opFont As Font = me.btn_buttonBolding.Font
If (value)
    me.btn_buttonBolding.Font = new Font(opFont, opFont.Style Or FontStyle.Bold)
Else 
    me.btn_buttonBolding.Font = new Font(opFont, opFont.Style And Not FontStyle.Bold)
End if
Sphingosine answered 17/12, 2013 at 18:7 Comment(0)
C
4

The FontStyle enum contains 5 distinct values. The one that reset your previous set is FontStyle.Regular

Regular Normal text.
Bold Bold text.
Italic Italic text.
Underline Underlined text.
Strikeout Text with a line through the middle.

It's a bitwise enum where Regular is 0. So setting this value alone reset all other flags

Chuch answered 19/3, 2012 at 21:8 Comment(1)
This does answer the example in the question, but not the question: other than bit masking if another style is set, such as FontStyle.Italic, is there a way to remove the style without complete reversion to FontStyle.Regular?Gastrointestinal
R
2

Try this:

    private void btn_buttonBolding_Click(object sender, EventArgs e)
    {
        var style = btn_buttonBolding.Font.Bold ? FontStyle.Regular : FontStyle.Bold;
        btn_buttonBolding.Font = new Font(this.Font, style);
    }
Russon answered 19/3, 2012 at 21:3 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.