Tom*_*len 1 c# fonts if-statement operators
鉴于:
bool isBold = true;
bool isItalic = true;
bool isStrikeout = false;
bool isUnderline = true;
System.Drawing.Font MyFont = new System.Drawing.Font(
thisTempLabel.LabelFont,
((float)thisTempLabel.fontSize),
FontStyle.Bold | FontStyle.Italic | FontStyle.Strikeout | FontStyle.Underline,
GraphicsUnit.Pixel
);
Run Code Online (Sandbox Code Playgroud)
我如何应用布尔值来确定我应该使用哪些字体样式?上面的代码使它们全部适用,所以它是粗体,斜体,删除线和下划线,但我想基于bools进行过滤.
好吧,你可以这样做:
FontStyle style = 0; // No styles
if (isBold)
{
style |= FontStyle.Bold;
}
if (isItalic)
{
style |= FontStyle.Italic;
}
// etc
Run Code Online (Sandbox Code Playgroud)
你可以使用:
FontStyle style = 0 | (isBold ? FontStyle.Bold : 0)
| (isItalic ? FontStyle.Italic : 0)
etc
Run Code Online (Sandbox Code Playgroud)
但我不确定我是否愿意.这有点"狡猾".注意,这两个代码都利用了常量0可以隐式转换为任何枚举类型的事实.
| 归档时间: |
|
| 查看次数: |
236 次 |
| 最近记录: |