locked
Superscript text in a richtextbox RRS feed

  • Question

  • Hi,

    I need to learn how to make text inside a richtextbox superscript. Thanks!

    Wednesday, June 23, 2010 4:00 AM

Answers

  • Hi,

    You can use RichTextBox.SelectionCharOffset Property to make a text as a superscript, or as a subscript. The following code from the MSDN link demonstrates how to specify superscript and subscript text within the RichTextBox.

    private void WriteOffsetTextToRichTextBox()
        {
          // Clear all text from the RichTextBox.
          richTextBox1.Clear();
          // Set the font for the text.
          richTextBox1.SelectionFont = new Font("Lucinda Console", 12);
          // Set the foreground color of the text.
          richTextBox1.SelectionColor = Color.Purple;
          // Set the baseline text.
          richTextBox1.SelectedText = "10";
          // Set the CharOffset to display superscript text.
          richTextBox1.SelectionCharOffset = 10;
          // Set the superscripted text.  
          richTextBox1.SelectedText = "2";
          // Reset the CharOffset to display text at the baseline.
          richTextBox1.SelectionCharOffset = 0;
          richTextBox1.AppendText("\n\n");
          // Change the forecolor of the next text selection.
          richTextBox1.SelectionColor = Color.Blue;
          // Set the baseline text.
          richTextBox1.SelectedText = "77";
          // Set the CharOffset to display subscript text.
          richTextBox1.SelectionCharOffset = -10;
          // Set the subscripted text. 
          richTextBox1.SelectedText = "3";
          // Reset the CharOffset to display text at the baseline.
          richTextBox1.SelectionCharOffset = 0; 
        }
    

     

    • Marked as answer by Benciticus Friday, June 25, 2010 1:00 AM
    Wednesday, June 23, 2010 5:24 AM

All replies

  • Hi,

    You can use RichTextBox.SelectionCharOffset Property to make a text as a superscript, or as a subscript. The following code from the MSDN link demonstrates how to specify superscript and subscript text within the RichTextBox.

    private void WriteOffsetTextToRichTextBox()
        {
          // Clear all text from the RichTextBox.
          richTextBox1.Clear();
          // Set the font for the text.
          richTextBox1.SelectionFont = new Font("Lucinda Console", 12);
          // Set the foreground color of the text.
          richTextBox1.SelectionColor = Color.Purple;
          // Set the baseline text.
          richTextBox1.SelectedText = "10";
          // Set the CharOffset to display superscript text.
          richTextBox1.SelectionCharOffset = 10;
          // Set the superscripted text.  
          richTextBox1.SelectedText = "2";
          // Reset the CharOffset to display text at the baseline.
          richTextBox1.SelectionCharOffset = 0;
          richTextBox1.AppendText("\n\n");
          // Change the forecolor of the next text selection.
          richTextBox1.SelectionColor = Color.Blue;
          // Set the baseline text.
          richTextBox1.SelectedText = "77";
          // Set the CharOffset to display subscript text.
          richTextBox1.SelectionCharOffset = -10;
          // Set the subscripted text. 
          richTextBox1.SelectedText = "3";
          // Reset the CharOffset to display text at the baseline.
          richTextBox1.SelectionCharOffset = 0; 
        }
    

     

    • Marked as answer by Benciticus Friday, June 25, 2010 1:00 AM
    Wednesday, June 23, 2010 5:24 AM
  • You can use below Comand to Enable superscript.

    EditingCommands.ToggleSuperscript.Execute(null, rtb);


    Regards
    Nayan Paregi (MCTS)
    Please remember to click “Mark as Answer” on the post that helps you, and to click “Unmark as Answer” if a marked post does not actually answer your question. This can be beneficial to other community members reading the thread.
    Wednesday, June 23, 2010 6:35 AM