... 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);
begin
end;
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
(*of TTMA23f1.eAnsChange*)
It should come up in a different color and font, probably blue italic
On the blank line between the begin and the end put...
laRemark.caption:='Hey!';
...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
laRemark.caption:='Yes';
end (*no ; here*)
else begin
laRemark.caption:='I don''t think so';
end;
(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);
...add...
procedure PickProb;
begin
TMA26f1.laNum1.caption:='5';
end;
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:
PickProb;
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.
procedure PickProb;
begin
TMA26f1.laNum1.caption:=IntToStr(random(9));
TMA26f1.laNum2.caption:=IntToStr(random(9));
end;
...AND add to TTMA26f1.FormCreate to make it:
procedure TTMA26f1.FormCreate(Sender: TObject);
begin
Randomize;
PickProb;
end;
(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
var
TMA26f1: TTMA26f1;
On the next line add
sAns:string;
Also, in procedure PickProb, just before its end;, add...
sAns:='5';
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
sAns:=IntToStr(StrToInt(TMA26f1.laNum1.caption)+StrToInt(TMA26f1.laNum2.caption));
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...
bGoOn.hide;
Add the following to TTMA26f1.eAnsChange just after laRemark.caption:='Yes';....
bGoOn.show;
Make the OnClick event of bGoOn be...
begin
bGoOn.hide;
PickProb;
eAns.setfocus;
end;
(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
www.arunet.co.uk/tkboyd/offers.htm
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 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 .....