views:

336

answers:

1

The FontDialog class in C# has a property "AllowScriptChange" that lets the user select the script (Western, Hebrew, Arabic, Turkish, etc). When enabled, the dropdown box provides all those options and whatever else is available depending on the font selected.

If the dialog is successful, the font selected has GdiCharSet set a value from 0-255. 177 is Hebrew, 161 is Greek, etc. Is there a function that will convert from value to string? I can write a switch statement myself but I'd like to do it The Right Way.

This is a partial list: http://msdn.microsoft.com/en-us/library/cc194829.aspx

Edit: A function that will convert from CharSet to codepage would work, too, because I think that getting the name of a codepage should be easy.

+2  A: 

If you don't want to use a switch how about using an enum? Something like:

public enum CharSet : byte
{
        ANSI_CHARSET = 0,
        DEFAULT_CHARSET = 1,
        SYMBOL_CHARSET = 2,
        SHIFTJIS_CHARSET = 128,
        HANGEUL_CHARSET = 129,
        HANGUL_CHARSET = 129,
        GB2312_CHARSET = 134,
        CHINESEBIG5_CHARSET = 136,
        OEM_CHARSET = 255,
        JOHAB_CHARSET = 130,
        HEBREW_CHARSET = 177,
        ARABIC_CHARSET = 178,
        GREEK_CHARSET = 161,
        TURKISH_CHARSET = 162,
        VIETNAMESE_CHARSET = 163,
        THAI_CHARSET = 222,
        EASTEUROPE_CHARSET = 238,
        RUSSIAN_CHARSET = 204
    }

And you can set up an Extension Method if your using .NET 3.5

public static class GdiCharHelper
{
    public static string ToGdiName(this byte GdiCharSet)
    {
            return Enum.GetName(typeof(CharSet), GdiCharSet);
    }
}

So you can use it in your code like so:

string name = Font.GdiCharSet.ToGdiName();

EDIT: Now that I think about it, you should probably change the return value of the Extension method to be the enum, so:

return (CharSet)GdiCharSet;

That way you can compare too:

If (Font.GdiCharSet.ToCharSet() == CharSet.ANSI_CHARSET) {...}
Paul U