Tom Gullen
Tom Gullen

Reputation: 61773

Make font italic and bold

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!

Upvotes: 32

Views: 104785

Answers (5)

usr-local-ΕΨΗΕΛΩΝ
usr-local-ΕΨΗΕΛΩΝ

Reputation: 26914

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 (|)

Upvotes: 58

user3552161
user3552161

Reputation: 15

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.

Upvotes: 1

lost_in_the_source
lost_in_the_source

Reputation: 11237

I think you could benefit from a Font class:

/*controlName*/.SelectionFont=new Font(maintext.Font, FontStyle.Italic);

Upvotes: 2

Gordon Thompson
Gordon Thompson

Reputation: 4844

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

Upvotes: 5

anothershrubery
anothershrubery

Reputation: 21023

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

FontStyle.Bold | FontStyle.Italic

Upvotes: 11

Related Questions