TAdvStringGrid

Example 79 : Adding scrollbars on grid cells

vcl grid scrollbars

When text is too large to fit in a cell, you could increase the cell size, ie. increase the row height, the column width or both. This is not always practical, as the higher the row height is, the less rows can be displayed. The same applies for the column width. The text could also be displayed cut-off but this solution is also far from ideal. Now, an alternative solution is available with the capability to add a vertical scrollbar to a cell. Any cell can as such have its own scrollbar and scroll separately the cell's text or HTML formatted text. This feature is made available with the grid.AddScrollbar() method. With this method a scrollbar is added to a cell. The scrollbar range and pagesize can either be automatically set according to the size of the text in a cell or can be programmatically set.
This is an overview of the methods available:
grid.AddScrollbar(col,row: integer; AutoRange: boolean);Adds a scrollbar to cell col,row and when AutoRange is true, the scrollbar range and pagesize is automatically calculated.
grid.RemoveScrollbar(col,row: integer); Removes the scrollbar again from the cell
grid.HasScrollBar(col,row: integer): booleanReturns true when the cell col,row has a scrollbar
grid.HasAutoRangeScrollBar(col,row: integer): booleanReturns true when the cell col,row has an autorange scrollbar
grid.SetScrollPosition(col,row,position: integer);Programmatically sets the position of the scrollbar in cell col,row
grid.GetScrollPosition(col,row,position: integer): integer;Programmatically gets the position of the scrollbar in cell col,row
grid.SetScrollProp(col,row: integer; Prop: TScrollProp);Sets the scrollbar range & pagesize
grid.GetScrollProp(col,row: integer): TScrollProp;Gets the scrollbar range & pagesize

Delphi project & source files for downloading included in the main demos distribution for Delphi
In the sample, these capabilities are demonstrated by adding very long HTML formatted text in 3 cells of the grid. In these cells, first an autorange scrollbar was added with:

  advstringgrid1.AddScrollBar(2,1,true);
  advstringgrid1.AddScrollBar(2,2,true);
  advstringgrid1.AddScrollBar(2,3,true);

When text is set in a cell, the scrollbar will then appear when necessary. In this sample, it is also demonstrated how the scrollbar position can be programmatically set. This is done from an UpDown control for the cell that is selected. Upon cell selection, the UpDown control is initialized to the cell's scrollbar range:

procedure TForm2.AdvStringGrid1SelectCell(Sender: TObject; ACol, ARow: Integer;
  var CanSelect: Boolean);
var
  sp: TScrollProp;
begin
  if AdvStringgrid1.HasScrollBar(Acol,ARow) then
  begin
    sp := AdvStringgrid1.GetScrollProp(ACol,ARow);
    updown1.Min := 0;
    updown1.Max := sp.Range;
    updown1.Position := sp.Range - AdvStringgrid1.GetScrollPosition(ACol,ARow);
  end;
end;


procedure TForm2.UpDown1Changing(Sender: TObject; var AllowChange: Boolean);
var
 sp: TScrollProp;

begin
  if AdvStringGrid1.HasScrollBar(AdvStringgrid1.Col, AdvStringGrid1.Row) then
  begin
    AdvStringGrid1.SetScrollPosition(AdvStringgrid1.Col, AdvStringGrid1.Row, updown1.Max - updown1.Position);
  end;
end;



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