- - - - - Delphi Tutorials TOC - - - - - -
Other material for programmers
Delphi tutorial, Start Here.
This has good information, and a search button at the bottom of the page.
Click here if you want to know more about the source and format of these pages.
Please don't dismiss this page because it isn't full of graphics, scripts, cookies, etc!
Viewing the page in a window which isn't the full width of your screen may make the text more readable. I could have created margins full of ads, but I wanted you to be in charge of how the text appears on your screen!
A simple Delphi programming example, with details applicable to any Delphi project.
This example assumes you know a little bit about programming.
It also moves along, completing a modestly interesting project, but introducing more than the essential minumums for a first lesson. It you are a very, very new Delphi programmer, you might prefer the first page of my Delphi Lessons.
These tutorials are really of little use unless you have a machine running Delphi to 'sing along' with the instructions. You should be able to work through the example in about half an hour. Some of the early tutorials were written when I was working in Delphi 1 with Windows 3.1... apologies if you find confusion with different versions. As of 6/05, I had been working with Delphi 2 under Windows 98SE for quite a while.
The program will give a simple arithmetic test. It is not polished to make everything as nice as you would want it to be. The instructions concentrate on just the essentials. Making it "perfect" is an exercise for the student!!
a) Choose the name you want on the final .exe file. For this tutorial, I am using the name TMA26. (Teach Mathematics, 26th program). The various names I will derive from that are not required by Delphi, they are just my way of keeping track of what everything is.
b) Again, not required by Delphi, but my (strong) recommendation: create a folder (directory) for the project. Name: TMA26
c) Start Delphi. Click File|New Project
d) Use the Object Inspector to change the name property of Form1 to TMA26f1. The Object Inspector was probably put on-screen when you started Delphi. F11 should bring it up again if it isn't showing. The Object Inspector has two tabs at the top of its form: Properties and Events. There's also a listbox at the top, though at the moment, there's only one object in the list. From now on I'll say things like 'Change the name of Form1 to...' as a shorthand version of 'Use the object inspector, properties page, to change Form1's name property to...' (Events? We'll deal with them in a moment!)
e) Do File|Save Project As.., and
i) Move to the directory you created for the project
ii) Save Unit1 as TMA26u1.PAS
iii) Save Project1 as TMA26.dpr
f) That has you started. From now on, you just....
- - Repeat
- - - - Repeat
- - - - - - Work on program, i.e. add code or modify earlier efforts
- - - - - - Run program to see effect (within Delphi)
- - - - Until nervous about amount of unsaved work
- - - - Save project
- - Until out of time for this work-session
- - File|Close Project
- - Go about the rest of your life until ready to resume programming
- - File|Open Project
Until happy with program.
g) Next, you will 'Put a label called laHello on Form1'. I'll say how to do that in detail this time. You'll have to learn the basic idea from this one-time blow-by-blow account.
i) On the Delphi toolbar (I hope that's what its called... the thingie with the menu, the 'File, Edit, Search...' SpeedBar, and the Compoent Palette) click on the 'Standard' tab of the 'Component Palette'. If you put your mouse pointer over the 'A' icon and wait a moment, a hint saying 'Label' should pop up. Click the icon. Nothing much appears to happen- yet. Move your pointer onto the TMA26f1 form. Move it to about 1cm below and right of the upper left hand corner. Press down the left hand button on the mouse, keep it down. Move the mouse down and right, which should "drag" open a box on the form. Keep going until it is about 1 cm from the right edge of the form and 3cm down from the top. Release mouse button. You should see a label saying 'Label1' on your form. Click on the Object Inspector's listbox. You can see that you now have two objects. Label1 was already selected by the object inspector; leave it selected. Go down to the autosize property. Make it 'false'. Go down to the Name property, change it to laHello.
j) Also by using the Object Inspector, change laHello's caption to 'Hello, welcome to my program.' Set alignment to taCenter.
k) Click somewhere within laHello on the Form. Change the size and position a bit, just to see that you can!
l) This would be a good time to check that File|Save Project works. You won't see much if it does... but if you get error messages sort out why.
m) Now run your program. There's a green pointing-to-right triangle on the toolbar for this, just left of two parallel bars, which are currently greyed out. (You can alos press F9 to run the program if you are unsure of the right icon.)
You have now got far enough to get caught up in some tangles that have annoyed me from time to time.
While your program is running, you cannot do further work on it. To stop your program, you can do alt-F4, or click on the little 'x' in the window's upper right hand corner.
If, when you try to run your program, you get 'Source has been modified. Rebuild?', you usually want to click "Yes". If you click "No", the program should run, but it will not incorporate changes you've made recently. Stop the program (as above, Alt-F4, or alternate), then you will be able to run it, and your changes will be incorporated.
If, when you run your program you get 'STOP: Project ... use Step or Run to Continue', just click the provided 'OK'. You may get another error message, or you may get taken to the text that makes your program do what it does, i.e., your 'source code', TMA26u1.pas in the example we are working through. Just click on the 'Run' green arrow and click OKs until you get to the point where you can stop your program (Alt-F4, etc), then edit, try again.
n) Now: Add two more labels, in the lower left part of the form. Put one above (not on top of!) the other, big enough to hold the text which appears. Set autosize false for both. Name them laNum1, laNum2. Make the caption of each 2
o) Add one more label: Lower right, autosize false. About 3cm high, 6cm wide. Name laRemark. Caption: What is the sum of these numbers?
p) Add an edit box. (The hint just says 'Edit'.) Put it beneath the two boxes laNum1 and laNum2. Autosize false, name eAns. (This would be another good place to save project!) Try running it. Your 'program' won't do much yet, but it shouldn't give rise to any error messages.
So far, we have just been creating the form which will appear when you run your program. With Delphi programming, it is useful to think quite hard about what your users will see. Once you are clear about that, the only other thing you have to worry about is what the effect is going to be of buttons, menu items, edit boxes, etc, will be. The edit box we put on our form is all this program needs for getting things into the computer... Now we can turn to how to make use of the components we've used....
q) Double click on the edit box (eAns). This should accomplish the same thing as: "Click on the Events tab of the Object Inspector. Double-click in the listbox field just to the right of 'OnChange' at the top of the list of possible events. The screen will alter as TMA26u1.pas comes to the fore, and the following will have been added, in a suitable place:"
procedure TTMA26f1.eAnsChange(Sender: TObject);
This is a "skeleton" for the OnChange event handler of the edit box. Press on, even if you're not to clear what an event handler is, yet....
After the end and it's semicolon, add
It should come up in a different color and font, probably blue italic
On the blank line between the begin and the end put...
...and try to run the program. With the program running, click on the edit box. (It says eAns at the moment. Press a key, say 'x'. As soon as what is in the edit box changes, i.e. OnChange, the bit of program you wrote is executes, and the caption of laRemark becomes 'Hey!'. Not very educational, but a start! The effect of the line you wrote was, 'When the computer comes to do this, change the caption property of the object laRemark to Hey!'.
r) Now let's make things more educational....
Stop the program (If you haven't done so already. You always have to before making changes.)
Replace laRemark.caption:='Hey!'; with:
if eAns.text='4' then laRemark.caption:='Hey!';
That still won't 'work' to help people learn adding, but play with the result... you should find that laRemark remains 'Good Luck' until you change the contents of the edit box to 4. Bear with me... this program will eventually do most of what it should... I simply want to get there a step at a time.
s) Replace your previous effort with... (leaving the begin and end; (*of TTMA23f1.eAnsChange*) from before)
if eAns.text='4' then begin
end (*no ; here*)
laRemark.caption:='I don''t think so';
(BTW: the following is very useful, once you've practiced it a few times so you remember to use it: To select some text, either:
i) drag the mouse over it, or,
ii) use the cursor keys to get to one end. Hold down shift. Use the cursor keys (shift still down) to move to the other end.
Once you have text selected, ctrl-c will copy it, ctrl-x will cut it. Once you have copied or cut, you can paste with ctrl-v. If after making your first selection and copying or cutting, you make a second selection before pasting, the second selection will be replaced by whatever you paste. You can copy something from this text, then use the mouse to move to the program code window, and paste there.)
(Also BTW: If you already know Pascal, you will see that two begin/end pairs above are not needed, and a semicolon or two. Don't worry about it... I had my reasons! And yes, I do know about StrToInt)
Semicolons: In general: If in doubt, stick one in. Don't, however, just before an else... and the exception is rare enough that I usually put the (*no ; here*) on the relevant line as a reminder.
Apostrophes: You can see the answer to a problem in 'I don''t think so'. If you want an apostrophe in a string, just do a double apostrophe.
This would be a good place to save project and take a break.
t) Now... Just before...
procedure TTMA26f1.eAnsChange(Sender: TObject);
Try running the program. No effect should be apparent yet, but there should be no error messages, either!
u) Use the Object Inspector to display TMA26f1's properties and events. Click on the Events tab. Double-click on the OnCreate event. Put the following between the begin and end pair:
Run the program. Other than the fact that it says that 5+2 is 4, all should be well.
v) Add to PickProb, making it what appears below. N.B. I said "Add to"... Do not type all of what you see here, just the new bits.
...AND add to TTMA26f1.FormCreate to make it:
procedure TTMA26f1.FormCreate(Sender: TObject);
(Without that randomize in FormCreate, you get the same problems each time. You can find out more about individual Borland provided functions and procedures by putting the cursor in the name and doing ctrl-F1)
When you run the program, you should get different numbers to add, even though '4' is still always the 'right' answer.
Save project again!
w) About a third of the way down TMA26u1.pas, you'll find
On the next line add
Also, in procedure PickProb, just before its end;, add...
Also, in procedure TTMA26f1.eAnsChange, make the first line say...
if eAns.text=sAns then begin
... and run the program. Now the 'right' answer is always 5, once you have everything correct.
x) Change the sAns:='5'; in PickProb to
At last! The program should only say 'yes' for the right answer!!
y) Add a button to the form (icon has 'ok' on it.) Name it bGoOn, make caption say 'Click this to go on'. Add the following to TTMA26f1.FormCreate...
Add the following to TTMA26f1.eAnsChange just after laRemark.caption:='Yes';....
Make the OnClick event of bGoOn be...
(The program will run without the eAns.setfocus, but you have to click on eAns before each answer.)
And there you have it! A working program. (And I didn't even massage the text to get the lettering of the steps to come out so well!)
Was it of any use? Please send an email, at least.
Even better, download a piece of my software, try it and pass it on to someone?
Cheers, Tom Boyd
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 freeware, shareware page.
Link to Tutorials main page
Here is how you can contact this page's author, Tom Boyd.
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 .....