In Windows Forms, I have a textbox which I want user to set its font style.
Something like:
Font font = new Font(textBox1.Font,FontStyle.Regular);
if (checkBox1.Checked == true)
font= new Font(font,FontStyle.Bold);
if (checkBox2.Checked == true)
font = new Font(font, FontStyle.Italic);
if (checkBox3.Checked == true)
font = new Font(font, FontStyle.Underline);
textBox1.Font = font;
The thing is if two of checkboxes are selected I would have to do like:
font = new Font(font, FontStyle.Italic|FontStyle.Italic);
Then check all possible combinations. Is there a way to define a font then add properties to its style? instead of checking all possible if combinations.
something like:
Font font= new Font();
if (checkBox1.Checked == true)
font.Bold=true;
if (checkBox2.Checked == true)
font.Italic=true;
if (checkBox3.Checked == true)
font.Underline=true;
Fonts are immutable, so you can't change a font once it's created.
What you can do is have a variable to hold the font style, and do something like this:
var fontStyle = FontStyle.Regular;
if (checkBox1.Checked)
{fontStyle |= FontStyle.Bold;}
if (checkBox2.Checked)
{fontStyle |= FontStyle.Italic;}
if (checkBox3.Checked)
{fontStyle |= FontStyle.Underline;}
textBox1.Font = new Font(textBox1.Font, fontStyle);