[C# Helper]
Index Books FAQ Contact About Rod
[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]

[Beginning Software Engineering]

[C# 5.0 Programmer's Reference]

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

Title: Explore Unicode characters in C#

Unicode characters

This example displays Unicode characters within a range of numeric values. Enter the minimum and maximum values to display and a font size. Then click List to display the characters in the range. When you move the mouse over the characters, the TextBox on the bottom left shows the character's code. The Label to its right shows a sample of the character.

The sample character is helpful because the TextBox doesn't always do a great job of knowing which character is under the mouse. For example, if you look closely at the picture, you'll see that the mouse is slightly below and to the left of the character that is actually selected. You can use the sample to verify that you have selected the character you want.

Note that some characters confuse the TextBox. For example, if you try to list characters 0 through 100, you won't see anything. If you try to list characters 1 through 100, you'll get lots of interesting results.

This example uses the following code to display characters.

// Display the desired Unicode characters private void btnList_Click(object sender, EventArgs e) { txtChars.Clear(); txtCharCode.Clear(); Cursor = Cursors.WaitCursor; Refresh(); // Set the font size. float font_size = float.Parse(txtFontSize.Text); txtChars.Font = new Font("Times New Roman", font_size); // Display the characters. int min = int.Parse(txtMin.Text); int max = int.Parse(txtMax.Text); StringBuilder sb = new StringBuilder(); for (int i = min; i <= max; i++) sb.Append(((char)i).ToString()); txtChars.Text = sb.ToString(); txtChars.Select(0, 0); Cursor = Cursors.Default; }

The code gets the font size you entered and gives the txtChars control a new font of that size.

Next the code gets the minimum and maximum character codes you want to display. It loops through those values, converts each to a character, and adds the character to a StringBuilder. When it finishes, the code displays the StringBuilder's results in the txtChars TextBox.

When you move the mouse over the txtChars control, the following code displays the character's code and sample.

// Display the code for the character under the mouse. private void txtChars_MouseMove(object sender, MouseEventArgs e) { char ch = txtChars.GetCharFromPosition(e.Location); lblSample.Text = ch.ToString(); txtCharCode.Text = ((int)ch).ToString(); }

This code uses the TextBox's GetCharFromPosition method to get the character under the mouse. It uses that character's ToString method to convert it into a string and displays the string in the lblSample Label.

It then converts the character into an integer and uses the result's ToString method to display the character's numeric value.

After you use this program to find a character's Unicode value, you can display the character in other programs by using code similar to the following.

int char_code = 10048; lblSample.Text = ((char)char_code).ToString();

Or more concisely:

lblSample.Text = ((char)10048).ToString();

Download the example to experiment with it and to see additional details.

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