The following examples show how to display subscripts and superscripts in a RichTextBox.
- Display subscripts and superscripts in a RichTextBox in C#
- Easily display subscripts and superscripts in a RichTextBox in C#
Those were fairly simple examples, and Sudhir Srivastava found a situation that the second one didn’t handle: it has trouble displaying + and – signs in subscripts and superscripts. This example uses a slightly modified approach to handle those characters.
The key is the following MakeRtfSubsSupers method.
// Make subscripts and superscripts in the control // for characters following - and +. To make - or +, // use /- and /+. private void MakeRtfSubsSupers(RichTextBox rch, string text, Font small_font, int offset) { // Find the subscript and superscript positions. List<int> subs = new List<int>(); List<int> supers = new List<int>(); string new_text = ""; int pos = 0; while (pos < text.Length) { char ch = text[pos]; // Check for special characters. if ((ch == '/') || (ch == '-') || (ch == '+')) { // Add the next character to the new text. pos++; new_text += text[pos]; // Mark as a subscript or superscript if necessary. if (ch == '-') subs.Add(new_text.Length - 1); if (ch == '+') supers.Add(new_text.Length - 1); } else new_text += ch; // Move to the next character. pos++; } // Format the subscripts and superscripts. rch.Text = new_text; foreach (int position in subs) { rch.Select(position, 1); rch.SelectionCharOffset = -offset; rch.SelectionFont = small_font; } foreach (int position in supers) { rch.Select(position, 1); rch.SelectionCharOffset = offset; rch.SelectionFont = small_font; } }
This method loops through the characters in a string. If it finds a / character, it adds the following character to the new string without interpreting that character. For example, to enter a + or – sign, the string should include /+ or /- respectively.
If the code finds a – sign, it adds the following character to the new string and records that character’s position in the subs list.
If the code finds a + sign, it adds the following character to the new string and records that character’s position in the supers list.
After it finishes processing the string, the code loops through the subs and supers lists and adjusts those characters to make the subscripts and superscripts as appropriate.
The sample text shown in the picture at the top of this post demonstrates the +, -, and / characters in both subscripts and superscripts (and regular text).




This blog is so helpful.
You are great Mr. Stephens.
Thanks! It’s always nice to hear that people are benefiting from my books and web sites.
Indeed, the above code is very helpful and useful.
I have implemented and expanded on it in my code, and it works great.
Now I would like to print the results in a document. I am thinking of using PrintDocument / PrintPage, as mentioned in one of your other posts. I found out that a formatted text is hard to draw.
Do you have any suggestions on how to approach this?
Thank you in advance
Yes, unfortunately printing can be hard.
One easies solution would be to save the RTF text into a RTF file and then use another program such as WordPad or Word to print. You could use Word from within your program so you can do it automatically.