Setting multiple Fonstyle enums individually ... is it possible?

  • Thread starter Thread starter Harag
  • Start date Start date
H

Harag

I need to set multiple Fontsyle enums individually but so far I have found
that you can only set a Fontyle on creation of a new Font.

My one other option is to build a function to to select between all the
possible combinations of the Fonstyle enums and then create the Font but
that is ugly to say the least.

The oher option would be if the OR delimited list of Fontstyle enumscould be
passed as variable but I have no idea of what that type should be if at all
possible.

Any help or pointers in a direction would much appreciated.
 
Found the solution, I work with the int values of the enumerators doing the
OR's and then cast the answer back to FontStyle.

int FontStyleOR = 0 ;

if(chkControlFontBold.Checked)
{
FontStyleOR = (int)FontStyle.Bold;
}

if(chkControlFontItalic.Checked)
{
FontStyleOR = FontStyleOR | (int)FontStyle.Italic;
}

if(chkControlFontStrikeout.Checked)
{
FontStyleOR = FontStyleOR | (int)FontStyle.Strikeout;
}

if (chkControlFontUnderline.Checked)
{
FontStyleOR = FontStyleOR | (int)FontStyle.Underline;
}

Font x = new
Font(textControlFont.Text,(float)Convert.ToDouble(textControlFontSize.Text),
(FontStyle)FontStyleOR );
 
All this casting isn't necessary; you can work directly with the
enumerations. For example:

FontStyle fs = FontStyle.Regular;

fs = fs | FontStyle.Bold;

fs = fs | FontStyle.Italic;

Debug.WriteLine(fs.ToString() + " = " + ((int)fs).ToString());

This prints out: "Bold, Italic = 3"


Tom Dacon
Dacon Software Consulting
 
Back
Top