I apply a FontStyle Bold on a text selected with a button but I can't go back to the default value when I click on it again I would like that when I click the first time, the FontStyle Bold is applied and when I click again, the FontStyle Regular is applied. Could someone please help me?
private void button3_Click(object sender, EventArgs e)
{
System.Drawing.Font bold = richTextBox1.SelectionFont;
System.Drawing.FontStyle noBold;
if (richTextBox1.SelectionFont.Bold == true)
{
noBold = FontStyle.Regular;
}
else
{
noBold = FontStyle.Bold;
}
richTextBox1.SelectionFont = new Font(richTextBox1.Font.FontFamily, richTextBox1.Font.Size, noBold);
}
Your logic is faulty. You're checking if there is selected text and if there is, you make it bold, otherwise you make it not bold, except there is no selected text so nobold has no effect.
You want to do something more along the following lines (Note that this example code is self-contained. You should initialize your font objects elsewhere.):