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