[C# Helper]
Index Books FAQ Contact About Rod
[Beginning Database Design Solutions, Second Edition]

[Beginning Software Engineering, Second Edition]

[Essential Algorithms, Second Edition]

[The Modern C# Challenge]

[WPF 3d, Three-Dimensional Graphics with WPF and C#]

[The C# Helper Top 100]

[Interview Puzzles Dissected]

[C# 24-Hour Trainer]

[C# 5.0 Programmer's Reference]

[MCSD Certification Toolkit (Exam 70-483): Programming in C#]

Title: Display Unicode symbols in C#

[Display Unicode symbols in C#]

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 the example to experiment with it and to see additional details.

© 2009-2023 Rocky Mountain Computer Consulting, Inc. All rights reserved.