how to replace an object's property with an unknown string vaule

L

Lance

Hi,
Sorry for the confusing subject, but here is what I am after: I am using
the 'System.Drawing.FontStyle' object, and I would like the caller of the
function to be able to decide when calling the function what style to use.
Hmm even more confusion, methinks. Here is some pseudocode for what I want
to do:

//calling the function
CreateGifImage("Bold");

//function to call
public static string CreateGifImage( string sFontStyle)
{
....
Font MyFont = new Font( sFont, pxSize,
System.Drawing.FontStyle.(sFontStyle),
System.Drawing.GraphicsUnit.Pixel );
....}


So the caller just passes in a string that matches up to an actuall
fontstyle, and MyFont is happily created. I'm looking for an alternative
to:

Font MyFont = new Font( sFont, pxSize,
(sFontStyle == "bold" ? System.Drawing.FontStyle.Bold :
System.Drawing.FontStyle.Regular),
System.Drawing.GraphicsUnit.Pixel );

Thanks,
Lance
 
N

Nicholas Paldino [.NET/C# MVP]

Lance,

You will want to use the FontConverter class in the System.Drawing
namespace to convert from the string to your font. It will allow you to
take a string like "Microsoft Sans Serif, 12pt, style=Bold, Underline,
Strikeout" (this is the same exact way you would see it in a property grid
for a property that exposes a Font instance, and you will have to adhere to
this format), and convert it to the appropriate font. Your code would look
like this:

public static string CreateGifImage( string sFontStyle)
{
...
// Create the type converter.
FontConverter converter = new FontConverter();

// Convert from the string to a font.
Font font = (Font) converter.ConvertFrom(null,
CultureInfo.CurrentUICulture, sFontStyle);

// Do something with the font.
}

You might have to play around with the culture to get it right (you
might want to pass an InvariantCulutre, depending on what you are doing, or
the value returned from CultureInfo.CurrentCulture), but that's basically
how you would make the call.

Hope this helps.
 
L

Lance

Hi,
That works really well, for this particular application. I can't seem to
find any syntax information on converting from a string; your example has
"Microsoft Sans Serif, 12pt, style=Bold, Underline, Strikeout", and that
works fine, but what other values are acceptable?

Also, is there a way in C# to do what I initially wanted? That is, using a
string as a property by using some sort of *magical* runtime conversion or
something.

Thanks,
Lance
You will want to use the FontConverter class in the System.Drawing
namespace to convert from the string to your font. It will allow you to
take a string like "Microsoft Sans Serif, 12pt, style=Bold, Underline,
Strikeout" (this is the same exact way you would see it in a property grid
for a property that exposes a Font instance, and you will have to adhere to
this format), and convert it to the appropriate font. Your code would look
like this:

public static string CreateGifImage( string sFontStyle)
{
...
// Create the type converter.
FontConverter converter = new FontConverter();

// Convert from the string to a font.
Font font = (Font) converter.ConvertFrom(null,
CultureInfo.CurrentUICulture, sFontStyle);

// Do something with the font.
}

You might have to play around with the culture to get it right (you
might want to pass an InvariantCulutre, depending on what you are doing, or
the value returned from CultureInfo.CurrentCulture), but that's basically
how you would make the call.

Hope this helps.


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

Lance said:
Hi,
Sorry for the confusing subject, but here is what I am after: I am using
the 'System.Drawing.FontStyle' object, and I would like the caller of the
function to be able to decide when calling the function what style to use.
Hmm even more confusion, methinks. Here is some pseudocode for what I
want
to do:

//calling the function
CreateGifImage("Bold");

//function to call
public static string CreateGifImage( string sFontStyle)
{
...
Font MyFont = new Font( sFont, pxSize,
System.Drawing.FontStyle.(sFontStyle),
System.Drawing.GraphicsUnit.Pixel );
...}


So the caller just passes in a string that matches up to an actuall
fontstyle, and MyFont is happily created. I'm looking for an alternative
to:

Font MyFont = new Font( sFont, pxSize,
(sFontStyle == "bold" ? System.Drawing.FontStyle.Bold :
System.Drawing.FontStyle.Regular),
System.Drawing.GraphicsUnit.Pixel );

Thanks,
Lance
 
N

Nicholas Paldino [.NET/C# MVP]

Lance,

The format is

<font family>, <point size>pt, style=<styles delimited by comma>

So you could do Arial 8pt by using the string:

Arial, 8pt

As for what you wanted before, you can get the value from the
enumeration by using the static Parse method on the Enum class, like so:

// Get the value for the style.
FontStyle style = (FontStyle) Enum.Parse(typeof(FontStyle), sFontStyle,
true);


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

Lance said:
Hi,
That works really well, for this particular application. I can't seem to
find any syntax information on converting from a string; your example has
"Microsoft Sans Serif, 12pt, style=Bold, Underline, Strikeout", and that
works fine, but what other values are acceptable?

Also, is there a way in C# to do what I initially wanted? That is, using
a
string as a property by using some sort of *magical* runtime conversion or
something.

Thanks,
Lance
You will want to use the FontConverter class in the System.Drawing
namespace to convert from the string to your font. It will allow you to
take a string like "Microsoft Sans Serif, 12pt, style=Bold, Underline,
Strikeout" (this is the same exact way you would see it in a property
grid
for a property that exposes a Font instance, and you will have to adhere to
this format), and convert it to the appropriate font. Your code would look
like this:

public static string CreateGifImage( string sFontStyle)
{
...
// Create the type converter.
FontConverter converter = new FontConverter();

// Convert from the string to a font.
Font font = (Font) converter.ConvertFrom(null,
CultureInfo.CurrentUICulture, sFontStyle);

// Do something with the font.
}

You might have to play around with the culture to get it right (you
might want to pass an InvariantCulutre, depending on what you are doing, or
the value returned from CultureInfo.CurrentCulture), but that's basically
how you would make the call.

Hope this helps.


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

Lance said:
Hi,
Sorry for the confusing subject, but here is what I am after: I am using
the 'System.Drawing.FontStyle' object, and I would like the caller of the
function to be able to decide when calling the function what style to use.
Hmm even more confusion, methinks. Here is some pseudocode for what I
want
to do:

//calling the function
CreateGifImage("Bold");

//function to call
public static string CreateGifImage( string sFontStyle)
{
...
Font MyFont = new Font( sFont, pxSize,
System.Drawing.FontStyle.(sFontStyle),
System.Drawing.GraphicsUnit.Pixel );
...}


So the caller just passes in a string that matches up to an actuall
fontstyle, and MyFont is happily created. I'm looking for an alternative
to:

Font MyFont = new Font( sFont, pxSize,
(sFontStyle == "bold" ? System.Drawing.FontStyle.Bold :
System.Drawing.FontStyle.Regular),
System.Drawing.GraphicsUnit.Pixel );

Thanks,
Lance
 
L

Lance

Thanks! - I just managed to get the syntax required by making the
FontConverter throw an error - and the message said something like... what
you just said.

I'll have to fool around with the Parse method and see if I can get it to
work. But I'll do that tomorrow...

Thanks again

Nicholas Paldino said:
Lance,

The format is

<font family>, <point size>pt, style=<styles delimited by comma>

So you could do Arial 8pt by using the string:

Arial, 8pt

As for what you wanted before, you can get the value from the
enumeration by using the static Parse method on the Enum class, like so:

// Get the value for the style.
FontStyle style = (FontStyle) Enum.Parse(typeof(FontStyle), sFontStyle,
true);


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

Lance said:
Hi,
That works really well, for this particular application. I can't seem to
find any syntax information on converting from a string; your example has
"Microsoft Sans Serif, 12pt, style=Bold, Underline, Strikeout", and that
works fine, but what other values are acceptable?

Also, is there a way in C# to do what I initially wanted? That is, using
a
string as a property by using some sort of *magical* runtime conversion or
something.

Thanks,
Lance
You will want to use the FontConverter class in the System.Drawing
namespace to convert from the string to your font. It will allow you to
take a string like "Microsoft Sans Serif, 12pt, style=Bold, Underline,
Strikeout" (this is the same exact way you would see it in a property
grid
for a property that exposes a Font instance, and you will have to
adhere
to
this format), and convert it to the appropriate font. Your code would look
like this:

public static string CreateGifImage( string sFontStyle)
{
...
// Create the type converter.
FontConverter converter = new FontConverter();

// Convert from the string to a font.
Font font = (Font) converter.ConvertFrom(null,
CultureInfo.CurrentUICulture, sFontStyle);

// Do something with the font.
}

You might have to play around with the culture to get it right (you
might want to pass an InvariantCulutre, depending on what you are
doing,
or
the value returned from CultureInfo.CurrentCulture), but that's basically
how you would make the call.

Hope this helps.


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

Hi,
Sorry for the confusing subject, but here is what I am after: I am using
the 'System.Drawing.FontStyle' object, and I would like the caller of the
function to be able to decide when calling the function what style to use.
Hmm even more confusion, methinks. Here is some pseudocode for what I
want
to do:

//calling the function
CreateGifImage("Bold");

//function to call
public static string CreateGifImage( string sFontStyle)
{
...
Font MyFont = new Font( sFont, pxSize,
System.Drawing.FontStyle.(sFontStyle),
System.Drawing.GraphicsUnit.Pixel );
...}


So the caller just passes in a string that matches up to an actuall
fontstyle, and MyFont is happily created. I'm looking for an alternative
to:

Font MyFont = new Font( sFont, pxSize,
(sFontStyle == "bold" ? System.Drawing.FontStyle.Bold :
System.Drawing.FontStyle.Regular),
System.Drawing.GraphicsUnit.Pixel );

Thanks,
Lance
 

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