Hi Experts, here's a little brain teaser for you. It's related to a previous question I asked a couple of weeks ago:
Basically, I have 2 RichEdits and I want to save off the original index position of each character in RichEdit1, before I copy the contents over to RichEdit2 and do possible manipulations and changes on the contents (not important why I'm doing these manipulations). Anyway, as noted in my previous question above, I think I've discovered a little trick using the SelAttributes of RichEdit. My dilemma seemed to be that I needed a variable where I could "save" the original position index of each character in RichEdit1... something like having a "tag" property for each character in the RichEdit. of course, there is no individual "tag" property for each character, but I think I found the next best thing - I discovered that if I set the font of the RichEdit to the "default font" of Arial|20|Bold, then I could freely use and overwrite the NAME attribute of RichEdit and it would save whatever value I stored in there. If I program a MouseMove event to reflect the values of each character position in RichEdit2 in a hint, this seems to work perfectly for me. Perfect provided I use SelLength = 1 while I'm displaying the hint. If I set SelLength = 0, then the indexing seems to get thrown off by 1 starting at the 2nd character.
If this is confusing to this point, I've created a small test app and have attached it to this post... using it to illustrate should clear everything up. Here's how to use it:
- make sure the RadioGroup is set to "RichEdit2.SelLength = 1".
- in the 1st RichEdit field, type in some text like ABCDEFGH.
- click the downward "double arrow" button to copy the text to RichEdit2 underneath it. In this ButtonClick routine, I loop through each character in RichEdit2 and assign to RichEdit2.SelAttributes.Na
me property, the index position of the current character (ie. "1" for 1st character, "2" for 2nd character, and so on). This event also produces a "tracefile" to your default folder, outputting each character, SelStart value, index position, and NAME property value.... just to verify that everything is getting stored right... and it is.
- hover the mouse over each character in RichEdit2 from left to right and you'll see everything is correct - the hint shows the correct character being hovered over and the correct "Current Position" index and "Original Position" index.
- Now in the RadioGroup, select RichEdit2.SelLength = 0. Now hover again in RichEdit2 from left to right. You'll see that on the 2nd character 'B', the "OrigPos" index value gets out of step with the "CurrPos" index value. That is my dilemma - why does this happen? Changing RichEdit2.SelLength from 1 to 0 is the culprit, but why? Setting SelLength to 0 shouldn't make any difference. According to the Delphi documentation for SelStart:
"If there is no selected text, SelStart indicates the position of the cursor."
In my MouseMove event, I'm setting SelStart with the RichEdit2.SelStart := iCharIndex statement, so really, there should be no need for me to have to set SelLength to 1, is there? But it just won't work right if SelLength is 0.
Now you're probably asking, "well just leave SelLength at 1 and be done with it, if it works!". Well yes I could, but when Sellength is 1, the RichEdit MouseMove event produces an ugly "flicker" when you hover the mouse over it whilst the focus is on RichEdit2. If the focus is NOT on RichEdit2 and you hover the mouse over it, there is of course no flicker.
So my goal is to have the indexing working right (of course), AND to have no ugly flicker as the user hovers the mouse over RichEdit2 if the focus is in RichEdit2. Can someone offer some help on this brain-twister please?
P.S: I created the sample project in Delphi 7