By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
458,013 Members | 1,167 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 458,013 IT Pros & Developers. It's quick & easy.

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

P: n/a
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
Nov 17 '05 #1
Share this Question
Share on Google+
3 Replies


P: n/a
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]
- mv*@spam.guard.caspershouse.com

"Lance" <lance@[nospam]keayweb.com> wrote in message
news:11*************@corp.supernews.com...
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

Nov 17 '05 #2

P: n/a
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]
- mv*@spam.guard.caspershouse.com

"Lance" <news@[nospam]keayweb.com> wrote in message
news:eH*****************@news-1.opaltelecom.net...
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]
- mv*@spam.guard.caspershouse.com

"Lance" <lance@[nospam]keayweb.com> wrote in message
news:11*************@corp.supernews.com...
> 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
>
>



Nov 17 '05 #3

P: n/a
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 [.NET/C# MVP]" <mv*@spam.guard.caspershouse.com> wrote in
message news:eD****************@TK2MSFTNGP09.phx.gbl...
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]
- mv*@spam.guard.caspershouse.com

"Lance" <news@[nospam]keayweb.com> wrote in message
news:eH*****************@news-1.opaltelecom.net...
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]
- mv*@spam.guard.caspershouse.com

"Lance" <lance@[nospam]keayweb.com> wrote in message
news:11*************@corp.supernews.com...
> 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
>
>



Nov 17 '05 #4

This discussion thread is closed

Replies have been disabled for this discussion.