RichTextBox.SelectionCharOffset 属性

定义

获取或设置控件中的文本是显示在基线上、作为上标还是作为基线下方的下标。

C#
[System.ComponentModel.Browsable(false)]
public int SelectionCharOffset { get; set; }

属性值

Int32

指定字符偏移量的数字。

属性

例外

指定的值小于 -2000 或大于 2000。

示例

下面的代码示例演示如何在使用SelectionCharOffset属性中RichTextBox指定上标和下标文本。 此示例要求 RichTextBox 已将名为的 richTextBox1控件添加到窗体中。

C#
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; 
}

注解

此属性的值必须介于 -2000 和 2000 之间。

如果此属性设置为零,则文本将显示在基线上。 如果它是正数,则数字指定在基线上方引发文本选择的像素数。 如果它是负数,则此数字指定文本选定内容下标的像素数。 可以使用此属性将文本指定为上标或下标。

如果未选择任何文本,则偏移量将应用于当前插入点以及用户键入插入点后键入的所有文本。 字符偏移量将应用于属性更改为其他值,或者直到插入点移动到控件中的不同节为止。

如果在控件中选择文本,则所选文本和文本选择后输入的任何文本都将应用此属性的值。 可以使用此属性为数学表达式等应用程序创建上标和下标文本。

适用于

产品 版本
.NET Framework 1.1, 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8
Windows Desktop 3.0, 3.1, 5, 6, 7