Solved

setting multiple font properties

Posted on 2006-10-20
2
379 Views
Last Modified: 2013-12-03
Hi,

I want to create a new Font object based on the user specified options:

private void SetFontProperties ( string fontName, float fontSize, bool boldStyle, bool italicStyle, bool underlineStyle )
{
    Font newFont = new Font ( fontName, fontSize, ???  )


}

How do I specify the multiple options for the style when creating the font object?
0
Comment
Question by:jumanac14
  • 2
2 Comments
 
LVL 18

Accepted Solution

by:
Ravi Singh earned 500 total points
ID: 17773778
Hi, it would be easier to just accept a FontStyle object as a formal paramater to your method:

private void SetFontProperties (string fontName, float fontSize, FontStyle fontStyle)
{
      Font newFont = new Font (fontName, fontSize, fontStyle);
}

To set the various types of style you have to use the bitwise OR "|" operator, e.g. if you want your font to be bold and italic use:

SetFontProperties("Verdana", 12.0f, FontStyle.Bold | FontStyle.Italic);

or this will set it to bold, italic and underlined:

SetFontProperties("Verdana", 12.0f, FontStyle.Bold | FontStyle.Italic | FontStyle.Underline);

If you want to use your current approach (with the bools) then I think you'll have to use a series of IF statements to initialise the Font object based on the styles specified.
0
 
LVL 18

Expert Comment

by:Ravi Singh
ID: 17773855
Using your current approach, you can accomplish it like:

private void SetFontProperties ( string fontName, float fontSize, bool boldStyle, bool italicStyle, bool underlineStyle)
{
      int style = (int) FontStyle.Regular;

      if (boldStyle)
            style |= (int) FontStyle.Bold;
      if (italicStyle)
            style |= (int) FontStyle.Italic;
      if (underlineStyle)
            style |= (int) FontStyle.Underline;

      Font newFont = new Font ( fontName, fontSize, (FontStyle) style);
}
0

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article introduced a TextBox that supports transparent background.   Introduction TextBox is the most widely used control component in GUI design. Most GUI controls do not support transparent background and more or less do not have the…
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…
A short tutorial showing how to set up an email signature in Outlook on the Web (previously known as OWA). For free email signatures designs, visit https://www.mail-signatures.com/articles/signature-templates/?sts=6651 If you want to manage em…

713 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question