none
setting superscript and subscript text in a rich text box RRS feed

  • Question

  • How can I programmatically set a character into superscript or subscript in a system.Windows.RichTextBox I control, I can easly type it in using the same way I do in Word, but there doesn't seem to be a way to do it programmatically. 

    Tuesday, December 12, 2006 8:03 PM

All replies

  • Use the RTB's SelectionCharOffset property.
    Wednesday, December 13, 2006 1:04 AM
    Moderator
  • You can use the RichTextBox.SelectionCharOffset Property  to achive this.
    The following MSDN link gives  examples for this

    http://msdn2.microsoft.com/en-us/library/system.windows.forms.richtextbox.selectioncharoffset(VS.80).aspx

    Wednesday, December 13, 2006 1:04 AM
  • Thank you very much, that definitly solves my problem.
    Saturday, December 16, 2006 2:24 PM
  • Hi and thanks a lot for the reply, the problem with SelectionCharOffset is that it controls the offset level of the character without making it a true superscript or subscript (it doesn't become smaller for instance); if you look more closely to the rtf, you will see that it sets \upN rather than \super, so you could have both \super and \upN set.  I'm almost convinced that the RTB implementation does not have a property to control the super/sub behavior, so what I will do is use the SelectionCharOffset property and than replace \upN with \super and replace \up0 with \nosupersub in the underlying Rtf.  This a bit hacky but it solves my problem very cleanly.

    Saturday, December 16, 2006 5:32 PM