Articles   Members Online:
-Article/Tip Search
-News Group Search over 21 Million news group articles.
-Delphi/Pascal
-CBuilder/C++
-C#Builder/C#
-JBuilder/Java
-Kylix
Member Area
-Home
-Account Center
-Top 10 NEW!!
-Submit Article/Tip
-Forums Upgraded!!
-My Articles
-Edit Information
-Login/Logout
-Become a Member
-Why sign up!
-Newsletter
-Chat Online!
-Indexes NEW!!
Employment
-Build your resume
-Find a job
-Post a job
-Resume Search
Contacts
-Contacts
-Feedbacks
-Link to us
-Privacy/Disclaimer
Embarcadero
Visit Embarcadero
Embarcadero Community
JEDI
Links
How to delete lines from a text file Turn on/off line numbers in source code. Switch to Orginial background IDE or DSP color Comment or reply to this aritlce/tip for discussion. Bookmark this article to my favorite article(s). Print this article
19-Oct-02
Category
Files Operation
Language
Delphi 2.x
Views
118
User Rating
No Votes
# Votes
0
Replies
0
Publisher:
DSP, Administrator
Reference URL:
DKB
			Author: Jonas Bilinkevicius

How can I open a file and add lines which start with PIL to a listbox. When I 
delete the appropriate line in the listbox, the line in the file should also be 
deleted.

Answer:

Load the complete file into a TStringList instance. Then iterate over the Items in 
the list and use the Pos function to check if the line starts with PIL, if it does 
you add it to the listbox. When time comes to save the possibly changed file back 
you again walk over the items in the listbox, but this times you do it from last to 
first. For each line that starts with PIL you use the listbox.items.indexof method 
to see if it is in the listbox, if not you delete it from the stringlist. Then you 
write the stringlist back to file. Example:

In the forms private section you declare a field
1   
2   FFilelines: TStringList;
3   
4   //In the forms OnCreate event you create this list:
5   
6   FFilelines := TStringList.Create;
7   
8   //In the forms OnDestroy event you destroy the list:
9   
10  FFilelines.Free;


On file load you do this:
11  
12  FFilelines.Loadfromfile(filename);
13  listbox1.items.beginupdate;
14  try
15    listbox1.clear;
16    for i := 0 to FFilelines.count - 1 do
17      if Pos('PIL', FFilelines[i]) = 1 then
18        listbox1.items.add(FFilelines[i]);
19  finally
20    listbox1.items.endupdate;
21  end;
22  
23  //To save the file you do this:
24  
25  for i := FFilelines.count - 1 downto 0 do
26    if Pos('PIL', FFilelines[i]) = 1 then
27      if listbox1.items.indexof(FFilelines[i]) < 0 then
28        FFilelines.Delete(i);
29  FFilelines.SaveToFile(filename);


			
Vote: How useful do you find this Article/Tip?
Bad Excellent
1 2 3 4 5 6 7 8 9 10

 

Advertisement
Share this page
Advertisement
Download from Google

Copyright © Mendozi Enterprises LLC