Replying to Messages

I received a few questions from my blog readers in email. I will try my best to answer them in this post.

Question 1: Hi there! I saw that you were posting about the use of RichTextBox control and I thought you might be able to help me on my problem: how can I get the current line number and the current character position when using the RichTextBox control? The TextBox control has some methods for this. But for the RichTextBox, I can’t seem to find them. Hope you can help. Thanks.

While the TextBox’s content is plain text only, the RichTextBox contains a FlowDocument that has rich flow content elements (see and my previous post on FlowDocument schema). RichTextBox doesn’t expose API to get a character or line index, but you can construct this information using some code.

How to get character index?

You can get the current TextPointer position by using RichTextBox.GetPositionFromPoint method. This method returns a TextPointer insertion position closest to the supplied point. Once you have this position, you can keep calling TextPointer.GetNextInsertionPosition(LogicalDirection.Backward) in a loop until it returns null, counting caret positions from current character until the document start.

How to get line index?

You can keep calling TextPointer.GetLineStartPosition (-1) in a loop until you reach the first line, counting lines.

Question 2: Your implementation of the XPath Visualizer is most interesting. Does your XPath Visualizer now have support for XPath 2.0 ? Thank you.

I believe you are referring to my MSDN article at Thanks for your feedback. At this moment, I don’t have any plans to update this code for XPath 2.0. (Though, that may be a very interesting WPF project!)

You should check out the XML team’s current blog at One recent post talks about an XPathMania plug-in to VS2005 xml editor. That should be really cool!

Question 3: I’m trying to build an Inline that draws itself like a square root with the text inside it. The thing is that I can’t find the way to change the look of an inline, or anything that gets me to my purpose. Can you help? Thanks.

If you want to build something that also edits such Inlines, we don’t support that in V1. If your content is read-only, you might want to try the WPF Text team’s blog or post your question on the WPF forum.