如何在Windows表单中更新现有字体



在Windows表单中,我有一个文本框,我希望用户设置其字体样式。

类似:

   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;

问题是,如果选择了两个复选框,我将要做:

font = new Font(font, FontStyle.Italic|FontStyle.Italic);

然后检查所有可能的组合。有没有办法定义字体,然后将属性添加到其样式中?而不是检查所有可能的组合。

类似:

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;

字体是不变的,因此创建字体后您将无法更改字体。
您可以做的是具有一个变量以保持字体样式,并执行类似的操作:

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);

最新更新