TAdvStringGrid

Example 76 : Balloon functionality in TAdvStringGrid

vcl grid balloon

From TAdvStringGrid version v4.5, several new balloon notification capabilities have been added. A balloon hint can be shown dynamically using the event OnCellBalloon, it can be programmatically added using the method grid.AddBalloon() and the balloon can also be used to signal invalid entries during editing of the grid.

With this first code snippet, the balloon is set dynamically via the event OnCellBalloon. This event has 3 parameters: ATitle sets the title of the balloon, the AText parameter sets the notes of the balloon and the AIcon sets the icon to display. The icon can have following values:
  • 0: no icon
  • 1: information icon
  • 2: warning icon
  • 3: error icon
The default icon is the information icon. This code snippet shows how a balloon is defined for cell 1,1 via this event:

procedure TForm2.AdvStringGrid1CellBalloon(Sender: TObject; ACol, ARow: Integer;
  var ATitle, AText: string; var AIcon: Integer);
begin
  if (acol = 1) and (arow = 1) then
  begin
    ATitle := 'Hello';
    AText := 'I am cell 1,1';
  end;
end;
When both ATitle and AText are empty, no balloon will be displayed.
A second way to add a balloon for a cell is via the method grid.AddBalloon(). This sample code defines a balloon for cell 2,2 and cell 3,3:

procedure TForm2.FormCreate(Sender: TObject);
begin
  advstringgrid1.AddBalloon(2,2,'Title A','Cell 2,2 is here', biError);
  advstringgrid1.AddBalloon(3,3,'Title B','Cell 3,3 is here', biWarning);
end;

Another interesting capability of balloons is to use the balloon as notifier for invalid entries during editing. The balloon can be set to notify from the OnCellValidate event that is triggered when editing ends. When the Valid parameter of the OnCellValidate event is set to false the balloon will show when grid.InvalidEntryTitle, grid.InvalidEntryText are a non empty text. Additionally, the icon can be set via grid.InvalidEntryIcon. The OnCellValidate event handler here shows how a different balloon invalid entry text is set when the length of the input is smaller than 3 or larger than 5:

procedure TForm2.AdvStringGrid1CellValidate(Sender: TObject; ACol,
  ARow: Integer; var Value: string; var Valid: Boolean);
begin
  if length(value) < 3 then
  begin
    advstringgrid1.InvalidEntryTitle := 'Input error';
    Advstringgrid1.InvalidEntryText := 'Entry not sufficiently long';
    Valid := false;
  end;

  if length(value) > 5 then
  begin
    advstringgrid1.InvalidEntryTitle := 'Input error';
    Advstringgrid1.InvalidEntryText := 'Entry is too long';
    Valid := false;
  end;
end;

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