Forum > Beginners

Slightly advanced example of "for in" construct for beginners

(1/1)

Ten_Mile_Hike:

--- Code: Text  [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} ---This is just an example of an easy way to load/sort/save multiple TMemos using "for-in". It comes in handyto shorten the code that needs to be written when you have 10-15 TMemos to deal with.I purposely didn't use "Try-Except" to simplify reading the source for the beginner.YES--- there are other ways to accomplish this. The purpose here is to familiarize the beginnerwith the "for in" construct especially as pertains to a dynamic array. 



--- Code: Pascal  [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} ---unit Unit1;{$mode objfpc}{$H+}interfaceuses Classes, SysUtils, Forms, Controls, Graphics, Dialogs, Math, StdCtrls;type  TMemoArray = array of TMemo; //Type declaration  { TForm1 }  TForm1 = class(TForm)    M1, M2, M3, M4: TMemo;    procedure FormCreate(Sender: TObject);    procedure FormCloseQuery(Sender: TObject; var CanClose: boolean);  private    FMemos: TMemoArray;  //Declare a dynamic array of TMemos as a field of Form1  end;var  Form1: TForm1;implementation  {$R *.lfm} {Load any existing text into the TMemos}procedure TForm1.FormCreate(Sender: TObject);var  M: TMemo;begin  FMemos := [M1, M2, M3, M4]; //Initialize a dynamic array {FMemos is declared as a Private field of TForm1 and initialized here. Because it is a field of the form, its value remains available to all methods of this form instance until the form is destroyed. "Somewhat" similar to a global variable}   for M in FMemos do if fileexists(M.Name) then M.Lines.LoadFromFile(M.Name);end; {Sort and Save the current contents of all TMemos assigned to the FMemos array}procedure TForm1.FormCloseQuery(Sender: TObject; var CanClose: boolean);var  M: TMemo;  SL: TStringList;begin  SL := TStringList.Create;  for M in FMemos do  //FMemos was initialized in FormCreate with [M1, M2, M3, M4].  begin    SL.Assign(M.Lines);    SL.Sort;    M.Lines.Assign(SL);    M.Lines.SaveToFile(M.Name);  end;  SL.Free;  CanClose := True;end;end.

n7800:
Off-topic, but an important caveat regarding TMemo. Do not use text in "TMemo.Lines" as this is display text and contains word wrapping (if "WordWrap" is enabled). If there are lines longer than the TMemo width, unnecessary word wrap will appear when saving to a file. The original text is contained in "TMemo.Text".

Ten_Mile_Hike:
@n7800
Thanks for the heads up. Here is the behavior that you warned about.

--- Code: Pascal  [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} ---procedure TForm1.FormClick(Sender:TObject);begin   memo1.WordWrap:=True;   memo1.lines.Text:='XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX';   memo1.Refresh;   Form1.Caption:=memo1.Lines.count.ToString; // Displays "4"   Memo1.Lines.SaveToFile('wrapped.txt');          // Saved file has 4 lines    sleep(2000);    memo1.WordWrap:=False;   memo1.lines.Text:='XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX';   memo1.Refresh;   Form1.Caption:=memo1.Lines.count.ToString; // Displays "1"   Memo1.Lines.SaveToFile('unwrapped.txt');       // Saved file has 1 lineend; 
if wordwrap=True and AND you want to preserve the original text without the extra lines created by wordwrap use:

   MyStringlist.text:=Memo1.Text;
   MyStringlist.SaveToFile('Memo1.txt');

     *** OR MORE SIMPLY ***
   Use Memo1.wordwrap:=False before using memo1.lines.savetofile('Memo1.txt')

 

n7800:

--- Quote from: Ten_Mile_Hike on July 02, 2026, 03:47:44 pm ---     *** OR MORE SIMPLY ***
   Use Memo1.wordwrap:=False before using memo1.lines.savetofile('Memo1.txt')

--- End quote ---

Turning WordWrap on and off can affect scrolling in text (even if it doesn't affect your OS, it's important to remember that it's cross-platform), and for large text, re-arranging it can even take time. Therefore, we have to resort to inconvenient methods ((

Navigation

[0] Message Index

Go to full version