我想根据用户的输入或数据库中保存的数据来更改控件的字体样式。我尝试了许多方法来在一句话中构建新的字体,但我不能。
最后,我写了这段代码
FontStyle fs = button1.Font.Style;
if (Bold.Checked == true)
fs |= FontStyle.Bold;
else
fs &= ~FontStyle.Bold;
if (Underline.Checked == true)
fs |= FontStyle.Underline;
else
fs &= ~FontStyle.Underline;
if (Italic.Checked == true)
fs |= FontStyle.Italic;
else
fs &= ~FontStyle.Italic;
if (Strikeout.Checked == true)
fs |= FontStyle.Strikeout;
else
fs &= ~FontStyle.Strikeout;
button1.Font = new Font("Tahoma", (float)27.75, fs);我想知道有没有一种聪明的方法来构造字体样式?
发布于 2017-05-10 23:11:43
是。不要从现有的FontStyle开始,而是使用一个Regular或一个属性(在我的示例中是Bold,当然,您可以使用任何属性作为第一个属性),然后简单地有条件地添加属性:
var fs = (Bold.Checked) ? FontStyle.Bold : FontStyle.Regular;
fs |= (Underline.Checked) ? FontStyle.Underline : FontStyle.Regular;
fs |= (Italic.Checked) ? FontStyle.Italic : FontStyle.Regular;
fs |= (Strikeout.Checked) ? FontStyle.Strikeout : FontStyle.Regular;
button1.Font = new Font("Tahoma", (float)27.75, fs);https://stackoverflow.com/questions/43896235
复制相似问题