Recent

Author Topic: Some stringgrids questions  (Read 2754 times)

torbente

  • Sr. Member
  • ****
  • Posts: 325
    • Noso Main Page
Some stringgrids questions
« on: March 26, 2015, 10:01:50 pm »
Ok, i created an aplicattion with 5 stringgrids in its form. I will need a lot of functions and procedures to manage those grids so i want create a new unit to do that, but i dont know how i could make the grids accesible from the new unit. I believe it could be done, but i dont remember how.

Also, i want know if there is any good way to sent a stringgrid to the printer. Any help?
Noso Cryptocurrency Main Developer
https://github.com/DevTeamNoso/NosoWallet

Blaazen

  • Hero Member
  • *****
  • Posts: 3237
  • POKE 54296,15
    • Eye-Candy Controls
Re: Some stringgrids questions
« Reply #1 on: March 26, 2015, 11:33:38 pm »
If you created a new unit with a new class derived from TStringGrid, you have two possibilities:

1) create the derived stringgrids at runtime (in code)
2) create package (new visual component) with your grid

Anyway, you need to add your unit to uses. If your unit need back access to your main unit, you need to add it to uses too, but it must be uses in implementation section of your unit, to avoid circular dependency.
Lazarus 2.3.0 (rev main-2_3-2863...) FPC 3.3.1 x86_64-linux-qt Chakra, Qt 4.8.7/5.13.2, Plasma 5.17.3
Lazarus 1.8.2 r57369 FPC 3.0.4 i386-win32-win32/win64 Wine 3.21

Try Eye-Candy Controls: https://sourceforge.net/projects/eccontrols/files/

howardpc

  • Hero Member
  • *****
  • Posts: 4144
Re: Some stringgrids questions
« Reply #2 on: March 27, 2015, 12:29:12 am »
Here's a small example. On the main form of a new Lazarus project drop five stringgrids. Double-click the form to generate an OnCreate handler and complete the code as follows:
Code: [Select]

unit mainMultipleGrids;

{$mode objfpc}{$H+}

interface

uses
  Forms, Grids, sysutils;

type

  { TmultiGridForm }

  TmultiGridForm = class(TForm)
    StringGrid1: TStringGrid;
    StringGrid2: TStringGrid;
    StringGrid3: TStringGrid;
    StringGrid4: TStringGrid;
    StringGrid5: TStringGrid;
    procedure FormCreate(Sender: TObject);
  end;

var
  multiGridForm: TmultiGridForm;

implementation

uses gridManager;

{$R *.lfm}

{ TmultiGridForm }

procedure TmultiGridForm.FormCreate(Sender: TObject);
var
  i, c, r: integer;
  grid: TStringGrid;
begin
  Randomize;
  for i:=0 to ControlCount-1 do
    if (Controls[i] is TStringGrid) then
      begin
        grid:=TStringGrid(Controls[i]);
        for r:=0 to grid.RowCount-1 do
          for c:=0 to grid.ColCount-1 do
            grid.Cells[c,r]:=IntToStr(Random(1000));
      end;

  manager.GridForm:=Self;
  Caption:=Format('Cells sum = %d',[manager.ForEachCellDo]);
end;

end.

Then create a new unit (not a new form) named gridManager and complete its code as follows:
Code: [Select]
unit gridManager;

{$mode objfpc}{$H+}

interface

uses
  SysUtils, Forms, Grids;

type

  { TGridManager }

  TGridManager = class(TObject)
  private
    FGridForm: TForm;
  public
    function ForEachCellDo: integer;
    property GridForm: TForm write FGridForm;
  end;

  var
    manager: TGridManager = nil;

implementation

{ TGridManager }

function TGridManager.ForEachCellDo: integer;
var
  i, r, c, n: integer;
  grid: TStringGrid;
begin
  Result:=0;
  for i:=0 to FGridForm.ControlCount-1 do
    if (FGridForm.Controls[i] is TStringGrid) then begin
      grid:=TStringGrid(FGridForm.Controls[i]);
      for r:=0 to grid.RowCount-1 do
        for c:=0 to grid.ColCount-1 do
          if TryStrToInt(Trim(grid.Cells[c,r]), n) then
            Inc(Result, n);
    end;
end;

initialization
  if manager = nil then
    manager:=TGridManager.Create;

finalization
  FreeAndNil(manager);

end.

To print a grid, AFAIK you have to do the grunt work yourself:
- figure out printer page size
- calculate the maximum number of print lines possible for your font size per page
- format your grid data as line-based text
- send your data lines to the printer a page at a time

If you have image data as well, it is yet more tedious.

Mike.Cornflake

  • Hero Member
  • *****
  • Posts: 1260
Re: Some stringgrids questions
« Reply #3 on: March 27, 2015, 05:11:23 am »
And to add to your options:

I have a TFrame, that simply contains a TDBGrid (sure, my grid is different to yours, but the principle is the same).  I call this TFrameGrid.

I implement all my grid handling routines in the unit for the TFrameGrid. 

Although you can use TFrames at designtime, out of experience I never do.  Gets too complicated when you make what you think is generic UI change, but instead is a local instance change.  I just put TPanel placeholders in my forms, with the captions set to the name of the TFrameGrid, then I create the FrameGrids at runtime.

This approach is functionally very similar to the method proposed by @Blaazen.  From an OO perspective, I'm just basing my hierarchy on TFrame, instead of on TxxGrid. 

Main advantage of this is TFrame allows for rapid prototyping of your UI.   
Lazarus Trunk/FPC Trunk on Windows [7, 10]
  Have you tried searching this forum or the wiki?:   http://wiki.lazarus.freepascal.org/Alternative_Main_Page
  BOOKS! (Free and otherwise): http://wiki.lazarus.freepascal.org/Pascal_and_Lazarus_Books_and_Magazines

Gizmo

  • Hero Member
  • *****
  • Posts: 831
Re: Some stringgrids questions
« Reply #4 on: March 30, 2015, 12:16:43 am »
As Blaazan as hinted, read this for a fuller understanding of using multiple forms.

http://lazplanet.blogspot.co.uk/2015/01/everything-about-using-2-forms-or-more.html

Once implemented, you can do :
Code: [Select]
Unit1
  uses
    ... ... Unit2; // contains my stringgrid handling routines

begin

  StringGrid1
  StringGrid2

end;

Unit2
  uses
    // whatever units you need but don't add Unit1 here
...
  implementation
    uses
      Unit1; // contains my 5 stringgrids
 
begin
  // Do stuff with my stringgrids in Unit1
  With StringGrid1 do ....
  // or Unit1.StringGrid1
 
  With StringGrid2 do ....
end;

 

TinyPortal © 2005-2018