Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Working with text in TMemo

Status
Not open for further replies.

andjem1

Technical User
Jul 16, 2007
16
GB
Hi
I've built a simple VCL that modifies a line of text (CNC G-Code) and displays result in a label. Now that the code is working ok, I want to use the same code to modify a g-code file loaded into a tmemo.
I am having some trouble wih this. How do I get the program to move line by line through the file modifing each line in turn ?
I tried to load the contents of the memo to a Tstringlist without success but I don't know whether or not this is the right approach. My failed attempt below.
Any help would be appreciated. Thanks.

Using Turbo Delphi.

Code:
procedure TForm1.btnRunClick(Sender: TObject);
var
   source : tstringlist;
   I: Integer;
   OldValue, NewValue : String;
   Radius: Extended;
begin
    source := tstringlist.Create;
    Source := Memo1.text;

    Radius := StrtoFloat(Edit1.text);

  if source = '' then
     Begin
     Showmessage('No File loaded.');
     Exit;
     end
   else

  for i := 0 to Memo1.Lines.Count-1 do
   // Following code works OK when taking one line of text from an editbox
   // but how to deal with every line in turn loaded into a TMemo ?
   begin
     if ansicontainsstr (Source,'X') then
      if HasUnwanted(['A','B','Y','F','H','D'],Source) then
        begin
         OldValue := GetXMove(Source);
         NewValue := 'B' + Convert(OldValue,Radius);
        end
      else
        begin
         OldValue := AnsiMidstr(source,(ansipos('X',Source)),maxint);
         NewValue := 'B' + Convert(OldValue,Radius);
        end;

      NewValue := AnsiReplaceStr(source,(OldValue), (NewValue));

     end;
end;
 
the memo already contains a TStringList (Lines)
so no need to use source

Code:
procedure TForm1.btnRunClick(Sender: TObject);
var
   I: Integer;
   OldValue, NewValue : String;
   Radius: Extended;
   Line : string;
begin
  if Memo1.text = '' then
     Begin
     Showmessage('No File loaded.');
     Exit;
     end
   else

  for i := 0 to Memo1.Lines.Count-1 do
   begin
     Line := Memo1.Lines[i]; // load current line
     if ansicontainsstr (Line,'X') then
      if HasUnwanted(['A','B','Y','F','H','D'],Line) then
        begin
         OldValue := GetXMove(Line);
         NewValue := 'B' + Convert(OldValue,Radius);
        end
      else
        begin
         OldValue := AnsiMidstr(Line,(ansipos('X',Line)),maxint);
         NewValue := 'B' + Convert(OldValue,Radius);
        end;

      NewValue := AnsiReplaceStr(Line,(OldValue), (NewValue));
      Memo1.Lines[i] := NewValue; // write converted line back into memo
     end;
end;

-----------------------------------------------------
What You See Is What You Get
Never underestimate tha powah of tha google!
 
whosrdaddy, Thanks for this. I suppose I ought to be a bit embarrassed for asking such a basic question.
 
Don't be embarrassed, we all have to start somewhere.

I can advise you to read a good book about delphi/pascal.

look here for further information: faq102-2794

Cheers,
Daddy

-----------------------------------------------------
What You See Is What You Get
Never underestimate tha powah of tha google!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top