Download Delphi User's Guide

Transcript
until the value of X becomes greater than the final value, 5, and the execution of the for
statement ends.
A counting example
This is a variation of the same for loop just discussed. It uses a string grid control and a
button the user clicks to start the counting process.
➤
To create the form,
1 Place a string grid control and a button on the form.
2 Use the Object Inspector to
• Change the value of the button’s Name property to CountButton.
• Change the caption of the button to Count.
• Change the ColCount property for the string grid to 6.
• Change the RowCount property for the string grid to 6.
You might have to resize the form and the string grid so that you can see all six columns.
Your form should look like this:
Figure 5.8
A sample form to illustrate a for loop
Row 0
Column 0
➤
Create this CountButtonClick event handler:
procedure TForm1.CountButtonClick(Sender: TObject);
var
Col: Integer;
begin
for Col := 1 to 5 do
StringGrid1.Cells[Col, 1] := IntToStr(Col);
end;
➤
Run the application and click the Count button.
This event handler uses the Cells property, which is a two-dimensional array of strings
indexed by a column and row value. (If you are unfamiliar with arrays, you can read
about them starting on page 199.) Each cell in the string grid is represented in theCells
property array. To access a particular cell in the grid, you specify the cell’s column and
row value. For example, the top left cell in the grid is Cells[0,0], meaning that the cell is in
column 0 and row 0 (the first position in the Cells array is 0).
Chapter 5, Writing Object Pascal code
171