PDA

Click to See Complete Forum and Search --> : Super and Subscript in RichTextBox


moeur
Aug 14th, 2005, 12:24 PM
The RichTextBox control supports the RTF codes for super and subscripting, but the control itself does not give you access to those features.
Here is some simple code to add that functionality.
Public Sub SetSubScript(RTB As RichTextBox)
Dim iPos As Long
Dim strRTF As String
With RTB
If .SelCharOffset >= 0 Then
iPos = .SelStart
.SelText = Chr(128) & .SelText & Chr(129) ' \'80 \'81
strRTF = Replace(.TextRTF, "\'80", "\sub\dn1 ")
.TextRTF = Replace(strRTF, "\'81", "\nosupersub\up0 ")
.SelStart = iPos
Else
.SelText = Chr(128) & .SelText
strRTF = .TextRTF
.TextRTF = Replace(strRTF, "\'80", "\nosupersub\up0 ", , 1)
End If
End With
End Sub

Public Sub SetSuperScript(RTB As RichTextBox)
'add tags \super\up1 and \nosupersub\up0
Dim iPos As Long
Dim strRTF As String

With RTB
iPos = .SelStart
If RTB.SelCharOffset <= 0 Then
.SelText = Chr(128) & .SelText & Chr(129) ' \'80 \'81
strRTF = Replace(.TextRTF, "\'80", "\super\up1 ")
.TextRTF = Replace(strRTF, "\'81", "\nosupersub\up0 ")
Else
.SelText = Chr(128) & .SelText
strRTF = .TextRTF
.TextRTF = Replace(strRTF, "\'80", "\nosupersub\up0 ", , 1)
End If
.SelStart = iPos
End With

End Sub