📄 delphi tutorial, start here (level 1)..htm
字号:
<html>
<!-- CS Dt1a
Rev 8 Mar 04
Around since at least 19 Nov 00-->
<head>
<title>Delphi tutorial, Start Here (Level 1).</title>
<meta name="description" content="Help with programming using Borland's Delphi">
<meta name="keywords" content="delphi tutorial borland beginnner introduction faq inprise">
<meta name="rating" content="general">
</head>
<body text="#000000" BGCOLOR="#FFFFFF">
<a href="http://www.arunet.co.uk/tkboyd/index2.htm">HOME </a>
- - - - - - - - - - - -
<a href="http://www.arunet.co.uk/tkboyd/prgmr1.htm">Other material for programmers</a>
<H2>Delphi tutorial, Start Here (Level 1)</H2>
<H3>This has good information, and a search button at the bottom of the page.</H3>
<h4>Please don't dismiss it because it isn't full of graphics, scripts, cookies, etc!</h4>
<a href="Tut1Ed.htm">Click here</a> if you want to know more about the source and format of these pages</a>
<H3>IGNORE ANY PERIODS (.) AT THE START OF ANY LINE</H3>
<hr>
<!-- Start Delphi supersite voting links.. graphic 1st, then text -->
<A HREF="http://www.sandbrooksoftware.com/cgi-bin/TopSite2/rankem.cgi?id=tkboyd">
<IMG SRC="dpsc5.gif" HEIGHT="54" WIDTH="79" BORDER="0" ALT="Delphi supersite: vote here"></A>
<A HREF="http://www.sandbrooksoftware.com/cgi-bin/TopSite2/rankem.cgi?id=tkboyd">Please click here to help this site get publicity</A>
<!-- End Delphi supersite voting links -->
<h3>A simple Delphi programming example, with details applicable to any Delphi project.</h3>
<br>__________
<br><br>
This example assumes you know something about programming.
<br><br>
It really isn't much 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. I work in Delphi 1, Win 3.11... apologies if you find confusion with different versions or Win95.
<br>__________
<br>
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.
<br>__________
<br>
<br>a) Choose the name you want on the final .exe file. For the example, the name is TMA26. 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.
<br>
<br>b) Again, not REQUIRED by Delphi, but my recommendation. Create a folder (directory) for the project. Name: TMA26
<br>
<br>c) Start Delphi, click File|New Project
<br>
<br>d) Change the name property of Form1 to TMA26f1.. To do this, you use the Object Inspector, which was probably put on-screen when you started Delphi. F11 should bring it up if it isn't showing. The Object Inspector has two tabs at the bottom of the 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!)
<br>
<br>e) Do File|Save Project As.., and
<br>
<br>. i) Move to the directory you created for the project
<br>. ii) Save Unit1 as TMA26u1.PAS
<br>. iii) Save Project1 as TMA26.dpr
<br>
<br>f) That has you started. From now on, you just....
<br>
<br>Repeat
<br>. Repeat
<br>. Repeat
<br>. Work on program, i.e. add code or modify earlier efforts
<br>. Run program to see effect (within Delphi)
<br>
<br>. Until nervous about amount of unsaved work
<br>. Save project
<br>. Until out of time for this work-session
<br>. File|Close Project
<br>. Go about the rest of your life until ready to resume programming
<br>. File|Open Project
<br>Until happy with program.
<br>
<br>g) Next, you will 'Put a label called lHello on Form1'. I'll say how to do that in detail this time, but you'll have to learn the basic idea from this one blow-by-blow account.
<br>
<br>i) On the Delphi toolbar (I hope that's what its called... the thingie with the 'File, Edit, Search...' menu) 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.) Move your pointer onto the TMA26f1 form. Move it to about 1cm below and right of the upper left hand corner. Drag open a box that takes you to 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 lHello.
<br>
<br>j) Change lHello's caption to 'Hello, welcome to my program.' Set alignment to taCenter.
<br>
<br>k) Click somewhere within lHello on the Form. Change the size and position a bit, just to see that you can!
<br>
<br>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.
<br>
<br>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 press F9 if unsure of the right icon.)
<br>
<br>You have now got far enough to get caught up in some tangles that have annoyed me from time to time.
<br>
<br>While your program is running, you cannot do further work on it. To stop your program, you can do alt-F4, or (Win 3.x) click on the small box with the horizontal 'slot', upper left, which should give you a menu including Close, or just double click that little box.
<br>
<br>If, when you try to run your program, you get 'Source has been modified. Rebuild?', click No. The program should run, but it may 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.
<br>
<br>If, when you run your program you get 'STOP: Project <did something awful>... 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. (The 'source code', TMA26u1.pas in the example.) Just click on the 'Run' green arrow and click OKs until you get to the point where you can stop you program (Alt-F4, etc), then edit, try again.
<br>
<br>n) Now: Add two more labels, lower left of screen, one above the other, big enough to hold the text which appears. Set autosize false for both. Name them lNum1, lNum2. Make the caption of each 2
<br>
<br>o) Add one more label: Lower right, autosize false. About 3cm high, 6cm wide. Name lRemark. Caption: What is the sum of these numbers?
<br>
<br>p) Add an edit box. (The hint just says 'Edit'.) Put it beneath the two boxes lNum1 and lNum2. Autosize false, name eAns. (This would be another good place to save project!) Also, try running it. Your 'program' won't do much yet, but it shouldn't give rise to any error messages.
<br>___
<br>So far, we have just been creating the form which will appear when you run your program. The edit box we put on it is all that is needed for getting things into the computer... now this is how to make use of them....
<br>___
<br>
<br>q) Select the edit box (eAns). 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:
<br>
<br>procedure TTMA26f1.eAnsChange(Sender: TObject);
<br>begin
<br>
<br>end;
<br>
<br>After the End and it's semicolon, add
<br>
<br>(*of TTMA23f1.eAnsChange*)
<br>
<br>It should come up in a different color and font, probably blue italic
<br>
<br>On the blank line between the Begin and the End put
<br>
<br>lRemark.caption:='Hey!';
<br>
<br>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 lRemark 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 lRemark to Hey!'.
<br>
<br>r) Now let's make things more educational....
<br>
<br>Stop the program (If you haven't done so already. You always have to before making changes.)
<br>
<br>Replace lRemark.caption:='Hey!'; with:
<br>
<br>. if eAns.text='4' then lRemark.caption:='Hey!';
<br>
<br>That still won't 'work' to help people learn adding, but play with the result... you should find that lRemark remains 'Good Luck' until you cnage 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.
<br>
<br>s) Replace your previous effort with... (leaving the Begin and End; (*of TTMA23f1.eAnsChange*) from before)
<br>
<br>if eAns.text='4' then begin
<br>. lRemark.caption:='Yes';
<br>. end (*no ; here*)
<br>. else begin
<br>. lRemark.caption:='I don''t think so';
<br>. end;
<br>
<br>(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:
<br>
<br>i) drag the mouse over it, or,
<br>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.
<br>
<br>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.)
<br>
<br>(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)
<br>
<br>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*)' reminder in on the relevant line.
<br>
<br>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.
<br>
<br>___
<br>This would be a good place to save project and take a break.
<br>
<br>___
<br>t) Now... Just before
<br>
<br>procedure TTMA26f1.eAnsChange(Sender: TObject);
<br>
<br>Add
<br>
<br>procedure PickProb;
<br>begin
<br>TMA26f1.lNum1.caption:='5';
<br>end;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -