Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

setting multiple font properties

Posted on 2006-10-20
2
Medium Priority
?
383 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 2000 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

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

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

Introduction Although it is an old technology, serial ports are still being used by many hardware manufacturers. If you develop applications in C#, Microsoft .NET framework has SerialPort class to communicate with the serial ports.  I needed to…
Summary: Persistence is the capability of an application to store the state of objects and recover it when necessary. This article compares the two common types of serialization in aspects of data access, readability, and runtime cost. A ready-to…
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…
If you’ve ever visited a web page and noticed a cool font that you really liked the look of, but couldn’t figure out which font it was so that you could use it for your own work, then this video is for you! In this Micro Tutorial, you'll learn yo…
Suggested Courses

773 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