Wiliam Cardoso
Wiliam Cardoso

Reputation: 444

Delphi FMX - How to stop a TStringGrid from scrolling both horizontally and vertically

I have a TStringGrid, however if I click and drag on it, it can be panned vertically and horizontally, I don't want the user to be able to do this, how can I stop this from happening?

Upvotes: 1

Views: 1204

Answers (2)

Nostradamus
Nostradamus

Reputation: 658

To prevent panning on drag you can set the TouchTracking property of TStringGrid to TBehaviorBoolean.False

Upvotes: 0

Jerry Dodge
Jerry Dodge

Reputation: 27266

You can use the OnTopLeftChanged event to catch whenever any sort of "scrolling" has occurred, and decide how to proceed. If you don't want user to go out of range in certain circumstances, you can reset the range as needed. Here's a rough example...

uStringGridTestMain.dfm:

object frmStringGridTestMain: TfrmStringGridTestMain
  Left = 0
  Top = 0
  Caption = 'String Grid Test'
  ClientHeight = 416
  ClientWidth = 738
  Color = clBtnFace
  Font.Charset = DEFAULT_CHARSET
  Font.Color = clWindowText
  Font.Height = -11
  Font.Name = 'Tahoma'
  Font.Style = []
  OldCreateOrder = False
  OnCreate = FormCreate
  PixelsPerInch = 96
  TextHeight = 13
  object StringGrid1: TStringGrid
    Left = 72
    Top = 32
    Width = 513
    Height = 329
    Options = [goFixedVertLine, goFixedHorzLine, goVertLine, goHorzLine]
    TabOrder = 0
    OnTopLeftChanged = StringGrid1TopLeftChanged
    ColWidths = (
      64
      64
      64
      64
      64)
    RowHeights = (
      24
      24
      24
      24
      24)
  end
end

uStringGridTestMain.pas:

unit uStringGridTestMain;

interface

uses
  Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, System.Classes, Vcl.Graphics,
  Vcl.Controls, Vcl.Forms, Vcl.Dialogs, Vcl.Grids;

type
  TfrmStringGridTestMain = class(TForm)
    StringGrid1: TStringGrid;
    procedure StringGrid1TopLeftChanged(Sender: TObject);
    procedure FormCreate(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  frmStringGridTestMain: TfrmStringGridTestMain;

implementation

{$R *.dfm}

procedure TfrmStringGridTestMain.FormCreate(Sender: TObject);
begin
  StringGrid1.Align:= alClient;
  //Let's put a big scroll in both directions...
  StringGrid1.RowCount:= 50;
  StringGrid1.ColCount:= 50;
end;

procedure TfrmStringGridTestMain.StringGrid1TopLeftChanged(Sender: TObject);
begin
  //You can change the "current" cell...
  StringGrid1.Row:= 1;
  StringGrid1.Col:= 1;
  //Or you can change the scrolled cell on top-left...
  StringGrid1.TopRow:= 1;
  StringGrid1.LeftCol:= 1;
end;

end.

Upvotes: 1

Related Questions