Display subscripts and superscripts in a RichTextBox in C#, version 2

[subscripts and superscripts]

The following examples show how to display subscripts and superscripts in a RichTextBox.

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.
            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.

    // 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).

Download Example   Follow me on Twitter   RSS feed   Donate

About RodStephens

Rod Stephens is a software consultant and author who has written more than 30 books and 250 magazine articles covering C#, Visual Basic, Visual Basic for Applications, Delphi, and Java.
This entry was posted in fonts, formatting and tagged , , , , , , , , , , , , , . Bookmark the permalink.

Leave a Reply

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