Recent

Author Topic: Cursor position in TMemo like "gotoxy"  (Read 527 times)

pascal111

  • Sr. Member
  • ****
  • Posts: 420
  • Un trabajo en equipo para programas serias.
Cursor position in TMemo like "gotoxy"
« on: June 28, 2022, 12:15:28 am »
Can I determine cursor position in TMemo control like "gotoxy" statement in C:

Code: C  [Select][+][-]
  1.  
  2. main()
  3. {
  4.  
  5. gotoxy(1, 3);
  6. printf("This is 3rd line.\n");
  7.  
  8. }
  9.  
  10.  
« Last Edit: June 28, 2022, 04:31:24 pm by pascal111 »
La chose par la chose est rappelé.

bobby100

  • Full Member
  • ***
  • Posts: 100
    • Malzilla
Re: Cursor position in TMemo like "gotoxy"
« Reply #1 on: June 28, 2022, 12:35:47 am »
Did you try TMemo.CaretPos or TMemo.SelStart?
https://gitlab.com/bobby100 - my Lazarus components and units
https://sourceforge.net/u/boban_spasic/profile/ - my open source apps

https://malzilla.org/ - remainder at my previous life as a web security expert

pascal111

  • Sr. Member
  • ****
  • Posts: 420
  • Un trabajo en equipo para programas serias.
Re: Cursor position in TMemo like "gotoxy"
« Reply #2 on: June 28, 2022, 09:36:20 am »
"memo1.CaretPos.SetLocation" has no effect at all.

"memo1.SelStart" changed "x" of the cursor, but has no effect on "memo1.Lines.Add".


Code: Pascal  [Select][+][-]
  1. unit Unit1;
  2.  
  3. {$mode objfpc}{$H+}
  4.  
  5. interface
  6.  
  7. uses
  8.   Classes, SysUtils, Forms, Controls, Graphics, Dialogs, StdCtrls;
  9.  
  10. type
  11.  
  12.   { TForm1 }
  13.  
  14.   TForm1 = class(TForm)
  15.     Memo1: TMemo;
  16.     procedure FormCreate(Sender: TObject);
  17.     procedure Memo1Change(Sender: TObject);
  18.   private
  19.  
  20.   public
  21.  
  22.   end;
  23.  
  24. var
  25.   Form1: TForm1;
  26.  
  27. implementation
  28.  
  29. {$R *.lfm}
  30.  
  31. { TForm1 }
  32.  
  33.  
  34. procedure TForm1.FormCreate(Sender: TObject);
  35. begin
  36.  
  37.   memo1.SelStart:=4;
  38.   //memo1.CaretPos.SetLocation(1,3);
  39.   memo1.Lines.Add('Hello!');
  40. end;
  41.  
  42. end.
  43.  
  44.  
La chose par la chose est rappelé.

winni

  • Hero Member
  • *****
  • Posts: 3125
Re: Cursor position in TMemo like "gotoxy"
« Reply #3 on: June 28, 2022, 11:05:50 am »
Hi!

First you have to add the text,
then you have to set the position.
Code: Pascal  [Select][+][-]
  1. procedure TForm1.Button3Click(Sender: TObject);
  2. begin
  3. memo1.clear;
  4. memo1.Lines.add('Hello');
  5. memo1.Lines.add('World');
  6. memo1.Selstart := 7;
  7. memo1.SelLength:= 2;
  8. end;
  9.  

Winni

paweld

  • Sr. Member
  • ****
  • Posts: 410
Re: Cursor position in TMemo like "gotoxy"
« Reply #4 on: June 28, 2022, 11:08:46 am »
the coordinate you want to set must exist
Code: Pascal  [Select][+][-]
  1. procedure TForm1.Button1Click(Sender: TObject);
  2. var
  3.   p: TPoint;
  4. begin
  5.   //line 4, char 3
  6.   p := Point(2, 3);
  7.   if p.Y >= Memo1.Lines.Count then
  8.     p.Y := Memo1.Lines.Count - 1;
  9.   if p.X >= Length(Memo1.Lines[p.Y]) then
  10.     p.X := Length(Memo1.Lines[p.Y]) - 1;
  11.   Memo1.CaretPos := p;
  12.   Memo1.SetFocus;
  13. end;    
Best regards
paweld
---
Lazarus trunk / FPC stable

bobby100

  • Full Member
  • ***
  • Posts: 100
    • Malzilla
Re: Cursor position in TMemo like "gotoxy"
« Reply #5 on: June 28, 2022, 11:19:48 am »
TMemo.Lines.Add will always add the line at the end of the text.
You need to access the TMemo.Text and edit it as a string.
You can access the lines with TMemo.Lines[i: integer], but the line must be created before you can access it.
https://gitlab.com/bobby100 - my Lazarus components and units
https://sourceforge.net/u/boban_spasic/profile/ - my open source apps

https://malzilla.org/ - remainder at my previous life as a web security expert

pascal111

  • Sr. Member
  • ****
  • Posts: 420
  • Un trabajo en equipo para programas serias.
Re: Cursor position in TMemo like "gotoxy"
« Reply #6 on: June 28, 2022, 04:34:53 pm »
the coordinate you want to set must exist

I was avoiding that!
La chose par la chose est rappelé.

Edson

  • Hero Member
  • *****
  • Posts: 1242
Re: Cursor position in TMemo like "gotoxy"
« Reply #7 on: June 28, 2022, 04:46:37 pm »
the coordinate you want to set must exist
I was avoiding that!

Maybe you don't need TMemo. It's an oriented-line control like any Edition control. Even TSynEdit works that way.

If you want to locate text in any arbitrary position you can use a char matrix like a Terminal screen or DOS screen. I have simulated a Terminal screens using TSynEdit but it need to be carefully programated.

A Stringgrid must do the trick but it's a table control and behave like that.

Other option is to draw directly the text in the Canvas or using some graphic library. If you need to edit the text it's more complicated. You could manage dynamically TEdit controls.
Lazarus 2.0.10 - FPC 3.2.0 - x86_64-win64 on Windows 8

winni

  • Hero Member
  • *****
  • Posts: 3125
Re: Cursor position in TMemo like "gotoxy"
« Reply #8 on: June 28, 2022, 06:18:16 pm »
the coordinate you want to set must exist

I was avoiding that!


Hi!

Ugly workaround:

Initialize the memo with the needed amount of strings filled with spaces (#32)
Then set your Memo in the overwrite mode.

Now you got what you need.

Don't tell anybody that you got this trick from me.

Winni

pascal111

  • Sr. Member
  • ****
  • Posts: 420
  • Un trabajo en equipo para programas serias.
Re: Cursor position in TMemo like "gotoxy"
« Reply #9 on: June 29, 2022, 10:04:48 am »
the coordinate you want to set must exist

I was avoiding that!


Hi!

Ugly workaround:

Initialize the memo with the needed amount of strings filled with spaces (#32)
Then set your Memo in the overwrite mode.

Now you got what you need.

Don't tell anybody that you got this trick from me.

Winni

I have no choice other of this trick for that other solutions cost much.
La chose par la chose est rappelé.

 

TinyPortal © 2005-2018