DBE NSC Information Technology
43 catatan belajar, dibuat oleh cookie, dalam 2 bagian yang mengikuti topik kursus.
Notes for the South African National Senior Certificate in Information Technology (DBE, Grades 10-12 curriculum), in two folders: Delphi programming and SQL, and the theory, which is in folders for the eight topics of the curriculum in its order, with one note per sub-topic. Delete any folder your course leaves out once the notes are yours.
Isi di dalamnya
Catatan pertama
Delphi programming and SQL / Programming basics / Projects, forms and components
A Delphi application is a project, which is a group of files that the compiler joins into one program. The project file (with the extension .dpr) starts the program and names the other files. Each window the user sees is a form, and each form has its own unit file (.pas) for the code and a form file (.dfm) that stores the layout. Keep the two in step: a control placed on the form is named in the form file, and the code in the unit refers to it by that name.
The form is a class
A form is an object built from the TForm class. The class supplies the window, its title bar and the ability to hold other controls, so you do not write those yourself. When you add a button, label or edit box to a form in the form designer, Delphi writes a declaration of that control as an attribute of the form, and it creates the object when the form is created at run time. The form is therefore the base of everything on it, and code that runs on the form can reach every control it holds by name.
Components
A component is a ready-made control, placed from the Tool Palette onto a form. The components a course uses are the ones that handle input, output and interaction:
- TButton, which runs code when it is clicked.
- TLabel and TPanel, which display text. A panel also holds a Caption, so it can show a value that the code changes.
- TEdit, a single line of text the user can type into, and TMemo, a box for several lines.
- TSpinEdit, a box that accepts a whole number and moves up or down with arrows.
- TComboBox, a drop-down list of choices, and TRadioGroup, a set of options where only one can be chosen.
- TCheckBox, TListBox and TImage, which hold a tick, a list of items or a picture.
Each component is an object. It has properties that describe it, events that run code when something happens to it, and methods that do work on it.
Properties, events and methods
A property is a value that describes a component and that you can read or change. Caption, Text, Value, Checked and ItemIndex are properties of the controls above. Reading a property gives its current value, and assigning to it changes the control at once:
lblTotal.Caption := 'Total: ' + IntToStr(iTotal);
spnSize.Value := 10;An event is something that happens to a component, such as a click, a key press or a change of text. The programmer writes a handler, a procedure that runs when the event fires. A method is an action the component can perform when called, such as Clear on a memo or SetFocus on an edit box. The difference matters in code: a property is set or read like a variable, while a method is called with brackets, even when it takes no arguments.
Naming components
Every component has a name, and the code uses that name to reach it. Naming conventions make the code easier to read and easier for someone else to follow. A common convention puts a short prefix in front of the name to say what kind of control it is: btn for a button, lbl for a label, edt for an edit box, spn for a spin edit, mem for a memo, pnl for a panel. A name such as edtAge then tells the reader at once that the code is reading an edit box. Names are not case-sensitive in Delphi, so edtAge and EdtAge are the same name, but they must be spelt the same way, letter for letter, everywhere they appear.
Starting a project
A new project starts with one empty form and a unit with the same base name. When a task gives you an incomplete project, open the .dpr file rather than the unit, so that every unit and form file in the folder loads. The compiler reports an undeclared identifier when a unit uses a name that does not exist on the form, which usually means a control was renamed or its form file is missing.
Save often. Delphi keeps the form, the unit and the project file separate, and a project that is saved only in part will not run the way you expect.
A first program
The smallest useful program has one control and one piece of code. A button shows the text in an edit box in a message:
procedure TForm1.btnShowClick(Sender: TObject);
begin
ShowMessage('You typed: ' + edtName.Text);
end;btnShowClick is an event handler. Its name joins the component name, the word Click and the form's class, and the compiler links it to the button's OnClick event when the form is designed. The parameter Sender is the component that raised the event, which is useful when one handler serves several controls.
The code reads Text from the edit box, joins it to a string, and shows the result. Nothing is returned, so the procedure has no result type.
Why the layout matters
The user never sees the code. They see the form, so a name that is hard to read or a control placed where its label cannot be seen makes the program harder to use, even when every line is correct. Good Delphi work keeps the layout simple, gives each control a clear caption, and puts labels next to the boxes they describe. These are also the habits a tester checks first.
Dan 42 lainnya: buka yang kamu mau dari daftar di atas.
Ulasan
Belum ada ulasan tertulis. Tambahkan catatan ini ke catatanmu dan jadilah yang pertama memberi ulasan.