473,426 Members | 1,851 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,426 software developers and data experts.

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

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
3 1658
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: Mark | last post by:
Hi all, i'm trying to serialize a class. Using the constructor of XmlSerializer i get these (odd?) errors: "File or assembly name goseij9w.dll, or one of its dependencies, was not found"....
12
by: Brian | last post by:
I want to use regxp to check that a form input contains at least 1 non-space charcter. I'd like to only run this if the browser supports it. For DOM stuff, I'd use if (documentGetElementById) {}...
4
by: Derek Martin | last post by:
I have an object with several string elements that I would like to check for invalid characters in the properties of each element. Can I use string.replace to do that or is there a better...
15
by: Sam Kong | last post by:
Hello! I got recently intrigued with JavaScript's prototype-based object-orientation. However, I still don't understand the mechanism clearly. What's the difference between the following...
12
by: Michael | last post by:
In PHP there is a function called str_replace (http://php.net/str_replace). Basically you can freed in two strings and a "subject" string. Then it goes through the subject string searching for...
11
by: jarod1701 | last post by:
Hi, i'm currently trying to replace an unknown string using regular expressions. For example I have: user_pref("network.proxy.http", "server1") What do I have to do to replace the...
21
by: gary | last post by:
How would one make the ECMA-262 String.replace method work with a string literal? For example, if my string was "HELLO" how would I make it work in this instance. Please note my square...
3
by: Jeremy | last post by:
I've created a serializable class and put attributes around all the properties that should be serialized. I return the class from a web service, but my problem is that the wsdl for the web service...
1
by: NvrBst | last post by:
I want to use the .replace() method with the regular expression /^ %VAR % =,($|&)/. The following DOESN'T replace the "^default.aspx=,($|&)" regular expression with "":...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.