Crash Course Delphi 12:
Load ListBox items from file

Acron loads strings from a file

Acron, our program that shows a searchable list of acronyms, learns how to load its items from a textfile ;-)

We also introduce a better solution for aligning the two columns in the listbox: we set tabulator stops, "tabs".

Loading the Items of a ListBox from a file

You can load the listbow items from a text file with the procedure LoadFromFile, for example:


Of course, this assumes that you have a text file named Acron.dat in the directory c:\delphila\acron02. But wouldn't it be nice if your program could automatically locate that text file, without you having to "hard code" its location?

Well, we know for one that the file is located in the same directory as the program's exe-file.
And the complete path plus file name of an application's exe-file is given by Application.ExeName.

So, let's use the function ExtractFilePath(FileFullName) to extract the path. This is the part of the string up to and including the final "\" before the file name, in our example c:\DelphiLa\Acron02\

It's obvious that we should load the listbox at the very beginning of the program, that's right after the main form is created. In programmers language: in the OnCreate event handler of the form, we load the items of the listbox from a textfile named Acron.dat, located in the same directory as the program's executable file.

Lesson 2 LESSON 11     TOP  LESSON 13 Lesson 4