2

enter image description here

How do I properly change the Font Style of the text without using a lot of if / else if conditions when checking multiple checkboxes?

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.

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();
    }
TerribleDog
  • 1,237
  • 1
  • 8
  • 31
  • 1
    Have an `Update()` method that you just call from the relevant event for each checkbox? – ProgrammingLlama Oct 30 '18 at 03:48
  • 2
    If you got a lot of if/else, switch/cases migth be better. In particular Pattern Matching might be a valuable addition. – Christopher Oct 30 '18 at 03:51
  • 2
    Please add if you are working with Windows Forms or what – Juan Salvador Portugal Oct 30 '18 at 04:08
  • 4 conditions is hardly _"a lot"_ –  Oct 30 '18 at 04:20
  • @MickyD It is if OP is repeating the same checks in each "Checked" event handler. – ProgrammingLlama Oct 30 '18 at 04:28
  • @John I'm just talking about 4 guard statements. Exactly what the OP is doing inside those checks is anyones' guess without code or technology. For all we know the entire `Font` needs to be re-created –  Oct 30 '18 at 04:37
  • Another question @John How do I "cancel" a font style if I uncheck one and another one is still checked? – TerribleDog Oct 30 '18 at 05:39
  • 1
    @untethered you need to answer the question of whether this is WinForms, WPF, Asp.net or whatever else, by [edit]ing your question and adding the relevant tag. It would probably help the above commenters by showing the *relevant* bits of the code that you have. – Richardissimo Oct 30 '18 at 06:03

2 Answers2

4
  • Assign the related FontStyle to each CheckBox.Tag property (in the Form constructor, or Load event).

  • 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).

  • FontStyle is a flag. 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);
 }


Set Font Style

Jimi
  • 29,621
  • 8
  • 43
  • 61
3

As Jimi already mentioned, but here you can also achieve your goal using 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.

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