Как добавить в TGrid TSwitch, так чтобы в каждой ячейке Switch всегда показывался и отображал значение массива?
Те примеры что находил в инете не пошли. Может из-за того что у меня последняя версия.
Сделал более менее рабочий пример.
К тому же не могу побороть ошибку, когда курсор выходит за границы колонок, на пустой области.
unit fmx_c;
interface
uses
System.SysUtils, System.Types, System.UITypes, System.Classes, System.Variants,
FMX.Types, FMX.Controls, FMX.Forms, FMX.Graphics, FMX.Dialogs, FMX.Objects,
System.Rtti, FMX.Grid, FMX.Layouts, FMX.StdCtrls, FMX.Controls.Presentation,
FMX.Edit, FMX.Memo;
type
Rgrid = record
col_1 : Tvalue;
col_2 : Tvalue;
col_3 : Boolean;
end;
TSwitchCell=class(TSwitch)
procedure DoSwitch; override;
procedure DoMouseEnter; override;
//constructor Create (AOwner : TComponent); override;
end;
TSwitchColumn=class(TColumn)
function CreateCellControl : TStyledControl; override;//virtual;
//constructor Create (AOwner : TComponent); override;
end;
TForm1 = class(TForm)
SGrid: TGrid;
cInfo: TMemo;
procedure FormCreate(Sender: TObject);
procedure SGridSetValue(Sender: TObject; const Col, Row: Integer;
const Value: TValue);
procedure SGridGetValue(Sender: TObject; const Col, Row: Integer;
var Value: TValue);
procedure SGridMouseMove(Sender: TObject; Shift: TShiftState; X, Y: Single);
private
{ Private declarations }
public
{ Public declarations }
end;
var
Form1: TForm1;
Agrid : array of Rgrid;
SgX,SgY : single;
SgCol,SgRow : integer;
implementation
{$R *.fmx}
{constructor TSwitchCell.Create(AOwner: TComponent);
begin
inherited;
end;}
{constructor TSwitchColumn.Create(AOwner: TComponent);
begin
inherited;
end;}
procedure TSwitchCell.DoMouseEnter;//.DoBeginUpdate
begin
if SgCol=2 then
begin
//showmessage(self.parent.parent.parent.ClassName);
( TGrid(self.parent.parent.parent).Columns[SgCol].Controls.Items[0] as TSwitchCell).IsChecked:=Agrid[SgRow].col_3;
end;
end;
procedure TSwitchCell.DoSwitch;
begin
Agrid[SgRow].col_3:=self.IsChecked;
Form1.cInfo.Lines.Add( 'DoSwitch Row='+inttostr(SgRow)+' value='+booltostr(Agrid[SgRow].col_3));
Form1.cInfo.GoToTextEnd;
end;
function TSwitchColumn.CreateCellControl : TStyledControl;
begin
//inherited;
Result:=TSwitchcell.Create(Self);
TSwitchCell(Result).OnSwitch:=self.DoTextChanged;
TSwitchCell(Result).OnExit:=self.DoTextExit;
end;
procedure TForm1.FormCreate(Sender: TObject);
var
lc3 : TSwitchColumn;
begin
Sgrid.AddObject(TStringColumn.Create(self) );
Sgrid.Columns[0].Header:='кол-1';
Sgrid.Columns[0].Name:='c1';
Sgrid.AddObject(TStringColumn.Create(self) );
Sgrid.Columns[1].Header:='кол-2';
Sgrid.Columns[1].Name:='c2';
lc3:=TSwitchColumn.Create(Sgrid);
Sgrid.AddObject(TSwitchColumn.Create(lc3));
Sgrid.Columns[2].Header:='кол-3';
Sgrid.Columns[2].Name:='c3';
Sgrid.RowCount:=5;
SetLength(Agrid,Sgrid.RowCount);
Agrid[0].col_1:='1';
Agrid[0].col_2:='11';
Agrid[0].col_3:=True;
Agrid[1].col_1:='2';
Agrid[1].col_2:='22';
Agrid[1].col_3:=False;
SGrid.UpdateColumns;
end;
procedure TForm1.SGridGetValue(Sender: TObject; const Col, Row: Integer;
var Value: TValue);
begin
case Col of//lIndexCol
0:Value:=Agrid[Row].col_1;
1:Value:=Agrid[Row].col_2;
2:begin
Value:=tvalue(Agrid[Row].col_3);
end;
end;
end;
procedure TForm1.SGridSetValue(Sender: TObject; const Col, Row: Integer;
const Value: TValue);
begin
case Col of
0:Agrid[Row].col_1:=Value;
1:Agrid[Row].col_2:=Value;
2:begin
Agrid[Row].col_3:=(Sgrid.Columns[Col].Controls.Items[0] as TSwitchCell).IsChecked;
end;
end;
//end;
end;
procedure TForm1.SGridMouseMove(Sender: TObject; Shift: TShiftState; X,
Y: Single);
var lcol : tColumn;
begin
SgX:=X;
SgY:=Y;
lcol:=TGrid(Sender).ColumnByPoint(x,y) as tColumn;
//if lcol.BoundsRect.Contains(tpointF.Create(x,y) ) then
SgCol:=lcol.Index;// Если курсор выходит за границы области в гриде где колонки - то происходит ошибка. !?
//else
// SgCol:=-1;
SgRow:=TGrid(Sender).RowByPoint(x,y);
Form1.cInfo.Lines.Add('GridMouseMove Col='+inttostr(SgCol)+' Row='+inttostr(SgRow));
Form1.cInfo.GoToTextEnd;
end;
end.
fmx_sample.zip Proj.zip