Delphi FMX - Как остановить TStringGrid от горизонтальной и вертикальной прокрутки - PullRequest
0 голосов
/ 25 марта 2019

У меня есть TStringGrid, однако, если я нажимаю и перетаскиваю его, его можно панорамировать по вертикали и горизонтали, я не хочу, чтобы пользователь мог это сделать, как я могу предотвратить это?

1 Ответ

1 голос
/ 26 марта 2019

Вы можете использовать событие OnTopLeftChanged для перехвата всякий раз, когда происходит какая-либо «прокрутка», и решить, как поступить. Если вы не хотите, чтобы пользователь выходил за пределы диапазона при определенных обстоятельствах, вы можете сбросить диапазон по мере необходимости. Вот грубый пример ...

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.
...