How do you apply multiple font styles to text?
System.Drawing.Font MyFont = new System.Drawing.Font(
thisTempLabel.LabelFont,
((float)thisTempLabel.f
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
FontStyle
is a flag enum and therefore you can set multiple styles by:
FontStyle.Bold | FontStyle.Italic
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 (|
)
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.
I think you could benefit from a Font class:
/*controlName*/.SelectionFont=new Font(maintext.Font, FontStyle.Italic);