I have the following code, and I’m wondering if there’s a more succinct, reduced way to write this code:
(FontStyle is a .NET Enum with the Flags attribute)
lblPrompt.Font.Style = FontStyle.Regular;
if (chkBold.Checked)
lblPrompt.Font.Style |= FontStyle.Bold;
if (chkItalics.Checked)
lblPrompt.Font.Style |= FontStyle.Italic;
if (chkUnderline.Checked)
lblPrompt.Font.Style |= FontStyle.Underline;
I have a feeling the answer lies in correctly applying the and & operator between CheckBox.Checked and the desired flag, similar to the following:
lblPrompt.Font.Style =
(chkBold.Checked & FontStyle.Bold)
| (chkItalics.Checked & FontStyle.Italic)
| (chkUnderline.Checked & FontStyle.Underline);
This does not work however because the compiler apparently does not like my direct application of the ampersand with a bool and a Flag/Enum type.
How about something along these lines: