Display Unicode symbols in C#

[Unicode symbols]

This example shows how you can display Unicode symbols in a C# program.

Unicode (aka Uni-mess) lets you represent fonts that contain a large number of characters in more than one byte per symbol. For example, Chinese, Japanese, Cyrillic, and Arabic fonts use Unicode to display more than the 255 characters that can fit in simple one-byte ASCII codes.

Even those of us who don’t typically deal with these fonts can sometimes find Unicode characters handy. For example, Unicode can represent lots of special symbols such as ∞, ≈, and ∑.

There are several ways you can use Unicode in your programs. If you know a character’s code, you can add it to character or string literals using an escape sequence of the form:

    \uXXXX

As in the following code:

    label1.Text = "\u0460";

You can also cast a numeric value into a Unicode character as in:

    char ch = (char)0x460;

These methods are useful, but require you to know a character’s code. You can also copy and paste Unicode characters directly into Visual Studio. Use Word, WordPad, or some other program to create the character you want. You can even select characters from a web page in your browser. Then copy and paste it into Visual Studio inside a character or string literal. You can even paste values into the Properties window to set a control’s Text property at design time.

This example displays a Label and a TextBox that shows special symbols assigned at design time. It also uses the following code to generate more symbols at run time. (Note that the special symbols don’t work well in this HTML page so I’ve replaced them with ☐ symbols.)

private void Form1_Load(object sender, EventArgs e)
{
    label2.Text = "\u0460☐☐☐☐☐☐☐☐☐☐☐☐☐☐☐☐☐☐☐☐☐☐☐☐☐☐";
    textBox2.Text = "\u0460☐☐☐☐☐☐☐☐☐☐☐☐☐☐☐☐☐☐☐☐☐☐☐☐☐☐";

    string txt = "";
    for (int i = 0x460; i < 0x470; i++)
    {
        txt += (char)i;
    }
    label3.Text = txt;
}

The code first sets the text displayed in label2 and label3 equal to string literals. It then uses a loop to build a string containing Unicode characters 0x460 through 0x470 and displays them in the control label3.

The code also displays a Label and a TextBox with Unicode set at design time.


Download Example   Follow me on Twitter   RSS feed   Donate




This entry was posted in fonts, graphics and tagged , , , , , , , , , , . Bookmark the permalink.

One Response to Display Unicode symbols in C#

  1. matsolof says:

    If you want to show special symbols at an HTML page, you can use the CSS3 @font-face rule. Read more here: http://www.w3schools.com/cssref/css3_pr_font-face_rule.asp.

    An example with the typeface Cordoba and a rune font I made is here: http://www.mkforlag.com/b3cker/1996_tandskelett.php. You can see all the source code (including the PHP code) if you click “Visa” (“Show” in English) at the top of the page and then select what you want to see in the combobox.

    An important detail is that you need to use two font files: an eot file for MS Explorer and a ttf file for other browsers.

Leave a Reply

Your email address will not be published. Required fields are marked *