[英]Properly change Font Style simultaneously
How do I properly change the Font Style of the text without using a lot of if / else if conditions when checking multiple checkboxes? 在检查多个复选框时,如何在不使用大量if / else if条件的情况下正确更改文本的字体样式?
PS. PS。 I know what to use when having multiple styles in one text but I d not want a long if/else conditions to achieve it.
我知道在一个文本中有多个样式时要使用什么,但我不想要一个很长的if / else条件来实现它。
Here's what I Have: 这就是我所拥有的:
public void updateFont()
{
//Individual
if (checkBox_Bold.Checked)
fontStyle = fontFamily.Style | FontStyle.Bold;
if (checkBox_Italic.Checked)
fontStyle = fontFamily.Style | FontStyle.Italic;
if (checkBox_Underlined.Checked)
fontStyle = fontFamily.Style | FontStyle.Underline;
if (checkBox_StrikeOut.Checked)
fontStyle = fontFamily.Style | FontStyle.Strikeout;
if (!checkBox_Bold.Checked && !checkBox_Italic.Checked && !checkBox_Underlined.Checked && !checkBox_StrikeOut.Checked)
fontStyle = FontStyle.Regular;
fontFamily = new Font(cbox_FontFamily.SelectedItem.ToString(), Convert.ToInt32(fontSize), fontStyle);
pictureBox_Canvass.Invalidate();
}
Assign the related FontStyle
to each CheckBox.Tag
property (in the Form
constructor, or Load
event). 将相关的
FontStyle
分配给每个CheckBox.Tag
属性(在Form
构造函数或Load
事件中)。
Assign a single Event handler to all CheckBoxes
CheckedChange
event (it's set in the designer, here; but it of course you can add it in the constructor as well). 为所有
CheckBoxes
CheckedChange
事件分配一个事件处理程序(它在设计器中设置,此处;但它当然也可以在构造函数中添加它)。
FontStyle
is a flag. FontStyle
是一面旗帜。 You can use |
你可以用
|
to add it and &~
to remove it. 添加它和
&~
删除它。
You could add a condition to mutually excludes the Underline and Strikeout styles, if required. 如果需要,您可以添加条件以相互排除下划线和删除线样式。
FontStyle fontStyle = FontStyle.Regular;
public form1()
{
InitializeComponent();
this.chkBold.Tag = FontStyle.Bold;
this.chkItalic.Tag = FontStyle.Italic;
this.chkUnderline.Tag = FontStyle.Underline;
this.chkStrikeout.Tag = FontStyle.Strikeout;
}
private void chkFontStyle_CheckedChanged(object sender, EventArgs e)
{
CheckBox checkBox = sender as CheckBox;
FontStyle CurrentFontStyle = (FontStyle)checkBox.Tag;
fontStyle = checkBox.Checked ? fontStyle | CurrentFontStyle : fontStyle &~CurrentFontStyle;
lblTestFont.Font = new Font("Segoe UI", 10, fontStyle, GraphicsUnit.Point);
}
As Jimi already mentioned, but here you can also achieve your goal using LINQ. 正如Jimi已经提到的,但在这里你也可以使用LINQ实现你的目标。
private void UpdateTextBoxFontStyle()
{
var fs = System.Drawing.FontStyle.Regular;
var checkedStyles = Controls.OfType<CheckBox>()
.Where(x => x.Checked)
.Where(x => x.Tag is System.Drawing.FontStyle)
.Select(x => (System.Drawing.FontStyle) x.Tag).ToList();
foreach (var style in checkedStyles) fs |= style;
lblTestFont.Font = new System.Drawing.Font("Segoe UI", 9f, fs, System.Drawing.GraphicsUnit.Point);
}
And assign CheckedChanged
event handler to each checkboxes. 并为每个复选框分配
CheckedChanged
事件处理程序。
foreach (Control control in Controls)
if (control is CheckBox checkBox)
checkBox.CheckedChanged += (s, e) => UpdateTextBoxFontStyle();
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.