导航:
论坛 -> DELPHI技术
斑竹:liumazi,sephil
作者:
2022/6/21 23:26:28
标题:
my StringGrid / Grid = TCustomGrid with some hacks (Popup, Checkbox and others) in FMX
浏览:471
加入我的收藏
楼主:
Here some "hacks" that you can do with your sub-class TCustomGrid (StringGrid / Grid ) in FMX (Firemonkey) ---------- Using a DoSetValue "private" method: ---------- type TForm1 = class(TForm) Grid1: TGrid; StringGrid1: TStringGrid; Button1: TButton; BindingsList1: TBindingsList; PrototypeBindSource1: TPrototypeBindSource; LinkGridToDataSourcePrototypeBindSource1: TLinkGridToDataSource; PrototypeBindSource2: TPrototypeBindSource; LinkGridToDataSourcePrototypeBindSource2: TLinkGridToDataSource; procedure Button1Click(Sender: TObject); procedure FormCreate(Sender: TObject); private procedure MyGridOrStringGridSelectCell(Sender: TObject; const ACol, ARow: Integer; var CanSelect: Boolean); public { Public declarations } end; var Form1: TForm1; implementation {$R *.fmx} type TMyHackTGridModel = class(TGridModel); var AGridColSelected: Integer; AGridRowSelected: Integer; // AStringGridColSelected: Integer; AStringGridRowSelected: Integer; procedure TForm1.FormCreate(Sender: TObject); var MyStringColumn: TStringColumn; begin AGridColSelected := 0; AGridRowSelected := 0; AStringGridColSelected := 0; AStringGridColSelected := 0; // MyStringColumn := TStringColumn.Create(Grid1); MyStringColumn.Index := 0; MyStringColumn.Width := 200; MyStringColumn.Parent := Grid1; Grid1.Options := Grid1.Options + [TGridOption.RowSelect]; Grid1.RowCount := 20; // MyStringColumn := TStringColumn.Create(StringGrid1); MyStringColumn.Index := 0; MyStringColumn.Width := 200; MyStringColumn.Parent := StringGrid1; StringGrid1.Options := StringGrid1.Options + [TGridOption.RowSelect]; StringGrid1.RowCount := 20; // Grid1.OnSelectCell := MyGridOrStringGridSelectCell; StringGrid1.OnSelectCell := Grid1.OnSelectCell; end; procedure MyEditGrids(AGridOrStringGrid: TCustomGrid; ACol, ARow: Integer); var AValue: string; begin if (AGridOrStringGrid = nil) then exit; // AValue := 'hello world: ' + DateTimeToStr(now); // Form1.Caption := 'Rows: ' + AGridOrStringGrid.RowCount.ToString; // try AGridOrStringGrid.BeginUpdate; // AGridOrStringGrid.Model.EditorMode := true; // AGridOrStringGrid.SelectCell(ACol, ARow); // TMyHackTGridModel(AGridOrStringGrid.Model).DoSetValue(ACol, ARow, AValue); // calls OnSetValue(...); // AGridOrStringGrid.Model.PostEditorValue; // AGridOrStringGrid.Model.EditorMode := false; // Form1.Caption := Form1.Caption + ', Selected=' + AGridOrStringGrid.Selected.ToString; finally AGridOrStringGrid.EndUpdate; end; end; procedure TForm1.Button1Click(Sender: TObject); begin MyEditGrids(Grid1, AGridColSelected, AGridRowSelected); // MyEditGrids(StringGrid1, AStringGridColSelected, AStringGridRowSelected); end; procedure TForm1.MyGridOrStringGridSelectCell(Sender: TObject; const ACol, ARow: Integer; var CanSelect: Boolean); begin if not(Sender is TGrid) and not(Sender is TStringGrid) then exit; // if (Sender is TGrid) then begin AGridColSelected := ACol; AGridRowSelected := ARow; end else begin AStringGridColSelected := ACol; AStringGridRowSelected := ARow; end; end; end.
此帖子包含附件: 大小: 30.2K
----------------------------------------------
The higher the degree, the greater the respect given to the humblest!
作者:
2022/6/21 23:27:42
1楼:
Hide the "Scrollbars" : ---------- implementation {$R *.fmx} type TMyHack = class(TCustomGrid); procedure TForm1.FormCreate(Sender: TObject); begin TMyHack(StringGrid1).ShowScrollBars := false; // or... // TMyHack(StringGrid1).ScrollDirections := TScrollDirections.Horizontal; // TMyHack(StringGrid1).ScrollDirections := TScrollDirections.Vertical; // TMyHack(StringGrid1).ScrollDirections := TScrollDirections.Both; // // OR... no needs "hack it" StringGrid1.Model.ShowScrollBars := false; // StringGrid1.RowCount := 10; StringGrid1.Columns[0].Width := 20; // GlyphColumn StringGrid1.Columns[1].Width := 100; // StringColumn StringGrid1.Options := StringGrid1.Options - [TGridOption.ColumnResize, TGridOption.Header]; // for var i: integer := 0 to 9 do begin StringGrid1.Cells[0, i] := i.ToString; StringGrid1.Cells[1, i] := i.ToString; end; // end;
此帖子包含附件: 大小: 57.6K
----------------------------------------------
The higher the degree, the greater the respect given to the humblest!
作者:
2022/6/21 23:29:09
2楼:
StringGrid1.Model.RowHeight := 64; StringGrid1.Columns[0].Width := 64; // GlyphColumn ... using StyleBook = Dark style
此帖子包含附件: 大小: 56.9K
----------------------------------------------
The higher the degree, the greater the respect given to the humblest!
作者:
2022/6/21 23:34:57
3楼:
using a "transparence" on background StringGrid using TStyleBook changes -- change the property "ClipChildren" on "Content" object in "Background" from GridStyle object on hierarchy in StyleBook ...
此帖子包含附件: 大小: 19.6K
----------------------------------------------
The higher the degree, the greater the respect given to the humblest!
作者:
2022/6/21 23:38:47
4楼:
Create a Popup custom with others components like CheckBoxes: ---------- Popup1: TPopup; Rectangle1: TRectangle; CheckBox1: TCheckBox; CheckBox2: TCheckBox; CheckBox3: TCheckBox; SpeedButton1: TSpeedButton; ... procedure MyOnMouseDown(Sender: TObject; Button: TMouseButton; Shift: TShiftState; X, Y: Single); ... implementation type TMyHackStringGrid = class(TStringGrid); ... OnCreate form: TMyHackStringGrid(StringGrid1).OnMouseDown := MyOnMouseDown; ... procedure TForm1.MyOnMouseDown(Sender: TObject; Button: TMouseButton; Shift: TShiftState; X, Y: Single); begin if (ssRight in Shift) then begin StringGrid1.CellByPoint(X, Y, AStringGridColSelected, AStringGridRowSelected); // Popup1.IsOpen := true; end; end; function MyCheckBoxValues(APopup: TPopup): string; begin result := ''; // for var MyItem in APopup.Children do if MyItem is TRectangle then for var MySubItem in MyItem.Children do begin if MySubItem is TCheckBox then result := result + ',ChkBx' + TCheckBox(MySubItem).Tag.ToString + '=' + IfThen(TCheckBox(MySubItem).IsChecked, 'true ', 'false '); end; // result := result.Remove(0, 1); end; procedure TForm1.SpeedButton1Click(Sender: TObject); var MyText: string; begin MyText := MyCheckBoxValues(Popup1); // if not MyText.IsEmpty then MyEditGrids(StringGrid1, AStringGridColSelected, AStringGridRowSelected, MyText); // Popup1.IsOpen := false; end; end. NOTE: you can use "Popup" ONCLOSE() event to performe some action on close it!
此帖子包含附件: 大小: 31.3K
----------------------------------------------
The higher the degree, the greater the respect given to the humblest!
作者:
2022/6/22 10:05:46
5楼:
Great
----------------------------------------------
-
作者:
2022/6/24 18:16:29
6楼:
学习学习
----------------------------------------------
-