Vous êtes sur la page 1sur 5

S t r i n g G r i d

C o n t r o l

This object allow s you to display or collect string data in a grid format. Row 0 and Column 0 are fixed and are generally used for headings. The number of fixed columns and row s can be increased to add more headings. The Fixed Column count must be less than the total column count. The fixed columns and row s cannot be edited w hen a program is running. You don't have to have fixed columns, but they are useful for headings.

S i g n i f i c a n t
C o l o r
Controls the colour of the cells.

P r o p e r t i e s

C o l W i d t h s
Set w idth of individual column at run-time e.g. stringgrid1.colw idths[2]:=120;

D e f a u l t R o w H e i g h t ,

D e f a u l t C o l W i d t h

Change default row height & column w idth. Change it at run-time w ith something like this: stringgrid1.defaultcolw idth:=120;

F i x e d C o l o r
Controls the colour of the fixed cells.

F i x e d C o l s
Set this to 1 to get a column of cells for labels.

F i x e d R o w
Set this to 1 to get a row of cells for labels.

F o n t
Set the font before run-time or change it during execution w ith e.g. stringgrid1.font.name:='Courier New '; stringgrid1.cells[0,0]:='Hello';

G r i d L i n e W i d t h
Used to change the w idth of grid border lines, e.g.
converted by Web2PDFConvert.com

if Draw Grid1.DefaultColW idth > 90 then StringGrid1.GridLineW idth := 2 else StringGrid1.GridLineW idth := 1;

O p t i o n s
GoEditing - set this to true to allow editing of cells. GoRow Sizing, GoColSizing - set these to true to allow re-sizing of columns and row s.

R o w C o u n t
Number of row s in grid.

S c r o l l B a r s
W hether present.

T a b S t o p

T S t r i n g G r i d . C e l l s [ c o l u m n , r o w ]
Use this to access the string inside an individual grid cell, to set it, read it or compare it w ith something else.

T S t r i n g G r i d . C o l s [ c o l u m n ] ,
Use this to set all cells in a column.

T S t r i n g G r i d . R o w s [ r o w ] ,

T S t r i n g G r i d . R o w C o u n t ,

T S t r i n g G r i d . C o l C o u n t

These can be used to change the number of row s and columns w hen a program is running. e.g. stringgrid1.row count:=rcount+1;

E x a m p l e

1 :

D i s p l a y i n g

R e c o r d

F r o m

F i l e

We can display a record in a string grid as follow s: procedure TForm1.DisplayButtonClick(Sender: TObject); var person:TPerson; personfile:TPersonfile; i,j:integer; begin j:=1; assignfile(personfile,'persons.dat'); reset(personfile); w hile not eof(personfile) do begin i:=1; read(personfile,person); stringgrid1.cells[i,j]:=person.name; inc(i); //increment string grid column pointer for next field stringgrid1.Cells[i,j]:=person.address; inc(i); //increment string grid column pointer for next field stringgrid1.Cells[i,j]:=datetostr(person.dateofbirth); inc(j); //increment string grid row pointer for next record end; end; This w ill require the declaration of TPerson and TPersonfile in the type section of the Interface section of the unit (as done previously , basically a record structure w ith name, address and date of birth). The variables person and personfile in the code above could also be declared in the var section of the Form class. W hen you create this program save it in a new directory and copy the file persons.dat that you made for the earlier program - this should w ork w ith the new code, all w e have done is add a file open dialog box to access it.

E x a m p l e

2 :

S i m p l e

L i s t

In the follow ing example a string grid has been used to provide a w ay to collect data for a file (from the PostQuickParcels project).

converted by Web2PDFConvert.com

The data type for the file is simple: Type Tprice_item=string[5]; The code to set up the string grid is: procedure TPriceEdit.FormActivate(Sender: TObject); begin w ith stringgrid1 do begin cells[1,0]:='Price'; cells[0,0]:='Weight'; cells[0,1]:=' <2kg.'; cells[0,2]:=' 2-4kg.'; cells[0,3]:=' 4-6kg.'; cells[0,4]:=' 6-8kg.'; cells[0,5]:=' 8-10kg.'; cells[0,6]:=' 10-12kg.'; cells[0,7]:=' 12-14kg.'; cells[0,8]:=' 14-16kg.'; cells[0,9]:=' 16-18kg.'; cells[0,10]:=' 18-20kg.'; cells[0,11]:=' 20-22kg.'; cells[0,12]:=' 22-24kg.'; cells[0,13]:=' 24-26kg.'; cells[0,14]:=' 26-28kg.'; cells[0,15]:=' 28-30kg.'; end; end; The code to load the file into the string grid is: procedure TPriceEdit.LoadButtonClick(Sender: TObject); var price_record:Tprice_item; pricefile:file of Tprice_item; i:integer; begin assignfile(pricefile,'x:\pricefile.dat'); reset(pricefile); i:=1; w hile not eof (pricefile) do begin read(pricefile,price_record); stringgrid1.Cells[1,i]:=price_record; inc(i); end; closefile(pricefile); end; The code to save the contents of the string grid to the file is: procedure TPriceEdit.SaveButtonClick(Sender: TObject); var price_record:Tprice_item; pricefile:file of Tprice_item; i:integer; begin assignfile(pricefile,'n:\pricefile.dat'); reset(pricefile); for i:= 1 to 15 do begin price_record:=StringGrid1.Cells[1,i]; w rite (pricefile, price_record);

converted by Web2PDFConvert.com

end; closefile(pricefile); end;

E x a m p l e

S t r u c t u r e d

D a t a

The principle of choosing a name from a list and displaying an associated data file can be extended to a w ide range of situations and applications. The 'Edit Data File' button opens a second form w ith a string grid to provide a means of entering data into the data file:

Here is the code for loading the data file into the string grid: procedure TStarsEdit.Button1Click(Sender: TObject); var starrecord:Tstar; starfile:file of Tstar; i:integer; begin assignfile(starfile,'starfile.dat'); reset(starfile); i:=1; w hile not eof (starfile) do begin read(starfile,starrecord); stringgrid1.Cells[1,i]:=starrecord.name; stringgrid1.Cells[2,i]:=starrecord.group_solo; stringgrid1.Cells[3,i]:=starrecord.act_type; stringgrid1.Cells[4,i]:=starrecord.nationality; stringgrid1.Cells[5,i]:=starrecord.datafile; inc(i); end; closefile(starfile); end; And here is the code for saving edited data to the data file: procedure TStarsEdit.Button2Click(Sender: TObject); var starrecord:Tstar; starfile:file of Tstar; i:integer; begin assignfile(starfile,'starfile.dat'); reset(starfile); for i:= 1 to stringgrid1.Row Count do begin starrecord.name:=StringGrid1.Cells[1,i]; starrecord.group_solo:=StringGrid1.Cells[2,i]; starrecord.act_type:=StringGrid1.Cells[3,i]; starrecord.nationality:=StringGrid1.Cells[4,i]; starrecord.datafile:=StringGrid1.Cells[5,i]; w rite (starfile, starrecord); end; closefile(starfile); end; The technique of loading names from a file into a ComboBox can be found on the ComboBox page.

E x a m p l e

In this example w e see how to fill in a cell in a string grid by clicking on it. This has many possibilities, such as colour-coding a grid according to w hether a facility is booked or not. A user w ould need additional facilities such as changing the colour back to w hite and changing font colours as w ell. Changing the colour of cells could also lead to the development of something like the 'Game of Life'.
converted by Web2PDFConvert.com

procedure TForm1.StringGrid1MouseUp(Sender: TObject; Button: TMouseButton; Shift: TShiftState; X, Y: Integer); var v,w :integer; arect:TRect; begin stringgrid1.MouseToCell(X,Y,v,w ); arect:=stringgrid1.CellRect(v,w ); stringgrid1.Canvas.Brush.Color:=clred; stringgrid1.Canvas.FillRect(arect); end; The DefaultDraw ing property should be set to True if the colour is to remain after a new cell is clicked.

E x a m p l e

In this example w e use the OnMouseUp event of a string grid to set the contents of a cell. This is a fragment from a project on a hotel bookings system. W hen a cell is clicked the code examines the contents of the cell to see if it is available, in w hich case the w ord 'booked' is inserted; if it is already booked the contents w ill be cleared. procedure TForm1.StringGrid1MouseUp(Sender: TObject; Button: TMouseButton; Shift: TShiftState; X, Y: Integer); var Column, Row : Longint; begin StringGrid1.MouseToCell(X, Y, Column, Row ); if stringgrid1.Cells[Column, Row ] = 'Booked' then stringgrid1.cells[Column, Row ]:= ' ' else stringgrid1.cells[Column, Row ]:= 'Booked'; end; Back to Tutorial Back to Palette List

converted by Web2PDFConvert.com

Vous aimerez peut-être aussi