Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

WPF RichTextBox to create editor with line numbers [closed]

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?

like image 618
John Alden Avatar asked Feb 19 '09 22:02

John Alden


3 Answers

AvalonEdit is a good one, and it's open source. I think it has nearly all of the features of the Aqistar control such as syntax highlighting and folding. Ease to configure and use. Further details can be found here. enter image description here

like image 70
Dave Clemmer Avatar answered Sep 30 '22 20:09

Dave Clemmer


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.

like image 44
casperOne Avatar answered Sep 30 '22 21:09

casperOne


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.

like image 27
Frank Krueger Avatar answered Sep 30 '22 21:09

Frank Krueger