0

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               |  
|                   |  
|                   | 
Tmc
  • 143
  • 1
  • 8
  • 1
    I think you'll need to explain a bit more about the problem you are having - it's hard to tell from your three "pictures". Also, are you asking about a VCL project or a FireMonkey one? – MartynA Jun 14 '16 at 17:04
  • I try to explain again – Tmc Jun 14 '16 at 17:05
  • 1
    So, what you want is that when you press the `Down` key, the cursor moves to the line below but stays in the same column, rather than moving to the lefthand column as it normally does. Is that correct? If you say yes, I can edit your q to make this clearer if you like. – MartynA Jun 14 '16 at 17:16
  • Aside from all other comments, explaining why you need this behavior might allow others to offer up other solutions. – John Easley Jun 14 '16 at 17:36
  • Also consider that this is only ideal for certain types of fonts where all characters consume the same width. Fonts where characters vary in width will make no sense for such a thing. – Jerry Dodge Jun 14 '16 at 17:43
  • @MartynA was just that – Tmc Jun 14 '16 at 17:49
  • @MartynA Thanks for the help – Tmc Jun 14 '16 at 18:21

2 Answers2

7

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.

Remy Lebeau
  • 555,201
  • 31
  • 458
  • 770
MartynA
  • 30,454
  • 4
  • 32
  • 73
  • 2
    What about selecting the line with the mouse? You'd think it would be a better idea filling spaces to empty lines while text is inserted. But then what would you do if the user selects a *spaced* line and deletes the selection? I don't think forcing this behavior is a workable idea. – Sertac Akyuz Jun 14 '16 at 18:08
  • @SertacAkyuz: With respect, I took the OP to mean what happens for key presses - the mouse is a different kettle of fish (or perhaps I mean can of worms?), annd what he does for that is up to him. As to workable, the IDE editor seems to do what the OP wants but iirc (possibly not), that's based on a RichEdit? – MartynA Jun 14 '16 at 18:12
  • @MartynA I'll try to do what you say thanks for the tip – Tmc Jun 14 '16 at 18:24
  • The IDE editor is nothing like a richedit or an edit control. Also it doesn't pad spaces. ... I'm not sure poster has even considered that there can be a mouse... – Sertac Akyuz Jun 14 '16 at 18:33
  • The IDE's editor is a custom control written in C++. It's proprietary to the IDE, and has nothing to do with any of the Windows standard controls including RichEdit. – Ken White Jun 14 '16 at 22:41
  • @KenWhite: Yes, I found an ancient Borland NG thread where this was mentioned. – MartynA Jun 15 '16 at 05:57
4

The TMemo in Delphi merely wraps the internal control within Windows itself - which does not natively support such functionality that you desire. It may be possible to modify it to behave in such manner, but easier to use something which is already designed for this. One largely popular control which can do this is the SynEdit which is geared towards code editors and syntax highlighting. It might do much more than you need, but it solves what you're looking for.

Jerry Dodge
  • 26,858
  • 31
  • 155
  • 327