【问题标题】:Properly change Font Style simultaneously同时正确更改字体样式
【发布时间】:2018-10-30 14:42:12
【问题描述】:

在选中多个复选框时,如何在不使用大量 if / else if 条件的情况下正确更改文本的字体样式?

PS。我知道在一个文本中有多种样式时应该使用什么,但我不希望使用很长的 if/else 条件来实现它。

这就是我所拥有的:

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

【问题讨论】:

  • 有一个 Update() 方法,您只需从每个复选框的相关事件中调用它吗?
  • 如果你有很多 if/else,switch/cases 可能会更好。特别是模式匹配可能是一个有价值的补充。
  • 如果您正在使用 Windows 窗体或其他内容,请添加
  • 4 个条件几乎没有“很多”
  • @untethered 您需要通过editing 您的问题并添加相关标签来回答这是 WinForms、WPF、Asp.net 还是其他任何问题。它可能会通过显示您拥有的代码的相关位来帮助上述评论者。

标签: c# winforms checkbox font-style


【解决方案1】:
  • 将相关的FontStyle 分配给每个CheckBox.Tag 属性(在Form 构造函数或Load 事件中)。

  • 将单个事件处理程序分配给所有 CheckBoxes CheckedChange 事件(在设计器中设置,在这里;当然你也可以在构造函数中添加它)。

  • FontStyle 是一个标志。您可以使用| 添加它并使用&~ 删除它。

如果需要,您可以添加一个条件以相互排除下划线和删除线样式。


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


【讨论】:

    【解决方案2】:

    正如 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);
    }
    

    并将CheckedChanged 事件处理程序分配给每个复选框。

    foreach (Control control in Controls)
          if (control is CheckBox checkBox)
             checkBox.CheckedChanged += (s, e) => UpdateTextBoxFontStyle();
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2012-01-30
      • 1970-01-01
      • 2016-09-07
      • 2012-10-20
      • 2011-12-02
      • 2015-11-01
      • 1970-01-01
      相关资源
      最近更新 更多