Easily display subscripts and superscripts in a RichTextBox in C#

[subscripts and superscripts]

The example Display subscripts and superscripts in a RichTextBox in C# requires you to make arrays holding the positions of the characters that should be subscripts and superscripts. That’s not a big deal but it does require you to count the characters carefully.

This example lets you indicate which characters should be subscripts and superscripts by placing – or + in front of them in the text you assign to the RichTextBox. If you want to display – or +, place — or ++ in the text.

The following MakeRtfSubsSupers method takes as parameters the RichTextBox and the text. It then makes the RichTextBox display the text with the desired subscripts and superscripts.

// 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)
    {
        // See if this character is - or  +.
        char ch = text[pos];
        if (ch == '-')
        {
            // Add the next character to the new text.
            pos++;
            new_text += text[pos];

            // See if this is a subscript.
            if (text[pos] != '-') subs.Add(new_text.Length - 1);
        }
        else if (ch == '+')
        {
            // Add the next character to the new text.
            pos++;
            new_text += text[pos];

            // See if this is a superscript.
            if (text[pos] != '+') 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;
    }
}

The method creates lists to hold the indexes of the characters that should be subscripts and superscripts. It then examines the characters in the input text.

If a character is -, the method adds the following character to the result text. That will either be – if this is a — sequence, or it will be the character that should be subscripted. If the following characters is not -, then that character should be subscripted. In that case the code adds the character’s position to the subs list.

The method follows similar steps if the character it is examining is +.

If the character is neither – nor +, the method simply adds it to the result text.

After it has examined all of the input text, the method sets the RichTextBox object’s Text property to the result text. It then loops through the subscript and superscript indices and formats those characters appropriately.


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.

3 Responses to Easily display subscripts and superscripts in a RichTextBox in C#

  1. Pingback: Display subscripts and superscripts in a WPF TextBlock in C# - C# HelperC# Helper

  2. Sudhir Srivastava says:

    Very nice control but one thing i need to show negative power then how to display…

Leave a Reply

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