Make font italic and bold

后端 未结 5 701
借酒劲吻你
借酒劲吻你 2020-12-05 22:59

How do you apply multiple font styles to text?

System.Drawing.Font MyFont = new System.Drawing.Font(
    thisTempLabel.LabelFont,
    ((float)thisTempLabel.f         


        
相关标签:
5条回答
  • 2020-12-05 23:40

    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

    0 讨论(0)
  • 2020-12-05 23:42

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

    FontStyle.Bold | FontStyle.Italic
    
    0 讨论(0)
  • 2020-12-05 23:51
    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
    );
    

    Maybe you wanted to use the OR operator (|)

    0 讨论(0)
  • 2020-12-05 23:56

    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.

    0 讨论(0)
  • 2020-12-05 23:57

    I think you could benefit from a Font class:

    /*controlName*/.SelectionFont=new Font(maintext.Font, FontStyle.Italic);
    
    0 讨论(0)
提交回复
热议问题