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.