[SOLVED] TMemo and splitting text lazarus / 31-LCL ---------------------------------------------------------------------- lazypas · 2020-05-04 02:26 Hello, Thank you all for taking the time to read my post. Simple question, what is the best way to split the text of one TMemo into another TMemo. Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i procedure TForm.ButtonSplit(Sender: TObject);var  textBefore, textAfter: string;  textCursorPosition: Integer; begin  textCursorPosition := Memo1.SelStart;  textBefore := Memo1.Lines.Text.Substring(0,textCursorPosition);  textAfter := Memo1.Lines.Text.Substring(textCursorPosition);   Memo1.Text := textBefore;  Memo2.Text := textAfter;end;  The above code does split the text of Memo1 between itself and Memo2. However, it occurs at some 'random' position before the position of the cursor Memo1. Is there an offset I am not aware of? Many thanks! ---------------------------------------------------------------------- lazypas · 2020-05-04 02:58 Solved Solution below where linesBefore and linesAfter are of the TStringList type. Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i procedure TForm.ButtonSplit(Sender: TObject);var  textBefore, textAfter: string;  i: integer;  linesBefore, linesAfter: TStringList;begin  linesBefore := TStringList.Create;  linesAfter := TStringList.Create;   for i := 0 to Memo1.Lines.Count-1 do  begin    if ( i < Memo1.CaretPos.y ) then    begin      linesBefore.Add( Memo1.Lines[i] );    end    else if ( i = Memo1.CaretPos.y ) then    begin      textBefore := Memo1.Lines[i].Substring(0, Memo1.CaretPos.x);      textAfter := Memo1.Lines[i].Substring(Memo1.CaretPos.x);       linesBefore.Add(textBefore);      linesAfter.Add(textAfter);    end  else    begin      linesAfter.Add( Memo1.Lines[i] );    end;  end;   Memo1.Text := linesBefore.Text;  Memo2.Text := linesAfter.Text;end; ---------------------------------------------------------------------- ASerge · 2020-05-04 16:38 Quote from: lazypas on May 04, 2020, 04:26:10 amHowever, it occurs at some 'random' position before the position of the cursor Memo1. Is there an offset I am not aware of? Maybe you shouldn't use line splitting at all. Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i procedure TForm1.Button1Click(Sender: TObject);var  FullText: string;  SplitPos: Integer;begin  FullText := Memo1.Text;  SplitPos := Memo1.SelStart;  if SplitPos < 0 then    SplitPos := Length(FullText);  Memo1.Text := Copy(FullText, 1, SplitPos);  Memo2.Text := Copy(FullText, SplitPos + 1, MaxInt);end;