0

hi I want to change the text font of text in rich text box to bold, italic, underline. I have the following code. But if i click another check box,the first check box change disappear. May I know how to fix it

    private void checkBox1_CheckedChanged(object sender, EventArgs e)
    {


            richTextBox1.Font = new Font(richTextBox1.Font, FontStyle.Bold);


    }

    private void checkBox2_CheckedChanged(object sender, EventArgs e)
    {
         richTextBox1.Font = new Font(richTextBox1.Font, FontStyle.Italic);
    }

    private void checkBox3_CheckedChanged(object sender, EventArgs e)
    {
        richTextBox1.Font = new Font(richTextBox1.Font, FontStyle.Underline);
    }

    private void checkBox4_CheckedChanged(object sender, EventArgs e)
    {
        richTextBox1.Font = new Font(richTextBox1.Font, FontStyle.Regular);
    }
avariant
  • 2,234
  • 5
  • 25
  • 33
Ema_wat
  • 33
  • 2
  • 11

1 Answers1

0

You can try to call a method like this from the checkBox_CheckedChanged

private void UpdateFont()
{
    System.Drawing.FontStyle style = System.Drawing.FontStyle.Regular;
    if (checkBox1.Checked) style |= System.Drawing.FontStyle.Bold;
    if (checkBox2.Checked) style |= System.Drawing.FontStyle.Italic;
    if (checkBox3.Checked) style |= System.Drawing.FontStyle.Underline;
    textBox1.Font = new Font(textBox1.Font, style);
}
Matteo
  • 2,029
  • 4
  • 22
  • 30