* * *

Author Topic: TStringList  (Read 1055 times)

samoraj

  • New member
  • *
  • Posts: 18
TStringList
« on: February 10, 2018, 02:03:28 am »
Hello, i want to change 2 lines in TStringList. i have select line in iLine If the selected line is 20, go to line 19 and line 19 to line 20.
how do I do that ?

Code: Pascal  [Select]
  1.     begin
  2.          sl:=TStringList.Create;
  3.          sl.LoadFromFile('\\RADOSLAV\print\model.txt');
  4.  
  5.          sl.SaveToFile('\\RADOSLAV\print\model.txt');
  6.          sl.free
  7.     end;        
  8.  

engkin

  • Hero Member
  • *****
  • Posts: 1971
Re: TStringList
« Reply #1 on: February 10, 2018, 02:18:05 am »
Code: Pascal  [Select]
  1. var   tmp: string;
  2. begin
  3.      sl:=TStringList.Create;
  4.      sl.LoadFromFile('\\RADOSLAV\print\model.txt');
  5.  
  6.      if iLine=20 then begin
  7.        tmp := sl[iLine];
  8.        sl[iLine] := sl[iLine-1];
  9.        sl[iLine-1] := tmp;
  10.      end;
  11.  
  12.      sl.SaveToFile('\\RADOSLAV\print\model.txt');
  13.      sl.free;

samoraj

  • New member
  • *
  • Posts: 18
Re: TStringList
« Reply #2 on: February 10, 2018, 02:44:59 am »
My code:
Code: Pascal  [Select]
  1. var   tmp: string;
  2.     begin
  3.          sl:=TStringList.Create;
  4.          sl.LoadFromFile('\\RADOSLAV\print\model.txt');
  5.          tmp := sl[iLine];
  6.          sl[iLine] := sl[iLine-1];
  7.          sl[iLine-1] := tmp;
  8.          sl.SaveToFile('\\RADOSLAV\print\model.txt');
  9.          sl.free
  10.     end;          
  11.  

but selected line goin down not up ...

(http://alfa.kachi-snimka.info/thumbs-2013/jdv1518227295z.png)

(http://alfa.kachi-snimka.info/thumbs-2013/bfi1518227430n.png)
« Last Edit: February 10, 2018, 02:51:19 am by samoraj »

samoraj

  • New member
  • *
  • Posts: 18
Re: TStringList
« Reply #3 on: February 10, 2018, 03:29:36 am »
My bad sorry. iLine selected next line and this is the problem :)
Thanks engkin !

Bart

  • Hero Member
  • *****
  • Posts: 2956
    • Bart en Mariska's Webstek
Re: TStringList
« Reply #4 on: February 10, 2018, 02:11:08 pm »
Doesn't TStringList have an Exchange method?

Bart

engkin

  • Hero Member
  • *****
  • Posts: 1971
Re: TStringList
« Reply #5 on: February 10, 2018, 02:41:44 pm »
Yes:
Code: Pascal  [Select]
  1.   sl.Exchange(iLine, iLine-1);

Bart

  • Hero Member
  • *****
  • Posts: 2956
    • Bart en Mariska's Webstek
Re: TStringList
« Reply #6 on: February 10, 2018, 03:20:21 pm »

jamie

  • Hero Member
  • *****
  • Posts: 708
Re: TStringList
« Reply #7 on: February 10, 2018, 04:19:38 pm »
having a little difficulties understanding the problem but I don't think the problem is here.

 He is loading the file, flipping two lines and resaving it, then showing his results elsewhere where the
display index is still where it was the last time.
 
 Changing the strings here and  have it reflect position change in remote code or even other components isn't
going to change the cursor viewing index that I know of.

 When the poster states doing down, I get the impression the line got switched and the cursor didn't move up
to reflect that. if that being the case, its not fixable here  ;)

engkin

  • Hero Member
  • *****
  • Posts: 1971
Re: TStringList
« Reply #8 on: February 10, 2018, 05:01:39 pm »
The code he presented was for testing. The two images he showed later were from opening the file, before and after the test, in Windows Notepad. He made the typical mistake to start from 1, while TStringList starts from 0.

 

Recent

Get Lazarus at SourceForge.net. Fast, secure and Free Open Source software downloads Open Hub project report for Lazarus