Export (0) Print
Expand All

RichTextBox.SelectionCharOffset Property

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

Namespace:  System.Windows.Forms
Assembly:  System.Windows.Forms (in System.Windows.Forms.dll)

[BrowsableAttribute(false)]
public int SelectionCharOffset { get; set; }

Property Value

Type: System.Int32
A number that specifies the character offset.

ExceptionCondition
ArgumentException

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

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.

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 = 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; 
		}

.NET Framework

Supported in: 4.5.2, 4.5.1, 4.5, 4, 3.5, 3.0, 2.0, 1.1, 1.0

.NET Framework Client Profile

Supported in: 4, 3.5 SP1

Windows 8.1, Windows Server 2012 R2, Windows 8, Windows Server 2012, Windows 7, Windows Vista SP2, Windows Server 2008 (Server Core Role not supported), Windows Server 2008 R2 (Server Core Role supported with SP1 or later; Itanium not supported)

The .NET Framework does not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.

Show:
© 2014 Microsoft