0

I want to have a grid where all rows look the same In Delphi, how can i let the user resize TStringGrid columns without fixed rows? Normally you can only adjust the fixed rows, and you can't make the whole grid fixed.

I am using XE2.

TIA

Mark

Mark Patterson
  • 417
  • 2
  • 6
  • 15
  • columns, rows... Please, can you elaborate what exactly should and what should not be possible for user ? maybe some PNG screenshots with arrows highlighting the chanegs ? i cannot understand a bit... – Arioch 'The Oct 23 '13 at 08:14
  • http://docwiki.embarcadero.com/Libraries/XE5/en/Vcl.Grids.TDrawGrid.Options This suggests that any column and row can be sized, did you set it right ? – Arioch 'The Oct 23 '13 at 08:25
  • The issue being referred to here is that Delphi (AFIAK) will only set the mouse 'resize' cursor for columns when it is over the header row (fixed row). – Matt Allwood Oct 23 '13 at 12:18
  • Yes, Matt Allwood has identifies the issue. I want all the cells to look the same, but you can only resize columns when at least one is fixed (FixedRows := 1) and you can't show a grid with all rows fixed. I was doing grid.FixedRows := grid.RowCount and it wouldn't take that. – Mark Patterson Oct 24 '13 at 01:49
  • What I have done is add a sort of header row. I now have one fixed row and one fixed column. This has the unintended consequence that I can adjust the widths of all rows, except row 0. – Mark Patterson Oct 28 '13 at 02:52

2 Answers2

1

You might override CalcSizingState.

Set
- State to gsRowSizing if your condition is met (in the example below check if Alt key is pressed in MouseMove) and
- Index to the Calculated Index from MouseDown using MouseToCell.

Some fine tuning might be necessary.

type

  TStringGrid = Class(Grids.TStringGrid)
  private
    FIsSizing: Boolean;
    FIndex: Integer;
    procedure CalcSizingState(X, Y: Integer; var State: TGridState; var Index: Longint; var SizingPos, SizingOfs: Integer; var FixedInfo: TGridDrawInfo); override;
    procedure MouseDown(Button: TMouseButton; Shift: TShiftState; X, Y: Integer); override;
    procedure MouseMove(Shift: TShiftState; X, Y: Integer); override;
  private

  End;

  TForm3 = class(TForm)
    StringGrid1: TStringGrid;
  private
    { Private-Deklarationen }
  public
    { Public-Deklarationen }
  end;

var
  Form3: TForm3;

implementation

{$R *.dfm}
{ TStringGrid }

procedure TStringGrid.CalcSizingState(X, Y: Integer; var State: TGridState; var Index, SizingPos, SizingOfs: Integer; var FixedInfo: TGridDrawInfo);
begin
  inherited;
  if FIsSizing then
    State := gsRowSizing;
  if (FIndex > -1) then
  begin
    Index := FIndex;
  end;
end;

procedure TStringGrid.MouseDown(Button: TMouseButton; Shift: TShiftState; X, Y: Integer);
var
  Col: Integer;
begin
  inherited;
  MouseToCell(X, Y, Col, FIndex);
end;

procedure TStringGrid.MouseMove(Shift: TShiftState; X, Y: Integer);
begin
  inherited;
  FIsSizing := ssAlt in Shift;
end;
bummi
  • 27,123
  • 14
  • 62
  • 101
0

Here are some of the tuning that I did to the bummi snippet (Alt key for ColSizing and Ctrl for RowSizing):

unit UConfigPanel;

interface

uses
  System.Classes, System.SysUtils, Vcl.Grids, Vcl.Controls;

type
  TCustomStringGrid = Class(Vcl.Grids.TStringGrid)
  protected
    procedure CalcSizingState(X, Y: Integer; var State: TGridState; var Index: Longint;
      var SizingPos, SizingOfs: Integer; var FixedInfo: TGridDrawInfo); override;
    procedure MouseDown(Button: TMouseButton; Shift: TShiftState; X, Y: Integer); override;
    procedure MouseMove(Shift: TShiftState; X, Y: Integer); override;
  private
    FColSizing: Boolean;
    FRowSizing: Boolean;
    FCol: Integer;
    FRow: Integer;
  public
    property ColSizing: Boolean read FColSizing;
    property RowSizing: Boolean read FRowSizing;
  end;      

{ TCustomStringGrid }

procedure TCustomStringGrid.CalcSizingState(X, Y: Integer; var State: TGridState;
  var Index: Longint; var SizingPos, SizingOfs: Integer;
  var FixedInfo: TGridDrawInfo);
begin
  inherited;
  if FColSizing then
  begin
    State := gsColSizing;
    if (FCol > -1) then
      Index := FCol;
  end
  else
  if FRowSizing then
  begin
    State := gsRowSizing;
    if (FRow > -1) then
      Index := FRow;
  end
end;

procedure TCustomStringGrid.MouseDown(Button: TMouseButton; Shift: TShiftState; X,
  Y: Integer);
begin
  inherited;
  MouseToCell(X, Y, FCol, FRow);
end;

procedure TCustomStringGrid.MouseMove(Shift: TShiftState; X, Y: Integer);
begin
  inherited;
  FColSizing := ssAlt in Shift;
  FRowSizing := ssCtrl in Shift;
end;

end.