Keeping Track of Inserted Text in a RichTextBox

E

eBob.com

I have a RichTextBox in which users can insert text which they select from a
list. They can also do all other editing actions associated with an RTB.
The problem is, having inserted text from the list they can then decide to
delete it. And I would really like for them to only have to do a right
click on the inserted text and then, when I see that they have clicked on
text inserted from the list, I will add to the context menu an item which
deletes the text. I.E. the user should not have to "select" the text first.

So far as I can tell I will have to maintain an array of offset/lenghts of
instances of inserted text (i.e. inserted from the list of available
strings). But every time the user inserts or deletes text the offsets of
all of the following instances of inserted text will have to be adjusted.
Say the RTB contains "the quick brown fox jumped over the lazy dog"; and
then the user inserted (from the list) "FAT" so that the text is now "the
quick brown fox jumped over the FAT lazy dog". Then maybe the user deletes
"brown". That of course would change the offset of "FAT".

I fear that keeping the array of offset/lengths will get messy. And I am
afraid that I may run into show stopping problems. For example, I have no
idea how to handle cut/paste. Well, no idea that is practical.

So ... is there a better way to keep track of the text inserted from the
list? I do, btw, "protect" the text inserted from the list. But I don't
think that helps because one string of inserted text could be inserted right
after another.

Thanks for any ideas. Bob
 
O

OmegaSquared

Hello, Bob,

One idea might be to delimit the text entered from the list by a pair of RTF
control words of your own creation. Something like:

\MyRTFCodeOpen List text to enter\MyRTFCodeClose

As long as the control words are not recognized by the RichTextBox (or
whatever other RTF reader they might end up in) then the rules say they
should be ignored.

Then you could use these special codes to determine whether or not the
currently selected text originated from your list. (If text is deleted or
inserted, the delimiters will move as well.)

But you will still have the problem of preventing the user from deleting a
delimiter by deleting text that includes part (but not all) of the delimited
text. There are other things I wonder about too. For instance, if the user
inserts an "S" into the word "FAT" that came from the list, then is the
resulting word (i.e. "FAST") still considered as being from the list. If
not, the delimiters would have to be removed.

I'm not sure what you mean by "I do, btw, "protect" the text..." So perhaps
you have already taken care of this part of the problem.

Anyway, perhaps it's an idea you could consider.

Cheers,
Randy
 
E

eBob.com

Thank you Randy. That is an interesting thought. I did not realize the RTF
could contain "user" control words. Well, in fact I know next to nothing
about RTF. I'll do some investigating.

There's a property, SelectionProtected, which allows you to protect text in
an RTB. So it would be impossible to change FAT to FAST.

Thanks again, Bob
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top