HOME - - - - - Delphi Tutorials TOC - - - - - - Other material for programmers

Delphi tutorial

For a discussion of making a High Score Table... stay here!

For information on creating stand alone units, pieces of code that can be used by more than one application, go to this newer, better tutorial

This is still in a draft form.... it is probably mostly right, but I make no promises just yet!!!

This has good information, and a search button at the bottom of the page

Please don't dismiss it because it isn't full of graphics, scripts, cookies, etc!

Click here if you want to know more about the source and format of these pages.

Delphi Tutorial 3c, part 1, (Version 28 Dec 98)

(Programs DD04 and DD05)

Purpose now: This tutorial describes making a high score table. It also talks about making re-usable units of code (DCUs). There is now a better tutorial on making a dcu, which you might want to read before doing this tutorial, to help you ignore the bits that you can learn better from the other tutorial!

Original Purpose: A component for use and reuse as a part of other programs.
Ongoing Limitiation: I don't intend to install it into your IDE, that is to say register it.

For the tutorial we will write a 'High Scores Table' component, and a little game to show it in action. The High Scores Table code will be in a discrete unit so that the next time you need a High Scores table in a project, you can have one simply by ading a few lines of code, and inserting calls to relevant methods provided by the High Scores code. The High Scores table will appear on the screen as an independant window outside of the game's window.
Start project, as in lesson 1. Filename DD04
Add a StringGrid to the form. (It is on the 'Additional' tab of the component palette)
Change the following properties from their defaults:
Fixed Cols:0
Height: 160
Width: 500

You should have something looking a bit like an empty spreadsheet, with a row of grey cells at the top. Put your cursor on the line between the first two cells of the first row. You should get a double headed arrow cursor. Use it to drag the width of the fisrt column down to something suitable for about 5 digits. Then drag the right hand edge of the to make it just fit the form. If you are left with a horizontal scrollbar, re-adjust position of the right hand edge of the grid. If, when you riun the program, your StringGrid column widths are re-set to previous positions: Stop the program, re-re-size the columns, then drag the whole window a bit on the screen. (This shouldn't be necessary, of sourse, but it seemed to fix a situation where I was losing re-sizings. I was also saving the project after re-sizing.. maybe that was 'the secret')

Put the following in the form's OnCreate event handler:
stringgrid1.cells[0,0]:=' Score';
stringgrid1.cells[1,0]:=' Name';

Save what you've got so far.
Try to run the project until successful.
Stop it (again!)
Adjust the position of the line between the grid's two columns to let 'score' show sensibly.

We'll have to go back and work on this more in a while, but first we'll begin a little application to use it. You can't (as far as I know!) work on two projects at once, so save DD04 and then use File|Close Project to shut down DD04 for the moment.

Set up a directory for the new project. Call it DD05. For now, just put a button on it, name/label the button 'Quit', and make the OnClick code...

Run DD05. See that clicking Quit quits it.

Now... there are many ways to make components available. The one described here isn't 'sophisticated'... but it has the virtue of minimal disruption to your Delphi environment. If ever you decide to 'register new components', be sure to back up the appropriate files first. (I'm being vague here because of gaps in my knowledge... but I DO know that it isn't hard to leave yourself with an unworking Delphi!!)

>>The following should work, I think it DID work... but when I tried it again, it seemed not to... don't worry, there's a messy 'fix' explained after the following>>>>

To use my solution, you must make a minor adjustment to your Delphi environment. In Delphi 1, you do the following:

Use Options|Environment. Click on the Library tab. Add to the library path the specification for whereever you put DD04 earlier, e.g. C:\Delphi\DD04
<< end of what SHOULD have worked<<<<

>>>Start of messier alternative which DOES (?) work (at a price)>>>>
Copy DD04u1.dcu and DD04.dfm to whereever Delphi looks for units, as shown by the Options|Environment Library tab. (In my case it was Delphi\Lib) The big pain of this 'solution' is that you have to remember to RE-copy these two files every time you make changes to them. If anyone can show me how to make the better solution work, I'll be grateful!)(Maybe File|Add is the way to go?)
<< end of messier solution<<<<<<

In any case.....
Now you should be able to add DD04u1 to the Uses clause of DD05, and re-run DD05. (You won't see anything new, but when all is well you won't see error messages, either.)

All of what follows until clear notice is within DD05u1.pas

Add to Var section...

DD04f1: TDD04f1;

Put in the FormCreate procedure which you create by clicking on that event in the object inspector in the usual way...


Run until that much works.

(By the way... you may think I'm an 'expert'... but you're being an optimist if you do. I'm just the one-eyed king stumbling around in the adventure game, discovering and passing on a few things that seem to work. If you'd like to do some of the stumbling and return the favour, tell me what the difference is between 'Create' and CreateNew' and explain when to use which!! (See Delphi Help, i.e. press f1))

Now, just after what you added above, add...


This isn't much of an application, but it does show how to access things in dd04f1 from within dd05!

A SHORTCOMING of my approach:
You'll only know about what is in dd04f1 from your work on it. One of the rewards for using the more complex provisions of Delphi by which you could register something like DD04 as a component would be that while working with DD05, you would be able to 'see' what was available in DD04, just as you can see the methods and properties of the components provided by Borland.

Well! That covers the essentials. In part two, I build up a full 'application' using DD04 as a high scores table for a little game. When you run DD05, it will look to see if an old high scores table was saved. If it was, it will fill the DD04 stringgrid with that data. As the game progresses, the display (DD04) and the data file will be updated asneeded.

A few final thoughts...
It is easy enough to change properties of the DD04 object at run time. For example (though I haven't tried it) I'm pretty sure you could move the DD04 Window around the screen with DD04f1.top:=50;, etc.

However, suppose you wanted to set one of the DD04 properties at design time? You could, for instance, add a property to say whether or not to OFFER the option of saving the datafile. This would need to be set before the OnCreate of DD04 was executed to give an elegant solution. With a normal, registered, Delphi component, setting properties at design time is simple. (You just use the object inspector.) If there's a simple way to alter initial property settings without registering a component, I haven't thought of it!

Registering the component would probably get you around the messy bit of creating the function to access the contents of OKToWriteToDisc, too.

As ever... any feedback on this tutorial would be welcome, especially notification of anything that is simply WRONG! The story continues in part two, click here to go on to Part Two.

   Search this site or the web        powered by FreeFind
  Site search Web search
Site Map    What's New    Search

Click here if you're feeling kind! (Promotes my site via "Top100Borland")
Ad from page's editor: Yes.. I do enjoy compiling these things for you... hope they are helpful. However.. this doesn't pay my bills!!! If you find this stuff useful, (and you run an MS-DOS or Windows pc) please visit my freeware and shareware page, download something, and circulate it for me? Links on your page to this page would also be appreciated!

Click here to visit editor's Sheepdog Software (tm) freeware, shareware page.

Link to Tutorials main page
Here is how you can contact this page's author, Tom Boyd.

Valid HTML 4.01 Transitional Page WILL BE tested for compliance with INDUSTRY (not MS-only) standards, using the free, publicly accessible validator at validator.w3.org

If this page causes a script to run, why? Because of things like Google panels, and the code for the search button. Why do I mention scripts? Be sure you know all you need to about spyware.

....... P a g e . . . E n d s .....