Hi,

I'm creating a 'Bookmark' functionality in my RichTextBox which allows the user to bookmark a line (store the linenumber for later reference).

I am having trouble letting the program know when the number of lines in the RTB have changed. If the user adds or deletes one or a few lines, any bookmarks after that line should move accordingly (otherwise the bookmark wouldn't be of much use would it?).

I have struggled with this problem for a while now and I got a very simple idea at work today; which turned out to not be as simple as I thought.

I thought all I had to do was store the number of lines before a text change, and compare that to the number of lines after a text change. That, in combination with the SelectionStart property, should allow me to determine how many lines the bookmark should move, if any.

However, I can't find any event that allows me to store the number of lines before the text is actually edited. I was certain there would be some kind of "BeforeChange" event like many controls have, but it seems the RichTextBox does not support this...
The only workaround I could think of was to have a Timer continously store the number of lines, but I realise that this would only work properly if I use a ridiculously fast timer which would probably slow the program down alot. Using a slower timer would leave a small chance the change in lines was done in between two ticks, so it would not be picked up (for example if the user pasts a large portion of text, right in between two timer ticks). I don't like the idea of my bookmarking functionality relying on chance to work properly...

Is there any event that allows me to use my simple idea? I have googled it but no luck yet...

Or is there a better way of doing what I want to do?


Thanks for any help!