TAdvStringGrid

Example 30 : TAdvStringGrid and rich text inplace editing

vcl grid inplace editing

With a minimum effort, TAdvStringGrid allows rich text inplace editing. Only 2 event handlers and one property open the way to rich text editing in every cell or selected cells of TAdvStringGrid.

Specifying the rich text editing

As with all editor types, rich text inplace editing for a cell is set with the OnGetEditorType event. For the cells that need to be edited with an inplace rich text editor, just specify the edRichEdit as inplace editor:

procedure TForm1.GridGetEditorType(Sender:TObject; ACol, ARow: Integer; var AEditor: TEditorType);
begin
  AEditor := edRichEdit
end;

Rich Text formatting in inplace rich text editor

TAdvStringGrid exposes its rich text inplace editor through the property Grid.InplaceRichEdit. Through this property the selection attributes of the inplace editor can be set just as if it was a normal standalone richedit control. The button that sets the font bold style therefore is implemented in the following way:

procedure TForm1.BoldBtnClick(Sender: TObject);
begin
  if Grid.InplaceRichEdit.Visible then
    if fsBold in Grid.InplaceRichEdit.SelAttributes.Style then
       Grid.InplaceRichEdit.SelAttributes.Style := Grid.InplaceRichEdit.SelAttributes.Style - [fsBold]
   else
      Grid.InplaceRichEdit.SelAttributes.Style := Grid.InplaceRichEdit.SelAttributes.Style + [fsBold];
end;

Other settings are done in a similar way.

Updating toolbar settings from the inplace rich text editor

A new event is introduced to let toolbar settings for selected characters in the rich text editor reflect the current selected style such as fontstyle, fontname etc.. This event OnRichEditSelectionChange is triggered whenever the user changes the selection in the inplace rich editor. In this event, the toolbar button style can then set to reflect the setting of the selected text.

For example, the BoldBtn style is set in this event handler in the following way:

procedure TForm1.GridInplaceRichEditSelectionChange(Sender:TObject);
begin
  BoldBtn.Down := fsBold in Grid.InplaceRichEdit.SelAttributes.Style;
end;

Special focus considerations

Normally, whenever another control gains focus, the TAdvStringGrid inplace editor is hidden and the inplace editor text is set in the grid's cell. However, with rich text inplace editing this behaviour is not wanted. If the inplace editor would be hidden, the selection would disappear and no longer available to apply changes such as font changes. Therefore, for a rich text inplace editor the editor remains visible even when another control on the form gains focus. Some controls, such as a font selection combobox can then be used to set the selected font name. However, for other control that perform something like a grid print or preview, the rich text inplace editor should be hidden and the cell contents should be updated before doing the print. This can be done with the Grid.HideInplaceEdit method.

Example:

This is the change event for the fontname combobox:

procedure TForm1.FontNameChange(Sender:TObject);
begin
  if Grid.InplaceRichEdit.Visible then
    Grid.InplaceRichEdit.SelAttributes.Name := Fontname.Items[Fontname.ItemIndex];
end;

For the print button this is:

procedure TForm1.PrintBtnClick(Sender:TObject);
begin
  Grid.HideInplaceEdit;
  Grid.Print;
end;


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