如何使用 vb.net 将下标和上标添加到 Richtextbox

How to Add Subscript and Superscript to Richtextbox with vb.net

我在使用 vb.net 向 Richtextbox 中的文本添加上标和下标时遇到一些问题,有人可以帮忙吗?

此处,直接来自 MSDN 网站:

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

来源:RichTextBox.SelectionCharOffset Property

为上标

Private Sub SuperScriptMenuItem_Click(sender As Object, e As EventArgs) Handles SuperScriptMenuItem.Click
        Try
            If RichTextBox1.SelectedText = "" Then
                MsgBox(" Please select the text first", MsgBoxStyle.Information, "Select")
            Else
                RichTextBox1.SelectionCharOffset = 10
                RichTextBox1.SelectedText = RichTextBox1.SelectedText
                RichTextBox1.SelectionCharOffset = 0

            End If
        Catch ex As Exception
            MsgBox(" This formatting can't possible", MsgBoxStyle.Information, "Superscript")
        End Try

    End Sub

下标

Private Sub SubScriptMenuItem_Click(sender As Object, e As EventArgs) Handles SubScriptMenuItem.Click
        Try
            If RichTextBox1.SelectedText = "" Then
                MsgBox(" Please select the text first", MsgBoxStyle.Information, "Select")
            Else
                RichTextBox1.SelectionCharOffset = -10
                RichTextBox1.SelectedText = RichTextBox1.SelectedText
                RichTextBox1.SelectionCharOffset = 0
            End If

        Catch ex As Exception
            MsgBox(" This formatting can't possible", MsgBoxStyle.Information, "Subscript")
        End Try

    End Sub