?
Unit 4, Object Pascal Introduction
4.1 Object Pascal Position Unit
4.1.1 Object Pascal Basic Architecture
Open an empty form.
Unit unit1; {program unit start and unit name} Interface {interface section of the beginning} Uses Windows, Messages, System1 = Class (TFORM) private} private} Private Declarations}} END; var {variable announcement} form1: tform1; realmentation {Home Start} {$ r * .dfm} end.
Unit: Object Pascal The most basic unit of each program or module, consists of a group constant, data type, variable, program, and a function. Generally include both interface and importation. Interface: Program unit external interface, he is used to define an element that can be seen and used by other program units. Implementation: The programs declared by INTERFACE.
4.1.2 Variable declaration
Var Form1: TFORM1; ICOUNTER: Interger; Sname: string;
Steps: First name the variable, then specify the type of variable. Variable naming principle
So remember and meaningful. Identification word up to 63 words. The identifier must be started with letters or _, and the subsequent characters are letters, bottom lines, or numbers. Identification words cannot be reserved or Delphi already defined identification words.
Object Pascal built-in data type
Category Data type Description Integer 2 Bytes, -32768 to 32767 Shortint 1 Byte, -128 to 127 Longint 4 Bytes Byte 1 byte, 0 to 255 Word 2 Byte, 0 to 65535 Single 7 to 8 Valid numbers, need 4 Bytes Double 15 to 16-bit valid numbers, 8 BYtes Extended 19 to 20 valid numbers, 10 BYtes Comp 19 to 20 valid numbers, 8 Bytes Real 11 to 12 valid numbers, need 6 bytes Boolean True OR False, need 1 bit character char ascii character string String longest 255 ASCII character indicators Pointer Unit index string indicator PCHAR pointing the string indicator
4.1.3 type declaration
Users must first declare. Start with the reserved word Type. The type is only the length of the data type, not a real variable.
4.1.4 Uses clause
Similar to include in C / C . Has a extension.
4.2 Connotation of the program unit
On a table, and study the program code it produces.
Add a listbox component and adjust its size properly. Add two Button components, as shown above. Bind the CAPTION of Button1 to "List Output", Button2 CAPTION is "clear information". Join the Table component in Data Access, the DatabaseName feature is set to "DBDemos", and the TableName feature is set to "Animals.dbf", Active = true, indexfieldnames is set to Name. Join DataSource Components in Data Access, DataSet = Table1. The storage form is Mainform, press F12 to switch to the program editor. unit MainForm; interface uses Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs, DB, DBTables, StdCtrls; type TForm1 = class (TForm) ListBox1: TListBox; Button1: TButton; Button2: TButton; Table1: TTable; DataSource1: TdataSource; private {private declarations} end; var form1: tform1; importation ... end.
Delphi is added to the component's declaration of the component to the form of the form, and its program unit is added to the USES clause. This function of this automatic maintenance program helps us quickly develop. But to remember, if you finger a more motion editor (add a component), Delphi's form does not follow, and it is often errors.
Double-click the "Clear Data" button, Delphi will generate Button2Click in the program editor is a piece of handler.
Unit1's Interface Site
TYPE TFORM1 = Class (TFORM) ... Procedure Button2Click (Sender: TOBJECT); Private ...
Unit1's IMPLEMentation section appears
Implementation {$ r * .dfm} procedure TFORM1.BUTTON2CLICK (Sender: TOBJECT); begin end;
At the Implementation section, we can call the Clear method to clear the content in ListBox.
Procedure TFORM1.BUTTON2CLICK (Sender: TOBJECT); Begin ListBox1.clear;
4.3 Process Control
4.3.1 Jump and Separation
IF narrative
IF thisyearis = '1995' Then MessageBeep (1) else Messagedlg ('Thisyearis is not 1995', Mtinformation, [Mbok], 0);
Nest IF narrative
ELSE will automatically pair with closest to IF.
IF thisyearis = '1995' Then if thismonthis = '8' damagebeep (1) else Messagedlg ('Thisyearis is not 1995', Mtinformation, [Mbok], 0);
Then, when 1995, "ThiSyearis is not 1995" is not in August.
If thisyearis = '1995' Then Begin if thismonthis = '8' Then MessageBeep (1) Else Messagedlg ('Thisyearis is not 1995', mtinformation, [mbok], 0); if it is not 1995, "ThiSyearis is not 1995"
Case narrative
Case start (thismonthis) of 1, 3, 5, 7, 9, 11: Messagedlg ('thismonthis is an odd month', mtinformation, [mbok], 0); 2, 4, 6, 8, 10, 12: Messagedlg 'Thismonthis is even month' mtinformation, [mbok], 0); Else Messagedlg (' thismonthis has an error ', Mtinformation, [Mbok], 0);
4.3.2 Recycling
REPEAT narrative
I: = 1; Repeat ... Writeln (i); until i = 10;
While narrative
I: = 1; while i <= 1 do begin i: = i 1; Writeln (i);
FOR narration
For i: = 1 to 10 DO Writeln (i);
Netoperative for round
For i: = 1 to 10 do for j: = 1 to 5 DO WRITELN (i * j);
4.3.3 Completion of the paradigm
Double-click the list of data buttons to complete the following.
Procedure tForm1.button1click (sender: Tobject); begin table1.first; while not table1.eof do begin listbox1.items.add (table1.fieldbyname ('name'); Table1.Next; end;
carried out.
4.4 Join the second table (The Second Form)
Select File | New Form to create a new form. Add four Label components in the table, bide its name to "Name", "Size", "Weight", "Region". Add four DBText components as the field of display. Add a dbimage to display the image.
The contents of the program editor at this time are as follows:
unit SndForm; interface uses Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs, DBCtrls, DB, DBTables, StdCtrls; Type TForm2 = class (TForm) Label1: TLabel; Label2: TLabel; Label3: TLabel; Label4: TLABEL; DBTEXT1: TDBTEXT; DBText2: TdbText; dbText3: TdbText; dbtext4: tdbtext; private public end; var form2: tform2; importation {$ r * .dfm} end.
Select ListBox1 in the first table, double-click the OndbkClick event, and fill in the following event handler.
Procedure TFORM1.ListBox1dblclick (Sender: TOBJECT); begin form2.showmodal;
Add a declaration for SNDFORM in the importation of MainForm.
Implementation Uses SndForm; {$ r * .dfm}
The execution process, after listing the data button, in double-click the list of information in ListBox. 4.5 Programs and Functions
4.5.1 Meter
Procedure starts with the reserved word procedure, the parameters are placed in the scraping number.
Procedure setdbname (name1: string; name2: string; Name3: string);
or
Procedure setdbname (Name1, Name2, Name3: string);
Function begins with the reserved word function, the parameters are placed in the scraping number, and the rear is added to the post.
Function getDBName (Name1, Name2, Name3: string): String;
4.5.2 Program block
Contains part of the declaration and the narrative. The declaration part told the compiler to be used. He contains type declaration (starting with the word Type), the variable declaration (starting with VAR), the constant declares (starting with const).
TYPE TCOUNT = INTEGER; TPRIMARYCOLOR = (Red, Yellow, Blue); TTESTINDEX = 1..100; TTEXTVALUE = -99..99; ttestlist = array [TTESTINDEX] of ttestValue; tcharval = ORD ('a') .. ord ('Z'); TDAYS = (Modday, Tuesday, Wednesday, Thursday, Friday, Saturday, Sunday);
The narrative part said that the logical action that can be performed, he is started by Begin, ending by END. The declaration of each program is valid in this program.
4.5.3 Recycling of a function value
There are two ways to pass the functions of the functions.
Specify the value of the return to the function name.
Function getRecordnumber: integer; begin getRecordnumber: = table.recordcount; end; assign the return value to the Result variable (this variation can not be required).
BEGER; begin result: = Table.RecordCount;
4.5.4 Join programs or functions in programs
How to make programs or functions are used by other programs
The program or a combine of the header is placed in the interface part of the program unit. Put the name of the program of this program or a function of the program or a USES clause of the program or a function unit.
The programs or functions of the program or a function must be announced before all the programs that use his.
Pre-announcement
Programs or functions using the pre-announcement can put their Implementation part in any place. He can be used to generate mutual delivery, that is, there is a B-letter in the A letter, and there is also a Vo-like in the B-card.
Function GetRecordnumber: Integer; Forward;
4.5.5 Transfer parameters
Numerical parameters: Parameters are passed in the way of call by value, and the subscriber does not change the value of the parameters in the original.
Procedure setformData (atblname: string);
Variable parameters: Parameter passed in the way in which the call by address is transmitted, and the parameter is transmitted, and the subscriber will change the value of the parameters in the original path.
Procedure gettablename (var atblname: string);
Constant parameters: The value of the formal parameters is not changed. The specified method is to add a const reserved word before the parameter.
Procedure setformdata (const atblname: string);
5.6 Complete the paradigm
Select ListBox1 in the first table, double-click the OndbkClick event, and fill in the following event handler. procedure TForm1.ListBox1DblClick (Sender: TObject); var aStr: string; begin aStr: = ListBox1.Items [ListBox1.ItemIndex]; Form2.SetClickedData (DataSource1, Table1, aStr); Form2.ShowModal; end;
Plus the use of DB and DBTABLES in the Uses clause of SNDFORM.
Unit SndForm; Interface Uses Windows, Messages, Sysutils, Classes, Graphics, Controls, Forms, Dialogs, Dbctrls, DB, DBTABLES, STDCTRLS
PRIVATE and PUBLIC parts declared in TFORM2 categories:
PRIVATE: TTABLE; ANAMESTR: STRING; public procedure setclickeddata (asource: tdataserce; atbl: ttable; const aname: string);
In the IMPLEMentation part, plus setClickedData's work.
PROCEDURE TFORM2.SETCLICKEDDATA (ATBL: TTABLE; const aname: string); begin adatasource: = asource; atten: = attbl; anamestr: = aname;
Add the following programs in Form2 onactivate events:
procedure TForm2.FormActivate (Sender: TObject); begin DBText1.DataSource: = aDataSource; DBText2.DataSource: = aDataSource; DBText3.DataSource: = aDataSource; DBText4.DataSource: = aDataSource; DBImage1.DataSource: = aDataSource; if aTable.FindKey ([aNameStr]) then begin DBText1.DataField: = 'NAME'; DBText2.DataField: = 'SIZE'; DBText3.DataField: = 'WEIGHT'; DBText4.DataField: = 'AREA'; DBImage1.DataField: = 'BMP End; end;
carried out.