Solved

setting multiple font properties

Posted on 2006-10-20
2
375 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

Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

Join & Write a Comment

Introduction Hi all and welcome to my first article on Experts Exchange. A while ago, someone asked me if i could do some tutorials on object oriented programming. I decided to do them on C#. Now you may ask me, why's that? Well, one of the re…
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…
Illustrator's Shape Builder tool will let you combine shapes visually and interactively. This video shows the Mac version, but the tool works the same way in Windows. To follow along with this video, you can draw your own shapes or download the file…

760 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

Need Help in Real-Time?

Connect with top rated Experts

23 Experts available now in Live!

Get 1:1 Help Now