簡體   English   中英

Delphi FMX - 如何阻止 TStringGrid 水平和垂直滾動

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

我有一個TStringGrid ,但是如果我點擊並拖動它,它可以垂直和水平平移,我不希望用戶能夠這樣做,我該如何阻止這種情況發生?

您可以使用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.

為了防止在拖動時平移,您可以將 TStringGrid 的 TouchTracking 屬性設置為 TBehaviorBoolean.False

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM