Click here to Skip to main content
15,040,212 members
Please Sign up or sign in to vote.
5.00/5 (1 vote)
See more:
Hi guys,
I'm currently trying to use bolding, superscript etc in order to format data in a rich textbox.

Does anyone know how to get the selection position within the RTF code??
NB i DON'T mean within the TEXT, but rather where in the RTF.

e.g Say the caret position was between the 'a' and the 't' in the the word sat
Text:
I sat on the mat
RTF (roughly):
I /b sat/b0 on the mat

Selection position would return 4 because it's the fourth place in the TEXT.
I would want to have 7 returned, because that's where the caret is in the RTF code.

FYI i reason I'm not using code like:
FontStyle style2Add = aRichTextBox.SelectionFont.Style ^ FontStyle.Bold;
aRichTextBox.SelectionFont = new Font(aRichTextBox.SelectionFont, style2Add | (aRichTextBox.SelectionFont.Style ^ FontStyle.Bold));

is because it has a lovely tendency to wipe out other formatting if you have selected a word which is formatted differently at the end. Formatting words on a letter-by-letter basis (which roughly would be a loop of the above for each character) is simply too slow when you have a lot of text highlighted (i've tried it).

So, the question again: Does anyone know how to get the selection position within the RTF code??

Thanks!
Posted

Havge you tried the SelectedRtf property?
Summary:
Gets or sets the currently selected rich text format (RTF) formatted text in the control.
Returns:
The selected RTF text in the control.

Can you not work with that, rather then trying to find the position?
   
Any ideas on inserting tags at the cursor position with such a method??

I can use the richtextbox.selectedRTF property to change the font, and input tags such as "//super " (superscript) and so on, which works great.

e.g.
"{\\rtf1\\ansi\\ansicpg1252\\deff0\\deflang5129{\\fonttbl{\\f0\\fnil\\fcharset0 Arial;}}\r\n\\uc1\\pard\\fs24 Some Selected Text!}\r\n"

*INSERT SUPERSCRIPT TAG*
"{\\rtf1\\ansi\\ansicpg1252\\deff0\\deflang5129{\\fonttbl{\\f0\\fnil\\fcharset0 Arial;}}\r\n\\uc1\\pard\\super\\fs24 Some Selected Text!}\r\n"


However, if no text is highlighted, I have no way of inserting formatting at the caret position, because the selectedRTF looks something like this:

"{\\rtf1\\ansi\\ansicpg1252\\deff0\\deflang5129\\uc1 }\r\n"


if you pressed the 'superscript' button, you'd expect to be able to type in superscript, even if nothing around was selected.
Any ideas on inserting tags at the cursor position??

Tearing my hair out here :s
   

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900