Back in the "Records in Delphi" article, a sample code was presented demonstrating pressing a button to populate the array of records with dummy information, then display the records in the memo control. This should give you a starting point to working with record arrays. The main thing to remember about the example is that we created a record type, supplied the name of TPerson, then created a local variable called MyPeople which is an array of type TPerson which can hold three (3) rows of information.
Let's use the idea to present a data entry form to a user, allow data to be entered about several people before returning to the main form to process it. Use the next piece of code to call the secondary form for the data entry:
procedure TfrmMain.PersonRecord1Click(Sender: TObject);
var
f:TForm5;
begin
f := TForm5.Create(Self);
try
if f.ShowModal = mrOk then
if f.PersonCount > 0 then
ShowMessage('The first person is' + #13 + '"'
+ f.PersonArray[0].FirstName + '" "'
+ f. PersonArray[0].LastName + '"');
finally
f.Release;
end;
end;
|
When the user invokes the event, the main form creates the data entry form, shows it, then checks to see if there were any people entered into the people array. The variable PersonCount in the data entry form is incremented each time a new person is added to the array of records.
The following code is placed into a simple unit (Delphi IDE Main Menu: File -> New -> Unit) so that any form in the project can see the People record along with a constant used to limit how many elements (records) can be used in the array of records.
unit kg_Globals; //saved as kg_Globals
interface
uses
Windows,Dialogs,SysUtils,Messages,Classes,Forms,FileCtrl;
const
ARRAY_SIZE = 5; {Could be a larger size}
type
{Keep each record member to strings to
make the example code easy to understand}
Tkg_People = Record
FirstName : String;
LastName : String;
Street : String;
City : String;
State : String;
ZipCode : String;
Phone : String;
Email : String;
end;
implementation
end.
|
In the public portion of the called form's declarations we declare an array of records and a variable for keeping track of how many records the user has entered.
...
public
{ Public declarations }
PersonArray: Array [0..ARRAY_SIZE] of Tkg_People;
PersonCount: Integer;
...
|
In the Create event of the data entry form the following code is called to clear all edit controls so that the form starts up with a clean slate. Each time the user accepts a screenful of people information the ClearEdit procedure is triggered:
procedure TForm5.ClearEdits;
var i:Integer;
begin
{ sets each TEdit control's
text property to an empty string }
for i := 0 to ComponentCount -1 do
if (Components[i] is TEdit) then
TEdit(Components[i]).Text := '' ;
end;
|
The next code fills a row of data in the people array and increments the row counter. The counter is checked to ensure that the number of rows keeps within the fixed boundaries of the array of records.
procedure TForm5.cmdAddPersonClick(Sender: TObject);
begin
{ Populate a record in an element in
the array of records with current textbox values }
PersonArray[PersonCount].FirstName := First.Text;
PersonArray[PersonCount].LastName := Last.Text;
PersonArray[PersonCount].Street := Street.Text;
PersonArray[PersonCount].City := City.Text;
PersonArray[PersonCount].State := State.Text;
PersonArray[PersonCount].ZipCode := Zip.Text;
PersonArray[PersonCount].Phone := Phone.Text;
PersonArray[PersonCount].Email := Email.Text;
Inc(PersonCount);
{
Once the maximim elements are used up,
disable this button.
NOTE: ARRAY_SIZE is a user defined constant
created for this demo. The Constant indicates
how many records can be accessed in the array.
}
TButton(Sender).Enabled := PersonCount < ARRAY_SIZE;
{
If we can still add more persons clear
old entries, place focus on "First"
name text control
}
if TButton(Sender).Enabled then
begin
ClearEdits;
First.SetFocus ;
end
else
{ Exit time , give them a hint by
placing focus on the exit button }
cmdCloseForm.SetFocus ;
end;
|
Using this example you can return just about any type of data, in either single or multiple rows.
To recap, you can detect how a modal form was closed and get information back in several ways. Keep in mind there are other ways to accomplish returning information from modal forms.
Detour time
The last thing a programmer needs when searching for assistance in a help file or manuals is an incorrect example of how to do a particular task. I caught one in D4 after writing this article, to do with "Passing additional arguments to forms", listed under "forms" in Delphi help. Shown below are the key pieces:
type
TResultsForm = class(TForm)
ResultsLabel: TLabel;
OKButton: TButton;
procedure OKButtonClick(Sender: TObject);
private
public
constructor CreateWithButton(whichButton: Integer; Owner: TComponent);
end;
constructor CreateWithButton(whichButton: Integer; Owner: TComponent);
begin
case whichButton of
1: ResultsLabel.Caption := 'You picked the first button.';
2: ResultsLabel.Caption := 'You picked the second button.';
3: ResultsLabel.Caption := 'You picked the third button.';
end;
end;
procedure TMainForm.SecondButtonClick(Sender: TObject);
var
rf: TResultsForm;
begin
rf := TResultsForm.CreateWithButton(2, self);
rf.ShowModal;
rf.Free;
end;
|
Don't feel bad if you can not figure out what's missing/wrong with the example. This code was posted on Borland's news group as a solution for a posted question. Instead of trying to figure out the errors, look at the correct code:
type
TResultsForm = class(TForm)
ResultsLabel: TLabel;
OKButton: TButton;
private
public
constructor Create(whichButton: Integer; Owner: TComponent); reintroduce;
end;
constructor TResultsForm.Create(whichButton: Integer; Owner: TComponent);
begin
inherited Create(Owner) ;
case whichButton of
1: ResultsLabel.Caption := 'You picked the first button.';
2: ResultsLabel.Caption := 'You picked the second button.';
3: ResultsLabel.Caption := 'You picked the third button.';
end;
end;
procedure TMainForm.SecondButtonClick(Sender: TObject);
var
f: TResultsForm ;
begin
f := TResultsForm.Create(2, self);
try
f.ShowModal;
finally
f.Release ;
end;
end;
|
Some exercises for you...
This time an easy one: download the example Delphi project for this chapter and try out the code without having to type it in yourself. The source units also expand some concepts beyond the scope of this chapter and provide more technical explanations.
To the next chapter: A Beginner's Guide to Delphi Programming
This is the end of the fifteenth chapter, in the next chapter, we'll take a small brake from forms. Delphi Personal edition does not offer database support. In the next chapter, you will find out how to create your own *flat* database and store any kind of data - all without a single data aware component.
If you need any kind of help at this point, please post to the Delphi Programming Forum where all the questions are answered and beginners are treated as experts.
First page > Finding out how a modal form was closed > Page 1, 2, 3
v