views:

1973

answers:

3

I'm creating a text editor for a domain specific language. I'm using the WPF RichTextBox as the basic control. I don't know how to gracefully include line numbering. Does anyone know of any examples?

+2  A: 

I would create a composite control, with a stack panel control and text blocks on the left which you would use to handle the line numbering. If you are concerned with the number of lines and having too many visual elements, then you could use a ListBox in virtual mode.

You would have to hook up to the various events on the RichTextBox so that you know when to update the ListBox, as well as calculate the height of each line, but that should be doable with the FlowDocument attached to the RichTextBox.

casperOne
I'm not sure if I like this idea - keeping the list box text baselines inline with the RichEdit baselines could be problematic (definitely if his code formatter allows different fonts) - but since I don't have an alternative, +1
Frank Krueger
I wasn't smart enough to make that work. I haven't been able to figure out when the RichTextBox is scrolling so that I can try to scroll the ListBox the same amount. And, as lines get too long in the RTB, it automatically wraps their display, and I should omit ListBox line numbers in that case.
John Alden
A: 

RichText supports "protected" - uneditable - spans. You could dump your line numbers as protected text spans as a part of the RTF stream (when you do your formatting).

In Win Forms, you can use RichTextBox.SelectionProtected Property. WPF must have something similar.

This way, all your baselines will be correct and you won't have to do any extra thinking/programming to get the editor to behave properly. Editable text will be editable, and line numbers will not be.

Only down side is that you have to resubmit the RTF stream after every edit. But I imagine you were already doing this to provide parse formatting / error diagnostics / whatever.

Frank Krueger
+1  A: 

How about using AqiStar's rich text WPF control? AqiStar.TextBox

I don't work for them...I just know that Oren is using this product in his NHibernate Profiler (NHProf) and he seems to dig it.

Todd Brooks
Unfortunately, my employer won't let us include software that we have to license in our solution.
John Alden