In my VCL project, I have a TMemo
with the following text (|
is the caret):
| | |Hello world | | | | |test | | | | |
When I press the Down button, the caret moves here:
| | |Hello world | || | |test | | | | |
What I need is for it to move here instead:
| | |Hello world | | | | |test | | | | |
I think you would find it instructive to devise your own solution for this. The default behaviour for a TMemo
responding to the Down key depends on how many characters there are on the next line. If there are at least as many on the next line as there are on the current line, the caret will stay in the same column number.
So a simple solution might be
When you detect an OnKeyDown
event caused by the Down key, check the number of characters in the next line, and if it is fewer than the number of characters in the current line to the left of the caret, right-pad the line below with spaces until the numbers of characters are equal. The on-screen appearance will only exactly maintain the column position if the memo uses a fixed-point font; with a proportional font, the cursor will still "wiggle" a bit left and right because spaces are narrower than most other characters.
Of course, you would need to do this for the Up key too, and it's up to you whether you do similar for mouse clicks.
With a bit of googling you can easily find Delphi code to detect the current line and column number of the caret in a TMemo
.