How to dynamically build font...?

T

Tim

Hi Guys,

I want to build a font like so:
headerFont = new Font("Times New Roman", 10, FontStyle.Underline |
FontStyle.Bold);

It is going to be built dynamically from the DB. How do I build the styles
dynamically. In the above example it has underline and bold, it could also
have italics, or none of them, or a different combination.

How do I build that in code? I guess it is the pipe character which is
throwing me off.

Thanks,

Tim
 
T

Tim

I think I found out the solution already. Didn't realize I could just set
headerFont.Bold = true.

Duh!

Tim
 
T

Tim

Okay - apparently, I can't just set headerFont.Bold = true as it is read
only, so that leaves me back where I started.

Help!

Tim
 
H

Hans Kesting

Tim said:
Hi Guys,

I want to build a font like so:
headerFont = new Font("Times New Roman", 10, FontStyle.Underline |
FontStyle.Bold);

It is going to be built dynamically from the DB. How do I build the
styles dynamically. In the above example it has underline and bold,
it could also have italics, or none of them, or a different
combination.
How do I build that in code? I guess it is the pipe character which is
throwing me off.

Thanks,

Tim

Fisrt build the FontStyle in a separate variable, *then* use it in the Font constructor.

FontStyle fs = FontStyle.None; // I'm guessing here
if (useBold)
fs = fs | FontStyle.Bold; // or shorter: fs |= FontStyle.Bold;
if (useUnderline)
fs = fs | FontStyle.Underline;
// etc.

headerFont = new Font("Times New Roman", 10, fs);


Hans Kesting
 
N

Nicholas Paldino [.NET/C# MVP]

Tim,

Use the FontConverter class in the System.Drawing namespace. It will
convert a font to a string representation, and back to a Font again, with
the appropriate point size, styles, etc, etc. The output of FontConverter
would be what you store in your database (or construct from your database,
but its easier to just store the value it outputs).

Hope this helps.
 
T

Tim

Thank you Nicholas, I don't suppose you have an example do you?

Tim

Nicholas Paldino said:
Tim,

Use the FontConverter class in the System.Drawing namespace. It will
convert a font to a string representation, and back to a Font again, with
the appropriate point size, styles, etc, etc. The output of FontConverter
would be what you store in your database (or construct from your database,
but its easier to just store the value it outputs).

Hope this helps.


--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)

Tim said:
Hi Guys,

I want to build a font like so:
headerFont = new Font("Times New Roman", 10, FontStyle.Underline |
FontStyle.Bold);

It is going to be built dynamically from the DB. How do I build the
styles dynamically. In the above example it has underline and bold, it
could also have italics, or none of them, or a different combination.

How do I build that in code? I guess it is the pipe character which is
throwing me off.

Thanks,

Tim
 
N

Nicholas Paldino [.NET/C# MVP]

Tim,

This is from the documentation for the FontConverter class. It shows
you how to get a converter for the font, as well as get the string for a
particular font:

private void ShowFontStringConversion(PaintEventArgs e)
{

// Create the FontConverter.
System.ComponentModel.TypeConverter converter =
System.ComponentModel.TypeDescriptor.GetConverter(typeof(Font));

Font font1 = (Font) converter.ConvertFromString("Arial, 12pt");

string fontName1 = converter.ConvertToInvariantString(font1);
string fontName2 = converter.ConvertToString(font1);

e.Graphics.DrawString(fontName1, font1, Brushes.Red, 10, 10);
e.Graphics.DrawString(fontName2, font1, Brushes.Blue, 10, 30);
}

To convert from a string to an object, it's easy, just call the
ConvertFromInvariantString (you want to store these, as they are not
locale-specific):

Font f = (Font) fc.ConvertFromInvariantString(s);

--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)
Tim said:
Thank you Nicholas, I don't suppose you have an example do you?

Tim

Nicholas Paldino said:
Tim,

Use the FontConverter class in the System.Drawing namespace. It will
convert a font to a string representation, and back to a Font again, with
the appropriate point size, styles, etc, etc. The output of
FontConverter would be what you store in your database (or construct from
your database, but its easier to just store the value it outputs).

Hope this helps.


--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)

Tim said:
Hi Guys,

I want to build a font like so:
headerFont = new Font("Times New Roman", 10, FontStyle.Underline |
FontStyle.Bold);

It is going to be built dynamically from the DB. How do I build the
styles dynamically. In the above example it has underline and bold, it
could also have italics, or none of them, or a different combination.

How do I build that in code? I guess it is the pipe character which is
throwing me off.

Thanks,

Tim
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top