Menu Component ( Open, Save, Save as, Print, Close)

Posted by Lionel Joyner P12219 on May 02, 2005

I am working on an Delphi application that performs some computations and displays the results in a ListBox. I am using the Delphi Menu Component and have the following setup:

File
Open
Save
Save as
Print
Close
Exit

I can Print the results and can Close the application and Exit the application but do not know how to Save the Results to a folder on a disk or hard drive or Save the File As something other than text or Open the File once it is saved. If someone has a simple example of a menu that does the above, I would appreciate hearing from you.

Reply by webmaster Guido on May 04, 2005

Saving the results in a text file is simple, as you've probably found out already. Because you have them in a ListBox, you can use:
ListBox1.Items.SaveToFile('c:\results\data.txt')

Saving other data then text, requires that you use some form of database:

1. Use a desktop database that is supported by Delphi, such as Paradox or dBase. For this, you need a Delphi version with the database components: D4 Pro and higher, D5 Pro, D6 Pro,...
In that case, it's very simple and we can guide you throught the process :)

2. If you don't have the database stuff in your version of Delphi, you have to create a simple database system of your own, based on text files.
Before writing data, all your data values are converted to text, "fields" are separated separated by a special character, and each "record" is on a separate line of the text-file. Example with semicolon ; as separator:

record1;12.500;50.888;0.15
record2;11;17.98888;1.588
and so on...

When reading back, you split up each line into strings and convert these strings to their original format (numbers, dates,...)

Which way do you want to go?

Reply by Lionel Joyner on May 06, 2005

I am using Delphi 7 Professional and the only Database I have is Microsoft Access 2002, I think the version of Delphi I am using has a Database engine built in but I have not studied up on it and therefore don't know much about it. I would like keep it simple and in the pass I have called up NotePad and save my Data as a text file. This works OK but I am looking for a better way and not having to work through NotePad. The Data files consist of Text and Numbers.

Do you have an example of a Menu with the (File, Open, Save, Save as, Print, Close) features using Delphi 7 Professional. I think sticking with Delphi 7 is the best way to go at this time since I don't have Paradox or dBase. --- This is an unfamiliar area for me and I appreciate your help. I have no problems doing the applications, but recording the work is what I am having the most problems with now.

Reply by Lionel on May 08, 2005

1) Created a File Folder on a floppy and named the folder "RESULTS"

2) Then in the (MENU) I added a (Save) and then entered the following statement:

begin
  ListBox1.Items.SaveToFile('A:\Results\data.txt')
end;

I then go to the floppy on the A: drive and click on the Folder and then click on the data.txt file and it comes up in Notepad. At this point everything is OK until I do another set of computations and click on Save and then the new Results overwrites the previous data.txt file.

This does not seem to be working out very well and maybe the best approach would be to save the data to a database and have each (record) on a separate line like you mention above. Also this would solve the Reading Back problem.

Having said all this I think the Database is the way to go. I don't know where to start on doing this so some help sure would be appreciated.

Reply by webmaster Guido on May 09, 2005

In the series of lessons for DelphiLand's "Crash Course", lessons 14 will be about building a flat-file database system. We shall use the CSV format, "Comma Separated Values", because a lot of people don't have Delphi's database components (they use a personal version of Delphi).

The entire series of articles is not finished yet, but you can have a sneak preview here:
www.festra.com/eng/les14.htm

In the series of lessons for DelphiLand's "Crash Course", lessons 14 and on are about building a flat-file database system. We use the CSV format, "Comma Separated Values". The entire series of articles is not finished yet, but you can have a sneak preview here:
www.festra.com/eng/les14.htm

Added today: beta version of lesson 15:
www.festra.com/eng/les15.htm

Reply by Lionel on May 16, 2005

Thanks, this is really helping me to understand the creation and workings of a CSV database.

Thanks again,
Regards,
Lionel


 

Delphi Forum :: Tutorials :: Source code :: Tips