Solved

setting multiple font properties

Posted on 2006-10-20
2
380 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
[X]
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
  • 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

MS Dynamics Made Instantly Simpler

Make Your Microsoft Dynamics Investment Count  & Drastically Decrease Training Time by Providing Intuitive Step-By-Step WalkThru Tutorials.

Question has a verified solution.

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

Suggested Solutions

Extention Methods in C# 3.0 by Ivo Stoykov C# 3.0 offers extension methods. They allow extending existing classes without changing the class's source code or relying on inheritance. These are static methods invoked as instance method. This…
This article describes a simple method to resize a control at runtime.  It includes ready-to-use source code and a complete sample demonstration application.  We'll also talk about C# Extension Methods. Introduction In one of my applications…
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…
Attackers love to prey on accounts that have privileges. Reducing privileged accounts and protecting privileged accounts therefore is paramount. Users, groups, and service accounts need to be protected to help protect the entire Active Directory …
Suggested Courses

734 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