Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people, just like you, are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
Solved

setting multiple font properties

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

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Video Player 12 27
Where did System.Data.Objects go? 2 40
I need help with Crystal Report's Parameter Prompt Form 2 47
JQuery Detecting page post back 6 4
In order to hide the "ugly" records selectors (triangles) in the rowheaders, here are some suggestions. Microsoft doesn't have a direct method/property to do it. You can only hide the rowheader column. First solution, the easy way The first sol…
Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
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…

790 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