TAdvStringGrid

Example 55 : almost codeless interface to use any TWinControl as inplace editor for TAdvStringGrid

vcl grid editing

Before opening or running the sampe application, make sure to add the included file FRMCTRLLINK.PAS to the package file in which TAdvStringGrid is installed. This installs TFormControlEditLink in the component palette in the TMS Grids tab. The TFormControlEditLink is the component that performs the magic to link any TWinControl you can drop on a form and use it as inplace editor for TAdvStringGrid.

Using TFormControlEditLink:

Drop the control you want to use as inplace editor in TAdvStringGrid on the form. You can set the properties of the control according to your needs and handle events by writing event handlers for this control. Drop a TFormControlEditLink on this form and set its Control property to the TWinControl descendent control you have placed on the form. Using this custom editor in TAdvStringGrid is specified through the OnGetEditorType event. The example below shows how to use a DriveComboBox and panel with 2 edit controls as inplace editors through 2 TFormControlEditLink components:

procedure TForm1.AdvStringGrid1GetEditorType(Sender: TObject; ACol,
  ARow: Integer; var AEditor: TEditorType);
begin
  if ARow in [1,2] then
    AEditor := edCustom;

  if ARow = 1 then
    AdvStringGrid1.EditLink := FormControlEditLink2;
  if ARow = 2 then
    AdvStringGrid1.EditLink := FormControlEditLink1;
end;

The TFormControlEditLink component has 2 events through which the value of the cell is set in the inplace edit control and the value of the inplace edit control is put back into the grid.

For the TFormEditLink with the DriveComboBox this is simply:

procedure TForm1.FormControlEditLink1GetEditorValue(Sender: TObject;
  Grid: TAdvStringGrid; var AValue: String);
begin
  AValue := DriveComboBox1.Text;
end;

procedure TForm1.FormControlEditLink1SetEditorValue(Sender: TObject;
  Grid: TAdvStringGrid; AValue: String);
begin
  DriveCombobox1.Text := AValue;
end;

In this sample, a panel is used with 2 edit controls on it. The 2 edit controls are used to edit 2 lines of the grid cell. Therefore the first line is mapped to the first edit control on the panel, the second line to the second edit control. These procedures perform the mapping:

procedure TForm1.FormControlEditLink2GetEditorValue(Sender: TObject;
  Grid: TAdvStringGrid; var AValue: String);
begin
  AValue := Edit1.Text + #13 + Edit2.Text;
end;

procedure TForm1.FormControlEditLink2SetEditorValue(Sender: TObject;
  Grid: TAdvStringGrid; AValue: String);
begin
  if pos(#13,AValue) > 0 then
  begin
    Edit1.Text := copy(AValue,1,pos(#13,AValue)-1);
    Edit2.Text := copy(AValue,pos(#13,AValue)+1,length(AValue));
  end
  else
  begin
    Edit1.Text := AValue;
    Edit2.Text := '';
  end;
end;

Two additional events are provided in the TFormControlEditLink:

OnSetEditorFocus

This event is triggered after the control has been placed into the grid cell and given focus.

OnSetEditorProperties

This event is triggered after the control has been placed in the grid cell and allows to customize some properties when the control is ready to be displayed and gain focus.

Delphi project & source files for downloading included in the main demos distribution for Delphi.