Freddy1990
2006-11-22 17:30:02
In this tutorial we're going to discuss the TStringList component.
This component is really recommended for people who want to save to and load from files but don't want to use scar's standard file functions.
Well, it's only partially a tutorial, instead of explaining the steps towards building an example i'm going to explain the component in general.
You could kind of call this component an enhanced array of string…
It contains an array of string where we stoire our lines, but it has alot more stuff than just that, so i'm going to explain some of those properties:
- Append
Adds a line (string) to the component. (separated by a return from the other lines)
- Text
All of the lines in a row separated by returns.
- Clear
Emptys the lines.
- Count
Returns the number of lines in the component.
- Strings
The array of string that contains the lines. (Strings[index])
- LoadFromFile
Loads lines from a file into the array of string.
- SaveToFile
Saves the lines to a file.
- Free
Frees the component from the memory. (Always do this when you don't need it anymore to prevent memory leaks)
Of course there are alot more of propertys, but these are the most useful ones.
Now here's an example to use all of this:
This component is really recommended for people who want to save to and load from files but don't want to use scar's standard file functions.
Well, it's only partially a tutorial, instead of explaining the steps towards building an example i'm going to explain the component in general.
You could kind of call this component an enhanced array of string…
It contains an array of string where we stoire our lines, but it has alot more stuff than just that, so i'm going to explain some of those properties:
- Append
Adds a line (string) to the component. (separated by a return from the other lines)
- Text
All of the lines in a row separated by returns.
- Clear
Emptys the lines.
- Count
Returns the number of lines in the component.
- Strings
The array of string that contains the lines. (Strings[index])
- LoadFromFile
Loads lines from a file into the array of string.
- SaveToFile
Saves the lines to a file.
- Free
Frees the component from the memory. (Always do this when you don't need it anymore to prevent memory leaks)
Of course there are alot more of propertys, but these are the most useful ones.
Now here's an example to use all of this:
program TStringListExample;
var // Variable declarations
Strings: TStringList;
Count, a: Integer;
begin
// Initialises the TStringList component.
Strings := TStringList.Create;
// Inputs the lines into the component.
Strings.Append('Testingline 1');
Strings.Append('Testingline 2');
Strings.Append('Testingline 3');
// Saves the lines to ..\SCAR\TStringListExample.txt
Strings.SaveToFile(AppPath + 'TStringListExample.txt');
// Clears the lines
Strings.Clear;
// Loads the lines from ..\SCAR\TStringListExample.txt
Strings.LoadFromFile(AppPath + 'TStringListExample.txt');
// Gets the number of lines in the component and stores them into the variable "Count".
Count := Strings.Count;
// Writes the text that is in the component aka all the lines together.
WriteLn('The TStringList contains the following text: ' + Strings.Text);
// Writes the ammount of lines that are in the component.
WriteLn('The list contains ' + IntToStr(Count) + ' lines:');
// Writes all of the lines 1 by 1.
for a := 1 to Count do
WriteLn(' ' + IntToStr(a) + ': ' + Strings.Strings[a - 1]);
// Frees the component.
Strings.Free;
end.