Recent

Author Topic: Something kind of funny about TMemo's  (Read 1539 times)

OC DelGuy

  • Full Member
  • ***
  • Posts: 208
  • 123
Something kind of funny about TMemo's
« on: April 28, 2025, 08:39:12 pm »
So, I was writing this little bit of code when I noticed...  Well, I really don't know what the heck I noticed, so we'll just say I noticed "something kind of funny".  When you save a TMemo to file, it saves it exactly as it shows on the screen.  Take, for example, the snippet below (the complete code is at the bottom).
Code: Pascal  [Select][+][-]
  1.         For i := 0 to Transcript.Count - 1 Do SinStr := SinStr + ' ' + Transcript[i];  // Make Transcript into one large string.
  2.         Memo1.Text := SinStr;
  3.       Finally
  4.         Input.Free;
  5.         Transcript.Free;
  6.       End;
  7.     End;
  8.  
  9.   Procedure TForm1.SaveClick(Sender: TObject);
  10.     Var
  11.       FileName : String = 'Transcript1.txt';
  12.     Begin
  13.       Memo1.Lines.SaveToFile(FileName);
  14.       ShowMessage('Transcript saved successfully to: ' + FileName);
  15.     End;
  16.  


  • I load a bunch of strings into a TStringList.
  • I take the TStringList and concatenate all the strings into one real long string.
  • I put the one big string into a TMemo.
  • Lastly, I save the contents of the TMemo in a file.
So when I loaded the file 'Transcript1.txt' to Grammarly, I noticed it was back to being a bunch of little strings!  Grammarly didn't like that.  Now don't ask me how, but I found out that depending on the width of the TMemo, that determines the length of the strings in the file!!  Whaaaat??

Now, I realize what's going on with the Memo1.Lines.SaveToFile(FileName); (Thank God, I at least know that).
I'm guessing that I can save the SinStr instead of the Memo1.Lines and (I didn't really try this, so I'm guessing) it'll be one string in the file.  The only way I know how to do that is by declaring SinStr as a global variable (because the concatenating and the saving are in two different procedures).  But, from all the examples I've seen, global variables are a "no, no" (I think I may have even read it somewhere too).
I tried Memo1.Text.SaveToFile(FileName);, but that just gave me an "Illegal qualifier" error.




So, how can I save the contents of a TMemo as one string in a file?
--or--
Is there a way to save a variable in the class declaration?




Code: Pascal  [Select][+][-]
  1. Unit Transkripts;
  2. {$mode objfpc}{$H+}   // Converts a file or pasted text into just the odd lines or just the even lines.
  3. Interface
  4. Uses Classes, Dialogs, Forms, Graphics, StdCtrls, ExtCtrls, Buttons;  // SysUtils, Controls,
  5. Type
  6.   { TForm1 }   // Use a TMemo to paste in the text to be converted.
  7.  
  8.   TForm1 = class(TForm)
  9.     Save: TBitBtn;
  10.     Convert: TButton;     // Starts the conversion.
  11.     Label1: TLabel;       // Just shows the title of this application.
  12.     Memo1: TMemo;         // Shows the contents of the file.  Maybe figure a way to switch from input file to output file.
  13.     Panel1: TPanel;       // Green if loaded and saved successfully.
  14.     OddEven: TToggleBox;  // Toggle between keeping odd or even Lines.
  15.     procedure ConvertClick(Sender: TObject);
  16.     procedure FormCreate(Sender: TObject);
  17.     procedure FormResize(Sender: TObject);
  18.     procedure OddEvenChange(Sender: TObject);
  19.     procedure OddEvenClick(Sender: TObject);
  20.     procedure SaveClick(Sender: TObject);
  21.   Private
  22.     Procedure Load;
  23.   Public
  24.   End;
  25.  
  26. { TODO -o'Del Guy' -cCoding : Add mechanism for detecting text in another Memo and deciding to convert the Memo text
  27.   or load file if Memo is empty }
  28.  
  29.  
  30. Var
  31.   Form1: TForm1;
  32.  
  33. Implementation
  34. {$R *.lfm}
  35. { TForm1 }
  36.  
  37.   Procedure TForm1.FormCreate(Sender: TObject);
  38.     Begin
  39.     End;
  40.  
  41.   Procedure TForm1.OddEvenChange(Sender: TObject);
  42.     Begin
  43.     End;
  44.  
  45.   Procedure TForm1.FormResize(Sender: TObject);
  46.     Begin
  47.       Memo1.Height := Form1.Height - 64
  48.     End;
  49.  
  50.   Procedure TForm1.ConvertClick(Sender: TObject);
  51.     Begin
  52.       Load;
  53.     End;
  54.  
  55.   Procedure TForm1.OddEvenClick(Sender: TObject);
  56.     Begin
  57.       If OddEven.Checked Then OddEven.Caption := 'Odd' Else OddEven.Caption := 'Even';
  58.       If OddEven.Checked Then Panel1.Color := clRed Else Panel1.Color := clLime
  59.     End;
  60.  
  61.   Procedure TForm1.Load;
  62.     Var
  63.       Input : TStringList;       // Holds the transcript before converting.
  64.       Transcript : TStringList;  // Holds the actual transcript after conversion
  65.       SinStr : String;           // Holds the transcript as a single string.
  66.       i : Integer;               // Iterator Variable
  67.     Begin
  68.       SinStr := '';
  69.       Input := TStringList.Create;
  70.       Transcript := TStringList.Create;
  71.       Input.LoadFromFile('Transcript.txt');
  72.       Try
  73.         For i := 1 to Input.Count - 1 Do
  74.           Begin
  75.             If not OddEven.Checked AND (i mod 2 = 1) Then Transcript.Add(Input[i]);  // Load only the odd or only the even lines.
  76.           End;
  77.         For i := 0 to Transcript.Count - 1 Do SinStr := SinStr + ' ' + Transcript[i];  // Make Transcript into one large string.
  78.         Memo1.Text := SinStr;
  79.       Finally
  80.         Input.Free;
  81.         Transcript.Free;
  82.       End;
  83.     End;
  84.  
  85.   Procedure TForm1.SaveClick(Sender: TObject);
  86.     Var
  87.       FileName : String = 'Transcript1.txt';
  88.     Begin
  89.       Memo1.Lines.SaveToFile(FileName);
  90.       ShowMessage('Transcript saved successfully to: ' + FileName);
  91.     End;
  92.  
  93.  
  94. end.
Free Pascal Lazarus Version #: 2.2.4
Date: 24 SEP 2022
FPC Version: 3.2.2
Revision: Lazarus_2_2_4
x86_64-win64-win32/win64

cdbc

  • Hero Member
  • *****
  • Posts: 2150
    • http://www.cdbc.dk
Re: Something kind of funny about TMemo's
« Reply #1 on: April 28, 2025, 09:00:40 pm »
Hi
You could try like this:
Code: Pascal  [Select][+][-]
  1. var
  2.   fs: TFileStream;
  3. ...
  4.   // Make Transcript into one large string
  5.   For i := 0 to Transcript.Count - 1 Do SinStr := SinStr + ' ' + Transcript[i];
  6.   { write that 1 string to a filestream, in one fell swoop }
  7.   fs:= TFileStream.Create('Transcript1.txt',fmCreate or fmWrite or fmShareDenyNone);
  8.   try
  9.     fs.write(pointer(SinStr)^,length(SinStr));
  10.   finally
  11.     fs.free;
  12.   end;
  13.  
The above should save the string as one line...
Regards Benny
If it ain't broke, don't fix it ;)
PCLinuxOS(rolling release) 64bit -> KDE5 -> FPC 3.2.2 -> Lazarus 3.6 up until Jan 2024 from then on it's both above &: KDE5/QT5 -> FPC 3.3.1 -> Lazarus 4.99

OC DelGuy

  • Full Member
  • ***
  • Posts: 208
  • 123
Re: Something kind of funny about TMemo's
« Reply #2 on: April 28, 2025, 09:43:40 pm »
Hi
You could try like this:
Code: Pascal  [Select][+][-]
  1. var
  2.   fs: TFileStream;
  3. ...
  4.   // Make Transcript into one large string
  5.   For i := 0 to Transcript.Count - 1 Do SinStr := SinStr + ' ' + Transcript[i];
  6.   { write that 1 string to a filestream, in one fell swoop }
  7.   fs:= TFileStream.Create('Transcript1.txt',fmCreate or fmWrite or fmShareDenyNone);
  8.   try
  9.     fs.write(pointer(SinStr)^,length(SinStr));
  10.   finally
  11.     fs.free;
  12.   end;
  13.  
The above should save the string as one line...
Regards Benny

Yes, but, the save and the concatenation are in different procedures.  The reason is because I want to see the result (make sure it's cool) and THEN save.
Free Pascal Lazarus Version #: 2.2.4
Date: 24 SEP 2022
FPC Version: 3.2.2
Revision: Lazarus_2_2_4
x86_64-win64-win32/win64

Bart

  • Hero Member
  • *****
  • Posts: 5564
    • Bart en Mariska's Webstek
Re: Something kind of funny about TMemo's
« Reply #3 on: April 28, 2025, 10:23:09 pm »
Turn off the TMemo's Wordwrap.

Bart

OC DelGuy

  • Full Member
  • ***
  • Posts: 208
  • 123
Re: Something kind of funny about TMemo's
« Reply #4 on: April 29, 2025, 01:55:54 am »
Turn off the TMemo's Wordwrap.

Bart

Yep, thought of that.  But then I won't be able to check the result (make sure it's cool) BEFORE I save.
Free Pascal Lazarus Version #: 2.2.4
Date: 24 SEP 2022
FPC Version: 3.2.2
Revision: Lazarus_2_2_4
x86_64-win64-win32/win64

OC DelGuy

  • Full Member
  • ***
  • Posts: 208
  • 123
Re: Something kind of funny about TMemo's
« Reply #5 on: April 29, 2025, 02:12:27 am »
Turn off the TMemo's Wordwrap.

Bart

Yep, thought of that.  But then I won't be able to check the result (make sure it's cool) BEFORE I save.
Oh, just thought about what you said!  I first thought you meant in the Object Inspector.  But then it hit me...  Do it in the code!  So I did.  I added


Code: Pascal  [Select][+][-]
  1.       Memo1.Wordwrap := False;
  2.       Memo1.Lines.SaveToFile(FileName);
  3.       Memo1.Wordwrap := True;
  4.       ShowMessage('Transcript saved successfully to: ' + FileName);
  5.  
Free Pascal Lazarus Version #: 2.2.4
Date: 24 SEP 2022
FPC Version: 3.2.2
Revision: Lazarus_2_2_4
x86_64-win64-win32/win64

OC DelGuy

  • Full Member
  • ***
  • Posts: 208
  • 123
Re: Something kind of funny about TMemo's
« Reply #6 on: April 29, 2025, 02:15:21 am »
One more small step.  How do I get past the 1000 character line limit in the TMemo?
The file is still split in 1000 character strings.
Free Pascal Lazarus Version #: 2.2.4
Date: 24 SEP 2022
FPC Version: 3.2.2
Revision: Lazarus_2_2_4
x86_64-win64-win32/win64

egsuh

  • Hero Member
  • *****
  • Posts: 1600
Re: Something kind of funny about TMemo's
« Reply #7 on: April 29, 2025, 06:58:21 am »

Code: Pascal  [Select][+][-]
  1. var
  2.     f: TextFile;
  3.     filename: string;
  4. begin
  5.     AssignFile(f, filename);
  6.     Reset(f);
  7.     writeln(f, memo1.lines.text);
  8.     Closefile(f);
  9. end;
  10.  

Thaddy

  • Hero Member
  • *****
  • Posts: 16982
  • Ceterum censeo Trump esse delendam
Re: Something kind of funny about TMemo's
« Reply #8 on: April 29, 2025, 02:29:13 pm »
  • I take the TStringList and concatenate all the strings into one real long string.
You mean TStringlist.Text I hope? The concatination is done for you.
https://www.freepascal.org/docs-html/rtl/classes/tstrings.text.html
Due to censorship, I changed this to "Nelly the Elephant". Keeps the message clear.

Bart

  • Hero Member
  • *****
  • Posts: 5564
    • Bart en Mariska's Webstek
Re: Something kind of funny about TMemo's
« Reply #9 on: April 29, 2025, 03:40:38 pm »
One more small step.  How do I get past the 1000 character line limit in the TMemo?
The file is still split in 1000 character strings.

This might be a limitation of your widgetset.
If you need such long line, use a TSynEdit instead.
That would sove some of your other problems as well

Bart

Thaddy

  • Hero Member
  • *****
  • Posts: 16982
  • Ceterum censeo Trump esse delendam
Re: Something kind of funny about TMemo's
« Reply #10 on: April 29, 2025, 03:49:09 pm »
The raw windows code should have a property bottomless.
Due to censorship, I changed this to "Nelly the Elephant". Keeps the message clear.

OC DelGuy

  • Full Member
  • ***
  • Posts: 208
  • 123
Re: Something kind of funny about TMemo's
« Reply #11 on: May 03, 2025, 08:15:01 am »
  • I take the TStringList and concatenate all the strings into one real long string.
You mean TStringlist.Text I hope? The concatination is done for you.
https://www.freepascal.org/docs-html/rtl/classes/tstrings.text.html

No, I tried TStringlist.Text and got an error.  I forgot what error it was, but I did try it.  Your link points to TStrings.Text.  That, I did not try as I don't know how TStrings work.  I'm barely learning the TStringList.


One more small step.  How do I get past the 1000 character line limit in the TMemo?
The file is still split in 1000 character strings.

This might be a limitation of your widgetset.
If you need such long line, use a TSynEdit instead.
That would sove some of your other problems as well

Bart
I still have no clear idea of what widgetsets are. :o  I have not looked into it. :(  I wasn't even aware I had one! :D  I'm still mastering basic stuff.
I want a long string because when I feed a bunch of little strings to Grammarly, it freaks!  but when I feed it long strings, it fixes the grammatical errors fine.  The transcripts are of lectures that usually last like 20 minutes to one hour.  A few even last 2 hours.  And they come with no capitalization or punctuation.  So, yeah, I need to be able to feed it to Grammarly to make it comprehensible!  ;D
I've seen the TSynEdit control, but I haven't looked into that either.  If it's kind of interchangeable with the TMemo or TEdit, then I will take a look.  If it's complicated then I might just wait on tackling advanced stuff.
Free Pascal Lazarus Version #: 2.2.4
Date: 24 SEP 2022
FPC Version: 3.2.2
Revision: Lazarus_2_2_4
x86_64-win64-win32/win64

cdbc

  • Hero Member
  • *****
  • Posts: 2150
    • http://www.cdbc.dk
Re: Something kind of funny about TMemo's
« Reply #12 on: May 03, 2025, 08:35:47 am »
Hi
Quote
Your link points to TStrings.Text.  That, I did not try as I don't know how TStrings work.  I'm barely learning the TStringList.
No worries mate, they're the same property, namely the strings in the list, concatenated together with 'LineEnding'(LF/CRLF) as delimiter/separator, to 1 long string.
I'd like to think, that 'Grammarly' doesn't care about CRLF...
...but if it does, the use my solution, only with a 'TMemoryStream' instead, 'cause e.g.: TMemo can easily load from stream.
Regards Benny
If it ain't broke, don't fix it ;)
PCLinuxOS(rolling release) 64bit -> KDE5 -> FPC 3.2.2 -> Lazarus 3.6 up until Jan 2024 from then on it's both above &: KDE5/QT5 -> FPC 3.3.1 -> Lazarus 4.99

jamie

  • Hero Member
  • *****
  • Posts: 6892
Re: Something kind of funny about TMemo's
« Reply #13 on: May 03, 2025, 08:53:49 pm »
You could always make a LCL version of this, which is Delphi currently.

https://github.com/frankwu-delphi/TTextEditor/blob/main/README.md
The only true wisdom is knowing you know nothing

d2010

  • Full Member
  • ***
  • Posts: 161
Re: Something kind of funny about TMemo's
« Reply #14 on: May 04, 2025, 06:44:44 am »
You could always make a LCL version of this, which is Delphi currently.

https://github.com/frankwu-delphi/TTextEditor/blob/main/README.md
I got this error.  Please, You give a a new-version demo,editor  with SynEdit.pas inside "Lazarus". Can you save the Text as Html inside SynEdit?
TTextTeditor is worst in "Lazarus", a new -quite demo, small , but 100% for "Lazarus4.0"
Code: [Select]
TTextEditorDemo.Form.Main.pas(8,3) Error: Cannot find TextEditor used by TTextEditorDemo.Form.Main of the Project Inspector.
uses
  Messages, Windows, Classes, Generics.Collections, SysUtils,
  Variants, Controls, Dialogs, ExtCtrls, Forms, Graphics, ImgList, StdCtrls,
  TextEditor, TextEditor.Types;   
« Last Edit: May 04, 2025, 07:05:34 am by d2010 »

 

TinyPortal © 2005-2018