TAdvStringGrid
example 5 : Combobox editors & varia
Demo 5 shows how to use the combobox editors, especially the use of different
combobox lists for different cells. But before delving into the combobox related
code, first the grid is made a little more attractive by the use of some images
in the column headers. Therefore, an Imagelist control is placed on the form,
and 3 bitmaps are added. Further, the EnableGraphics property is set true. Then
in the formcreate method, the images are assigned to the column headers cells
with: procedure TForm1.FormCreate(Sender: TObject);
var
i:integer;
begin
for i:=0 to 2 do
AdvStringGrid1.AddImageIdx(i,0,i,haBeforeText,vaTop);
end;
Now, let's delve into the combobox related code. In this case, we want the combobox displayed for column 2 to be dependent of the data in the cells in column 1, since the model of the cars is dependent of the brand. To setup this demo very simple, some files have been made : cars.dat, bmw.dat, mercedes.dat, ferrari.dat, audi.dat, vw.dat. The first file contains the names of all brands required in column 1. For each brand, a corresponding file exists with the models. The files are simple ASCII files, with a brand or model on each line. These files can be loaded in a very simple way into stringlist and that's exactly what is needed for this demo. To specify the type of inplace editor, an OnGetEditorType event handler is required. This event handler is called when editing for a specific cells is about to be started. The parameters are :
procedure TForm1.AdvStringGrid1GetEditorType(Sender: TObject; aCol, aRow: Integer; var aEditor: TEditorType);
It's the task of the program to set the parameter aEditor to either one of these values :TEditorType = (edNormal,edSpinEdit,edComboEdit,edComboList,edEditBtn,edCheckBox,edDateEdit, edTimeEdit,edButton,edDataCheckBox,edNumeric,edFloat,edCapital,edMixedCase);
For this application, a edComboList is used as it's not allowed to specify any other car or type than these from the lists. Secondly, the combobox list must be specified for each column. This is handled in the following way :procedure TForm1.AdvStringGrid1GetEditorType(Sender: TObject;
aCol, aRow: Integer; var TEditorType);
begin
with advstringgrid1 do
case acol of
1:begin
aEditor:=edComboList;
ClearComboString;
if fileexists('cars.dat') then
combobox.items.loadfromfile('cars.dat');
end;
2:begin
aEditor:=edComboList;
ClearComboString;
if (cells[1,arow]<>'') then
if fileexists(cells[1,arow]+'.dat') then
combobox.items.loadfromfile(cells[1,arow]+'.dat');
end;
end;
end;
Some other properties that affect the editing behaviour can be found in Navigation and MouseActions. For example, there is the AlwaysEdit property in Navigation which must be used instead of goAlwaysShowEdit in Options if you always want to have an inplace editor shown. The AdvanceOnEnter can be used to automatically select the next cell when enter is pressed. Furthermore, if AlwaysEdit is not required, but immediate editing is required when clicking with the mouse on the cell, use DirectEdit in MouseActions.
To show one more capability of the grid, lookup editing is turned on by the Lookup property. This means that while editing in the first column, text is automatically completed based on the values in the LookupItems list. This list can dynamically grow while editing if LookupHistory is also turned on. In this example the list is filled with the names of some CEO's of computer companies. If the letter 'P' is entered in the first cell, it will automatically suggest Philippe Khan.
Delphi project & source files for downloading included in the main demos distribution for Delphi.
The project and source files have been written with Delphi 6,7. To use these files in other versions of Delphi, ignore any remarks when opening the form files and save the files. After this, compilation can be done. The error messages are due to properties included in the Delphi 6 form file, but not available in lower versions of Delphi.


ONLINE ORDERS
Subscribe to RSS Feed