RichTextBox.SelectionCharOffset Property

Definition

Gets or sets whether text in the control appears on the baseline, as a superscript, or as a subscript below the baseline.

public:
 property int SelectionCharOffset { int get(); void set(int value); };
[System.ComponentModel.Browsable(false)]
public int SelectionCharOffset { get; set; }
[<System.ComponentModel.Browsable(false)>]
member this.SelectionCharOffset : int with get, set
Public Property SelectionCharOffset As Integer

Property Value

A number that specifies the character offset.

Attributes

Exceptions

The specified value was less than -2000 or greater than 2000.

Examples

The following code example demonstrates how to specify superscript and subscript text within the RichTextBox using the SelectionCharOffset property. This example requires that a RichTextBox control, named richTextBox1, has been added to the form.

private:
   void WriteOffsetTextToRichTextBox()
   {
      // Clear all text from the RichTextBox.
      richTextBox1->Clear();

      // Set the font for the text.
      richTextBox1->SelectionFont = gcnew System::Drawing::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;
   }
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; 
}
Private Sub 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.SelectedText = ControlChars.CrLf + ControlChars.CrLf
   ' Change the forecolor of the next text selection.
   RichTextBox1.SelectionColor = Color.Blue
   ' Set the baseline text.
   RichTextBox1.SelectedText = "777"
   ' 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
End Sub

Remarks

The value of this property must be between -2000 and 2000.

If this property is set to zero, the text appears on the baseline. If it is a positive number, the number specifies the number of pixels by which to raise the text selection above the baseline. If it is a negative number, this number specifies the number of pixels by which to subscript the text selection. You can use this property to specify text as superscript or subscript.

If no text is selected, the offset is applied to the current insertion point and to all text that the user types after the insertion point. The character offset applies until the property is changed to a different value or until the insertion point is moved to a different section within the control.

If text is selected within the control, the selected text and any text entered after the text selection will have the value of this property applied to it. You can use this property to create superscript and subscript text for such applications as mathematical expressions.

Applies to