Make font italic and bold

后端 未结 5 720
借酒劲吻你
借酒劲吻你 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: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.

提交回复
热议问题