32

How do you apply multiple font styles to text?

System.Drawing.Font MyFont = new System.Drawing.Font(
    thisTempLabel.LabelFont,
    ((float)thisTempLabel.fontSize),
    FontStyle.Bold + FontStyle.Italic,    // + obviously doesn't work, but what am I meant to do?
    GraphicsUnit.Pixel
);

Thanks for any help!

Raktim Biswas
  • 3,891
  • 5
  • 25
  • 31
Tom Gullen
  • 59,517
  • 82
  • 274
  • 446

5 Answers5

54
System.Drawing.Font MyFont = new System.Drawing.Font(
    thisTempLabel.LabelFont,
    ((float)thisTempLabel.fontSize),
    FontStyle.Bold | FontStyle.Italic,
    GraphicsUnit.Pixel
);

Maybe you wanted to use the OR operator (|)

Jay Brown
  • 89
  • 1
  • 13
usr-local-ΕΨΗΕΛΩΝ
  • 24,688
  • 29
  • 146
  • 274
  • 2
    That seems to work thanks! I don't understand the logic of it though? – Tom Gullen Mar 18 '11 at 10:31
  • 6
    @Tom - it's a "bitwise OR" operator. The underlying values of the FontStyles enum (that must have a `[Flags]` attribute) are designed to be combined in this way. – Hans Kesting Mar 18 '11 at 10:34
  • @Hans, thanks, I think I get it now, so | will evaluate all the conditions regardless of it any of them are true/false? – Tom Gullen Mar 18 '11 at 10:36
  • @Tom - that's the way it works for bools. For integers it combines the bits: 0011 | 0101 == 0111 (3|5==7) – Hans Kesting Mar 18 '11 at 10:40
  • 2
    Enums are basically integers. As Hans said, there are special enumerations flagged with [Flags] attribute so that the binary representation of each value is such that it has only one 1 (sorry for all word-games in my post...) and all 0s, and no other value has 1 in that position. So, bitwising ANDing and ORing keeps information about all the values chosen. When the back-end reads the value, it tests against the "activation" of a flag by ANDing it with the value to test (so if (value && FontStyle.italic) renderItalic(), something like that) – usr-local-ΕΨΗΕΛΩΝ Mar 18 '11 at 10:40
  • 2
    @dj @hans, ahhhh ok! So the enum for font style will be like 0000 and then each style is like 1000 0100 0010 0001 and we combine them all to make a 1111 for all to be set to true right? – Tom Gullen Mar 18 '11 at 10:41
  • @tom: exactly. 0000 is usually the "default" or "empty" value – usr-local-ΕΨΗΕΛΩΝ Mar 18 '11 at 11:31
10

FontStyle is a flag enum and therefore you can set multiple styles by:

FontStyle.Bold | FontStyle.Italic
anothershrubery
  • 19,639
  • 11
  • 51
  • 94
4

I think it's FontStyle.Bold | FontStyle.Italic

You generally use the pipe (bitwise OR) symbol to combine multiple flags in these functions

This page explains it

http://www.blackwasp.co.uk/CSharpLogicalBitwiseOps_2.aspx

Gordon Thompson
  • 4,574
  • 8
  • 46
  • 61
2

I think you could benefit from a Font class:

/*controlName*/.SelectionFont=new Font(maintext.Font, FontStyle.Italic);
Edward Karak
  • 10,658
  • 8
  • 44
  • 70
1

Hi I was writing a simple Text Editor and I had the same problem, I didn't find anything helpful on the internet. The if , else if method isn't optimal if there are many buttons in the form, so I thought why not take the existing font.style and just add to it using | symbol like people suggested above. I tested this code and it works. I call this method from pictureBox I click.

Update. I found a bug. when you deselect a font, it resets all others to regular too. But the code that combines them works.

private void ChangeFontStyle(PictureBox p)
        {
            if (p == pictureBox1)
            {
                if (BClicked)
                {
                    richTextBox1.SelectionFont = new Font(richTextBox1.Font, richTextBox1.Font.Style | FontStyle.Bold);
                }
                else 
                {
                    richTextBox1.SelectionFont = new Font(richTextBox1.Font, richTextBox1.Font.Style | FontStyle.Regular);
                }
            }
            else if (p == pictureBox2)
            {
                if (IClicked)
                {
                    richTextBox1.SelectionFont = new Font(richTextBox1.Font, richTextBox1.Font.Style | FontStyle.Italic);
                }
                else 
                {
                    richTextBox1.SelectionFont = new Font(richTextBox1.Font,  richTextBox1.Font.Style | FontStyle.Regular);
                }
            }
            else if (p == pictureBox3)
            {
                if (UClicked)
                {
                    richTextBox1.SelectionFont = new Font(richTextBox1.Font, richTextBox1.SelectionFont.Style | FontStyle.Underline);
                }
                else
                {
                    richTextBox1.SelectionFont = new Font(richTextBox1.Font, richTextBox1.Font.Style | FontStyle.Regular);
                }
            }
        }         

P.S I used picture boxes instead of buttons and boolean variables like BClicked indicate whether they are activated or not.