|
-
Jul 24th, 2008, 01:58 PM
#1
[2008] RTB counting line changes - BeforeChange event?
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!
-
Jul 24th, 2008, 02:18 PM
#2
Fanatic Member
Re: [2008] RTB counting line changes - BeforeChange event?
I think that you should decide what happens when a line is deleted. Do you reset the bookmark to the previous line, the next line, or do you get rid of it altogether.
CLanguage; 
IF Post = HelpFull Then
RateMe
Else
Say("Shut UP")
End If
DotNet rocks
VB 6, VB.Net 2003, 2005, 2008, 2010, SQL 2005, WM 5.0,ahem ?OpenRoad?
-
Jul 24th, 2008, 02:25 PM
#3
Re: [2008] RTB counting line changes - BeforeChange event?
If the deleted line is before the bookmarked line, the bookmarked line should move up one line. If the deleted line is the bookmarked line, the bookmark should be deleted aswell. If the deleted line is after the bookmarked line, nothing happens to the bookmarked line.
For example:
(in no particular order)
Bookmarked line: 15
Delete line 13 --> bookmark is now line 14
Delete line 15 --> bookmark is now gone
Delete line 18 --> bookmark stays line 15
Or when adding a line:
Bookmarked line: 15
Add a line between lines 12 and 13 --> bookmark is now line 16
Add a line between lines 18 and 19 --> bookmark stays line 15
The general idea is that the user is bookmarking the actual text line, not just the line number. If the line he bookmarked moves, the bookmark should obviously move aswell... It doesn't make sense to keep a bookmark at line 89 if the text you had bookmarked is going to be at line 190 after copying in a block of text... The bookmarked line number should also be 190 then...
Last edited by NickThissen; Jul 24th, 2008 at 02:28 PM.
-
Jul 24th, 2008, 02:39 PM
#4
Fanatic Member
Re: [2008] RTB counting line changes - BeforeChange event?
I think that you are limited to the TextChanged event unless you write your own event capturing the delete key and the backspace key
CLanguage; 
IF Post = HelpFull Then
RateMe
Else
Say("Shut UP")
End If
DotNet rocks
VB 6, VB.Net 2003, 2005, 2008, 2010, SQL 2005, WM 5.0,ahem ?OpenRoad?
-
Jul 24th, 2008, 02:45 PM
#5
Re: [2008] RTB counting line changes - BeforeChange event?
If it were only the delete and backspace key that would maybe have worked... but there is also the possibility of adding a line, or adding multiple lines (copying). So you would have to check every possible way that someone can change the text of the RTB (a few I can think of: typing and deleting via the keyboard, copying / cutting text, dragging text (either from an outside source into the RTB, from inside the RTB to an outside source, or dragging from within the RTB itself, to another location)... Those are alot of possible events to handle, I don't think I can pull that off by writing my own events lol...
So are you sure there isn't some way to detect a BeforeChange event? (Subclassing, hooking, whatever it's called, no idea what it is tbh but, at least in VB6, it was often used to do things like this lol...)
-
Jul 24th, 2008, 02:49 PM
#6
Fanatic Member
Re: [2008] RTB counting line changes - BeforeChange event?
You are right there would be too many possibilities. I am not to kin in constantly comparing the text that has changed to the before text. Hopefully someone else will come up with a better solutiion.
CLanguage; 
IF Post = HelpFull Then
RateMe
Else
Say("Shut UP")
End If
DotNet rocks
VB 6, VB.Net 2003, 2005, 2008, 2010, SQL 2005, WM 5.0,ahem ?OpenRoad?
-
Jul 25th, 2008, 12:27 PM
#7
Re: [2008] RTB counting line changes - BeforeChange event?
Sorry for bumping but I still need this... Any help (however small) would be much appreciated..!
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|