Recent

Author Topic: [SOLVED] How Do I Print a StringGrid with all its Contents?  (Read 4517 times)

wp

  • Hero Member
  • *****
  • Posts: 11854
Re: [SOLVED] How Do I Print a StringGrid with all its Contents?
« Reply #30 on: October 21, 2022, 08:09:38 pm »
Apart from small rounding errors the print-out and preview should match the grid on the form. Therefore you should see the too-narrow row heights already in the grid on the form? If not, you should post a sample project so that I can find out why form and print/preview do not match.

bonmario

  • Sr. Member
  • ****
  • Posts: 346
Re: [SOLVED] How Do I Print a StringGrid with all its Contents?
« Reply #31 on: November 14, 2022, 03:37:11 pm »
Sorry, I can't find them anymore, or at this link https://github.com/wp-xyz/LazSamples/tree/master/grids/gridprinter there are no more sources?

Thanks, Mario

paweld

  • Hero Member
  • *****
  • Posts: 970
Best regards / Pozdrawiam
paweld

wp

  • Hero Member
  • *****
  • Posts: 11854
Re: [SOLVED] How Do I Print a StringGrid with all its Contents?
« Reply #33 on: November 14, 2022, 04:57:14 pm »
Sorry, I moved it to CCR as paweld noted already - that's where it should have been from the start... There is even a release version available for OPM (but this was a bit too early because I had some more ideas which might break the interface). I'll prepare a new release when I'm finished with what's still on my mind. There is also a (yet incomplete) wiki documentation at https://wiki.freepascal.org/GridPrinter.

bonmario

  • Sr. Member
  • ****
  • Posts: 346
Re: [SOLVED] How Do I Print a StringGrid with all its Contents?
« Reply #34 on: November 14, 2022, 05:40:03 pm »
Ok, thanks !!!

PasCoder

  • New Member
  • *
  • Posts: 34
Re: [SOLVED] How Do I Print a StringGrid with all its Contents?
« Reply #35 on: November 14, 2022, 09:06:14 pm »
Dear WP,
Thank you very much for the work. I've been following this thread and I'm glad that you've made Printing Components for the TStringGrid. This answers well my original question. However, I need some kind of help.

1. I've dragged the two components and linked them together but the cells with strings in multiple lines are all in one line in the preview!
2. The colors that I painted for each row are also not recognized.

I've attached the images also, below is the code for the PrepareCanvas

Code: Pascal  [Select][+][-]
  1. procedure TFormAKJV.ChapterStringGridPrepareCanvas(Sender: TObject; aCol, aRow: integer; aState: TGridDrawState);
  2.   var
  3.     lCanvas: TCanvas;
  4.     s: String;
  5.     ts: TTextStyle;
  6.   begin
  7.     ts := ChapterStringGrid.Canvas.TextStyle;
  8.     ts.SingleLine := False;
  9.     ts.Wordbreak := True;
  10.     ChapterStringGrid.Canvas.TextStyle := ts;
  11.  
  12.   if Sender is TStringGrid then
  13.     lCanvas := TStringGrid(Sender).Canvas
  14.   else if Sender = ChapterGridPrinter then
  15.     lCanvas := ChapterGridPrinter.Canvas
  16.   else
  17.     exit;
  18.  
  19.   if Sender is TStringGrid then
  20.     s := TStringGrid(Sender).Cells[4, ARow]
  21.   else if Sender = ChapterGridPrinter then
  22.     s := ChapterGridPrinter.GetCellText(4, ARow);
  23.  
  24.   if s = 'AntiqueWhite' then begin
  25.       lCanvas.Brush.Color := TColor($FAEBD7);
  26.       end else if s = 'Aqua' then begin
  27.       lCanvas.Brush.Color := TColor($00FFFF);
  28.       end else if s = 'Aquamarine' then begin
  29.       lCanvas.Brush.Color := TColor($7FFFD4);
  30.       end else if s = 'Azure' then begin
  31.       lCanvas.Brush.Color := TColor($F0FFFF);
  32.       end else if s = 'Beige' then begin
  33.       lCanvas.Brush.Color := TColor($F5F5DC);
  34.       end

Thanks


wp

  • Hero Member
  • *****
  • Posts: 11854
Re: [SOLVED] How Do I Print a StringGrid with all its Contents?
« Reply #36 on: November 14, 2022, 10:00:03 pm »
You apply the TextStyle to the Canvas of the ChapterStringGrid only. Move the evaluation of the Sender to the top of the event handler and apply the TextStyle to lCanvas.

Additionally a simplification for cell text extraction: Since grid and printer access the same cells it is enough to read the the cell text from the grid directly; there is no need to distinguish the Sender here - sorry, one of my previous sample codes was a bit too complicated here.

Code: Pascal  [Select][+][-]
  1. procedure TFormAKJV.ChapterStringGridPrepareCanvas(Sender: TObject; aCol, aRow: integer; aState: TGridDrawState);
  2. var
  3.   lCanvas: TCanvas;
  4.   s: String;
  5.   ts: TTextStyle;
  6. begin
  7.   if Sender is TStringGrid then
  8.     lCanvas := TStringGrid(Sender).Canvas
  9.   else if Sender = ChapterGridPrinter then
  10.     lCanvas := ChapterGridPrinter.Canvas
  11.   else
  12.     exit;
  13.  
  14.   ts := lCanvas.TextStyle;
  15.   ts.SingleLine := False;
  16.   ts.Wordbreak := True;
  17.   lCanvas.TextStyle := ts;
  18.  
  19.   s := ChapterStringGrid.Cells[4, ARow];
  20.  
  21.   if s = 'AntiqueWhite' then begin
  22.       lCanvas.Brush.Color := TColor($FAEBD7);
  23.       end else if s = 'Aqua' then begin
  24.       lCanvas.Brush.Color := TColor($00FFFF);
  25.       end else if s = 'Aquamarine' then begin
  26.       lCanvas.Brush.Color := TColor($7FFFD4);
  27.       end else if s = 'Azure' then begin
  28.       lCanvas.Brush.Color := TColor($F0FFFF);
  29.       end else if s = 'Beige' then begin
  30.       lCanvas.Brush.Color := TColor($F5F5DC);
  31.       end;
  32. end;
  33.  

Sheepdog

  • New Member
  • *
  • Posts: 39
Re: [SOLVED] How Do I Print a StringGrid with all its Contents?
« Reply #37 on: November 15, 2022, 09:27:09 am »
For what it is worth... mostly for someone drawn to this thread with an interest in "getting started" with string grids, I suspect, I have a webpage with some notes on that... https://sheepdogguides.com/lut/lt4Nc.htm

PasCoder

  • New Member
  • *
  • Posts: 34
Re: [SOLVED] How Do I Print a StringGrid with all its Contents?
« Reply #38 on: December 06, 2022, 08:48:44 am »
You apply the TextStyle to the Canvas of the ChapterStringGrid only. Move the evaluation of the Sender to the top of the event handler and apply the TextStyle to lCanvas.

Additionally a simplification for cell text extraction: Since grid and printer access the same cells it is enough to read the the cell text from the grid directly; there is no need to distinguish the Sender here - sorry, one of my previous sample codes was a bit too complicated here.

Code: Pascal  [Select][+][-]
  1. procedure TFormAKJV.ChapterStringGridPrepareCanvas(Sender: TObject; aCol, aRow: integer; aState: TGridDrawState);
  2. var
  3.   lCanvas: TCanvas;
  4.   s: String;
  5.   ts: TTextStyle;
  6. begin
  7.   if Sender is TStringGrid then
  8.     lCanvas := TStringGrid(Sender).Canvas
  9.   else if Sender = ChapterGridPrinter then
  10.     lCanvas := ChapterGridPrinter.Canvas
  11.   else
  12.     exit;
  13.  
  14.   ts := lCanvas.TextStyle;
  15.   ts.SingleLine := False;
  16.   ts.Wordbreak := True;
  17.   lCanvas.TextStyle := ts;
  18.  
  19.   s := ChapterStringGrid.Cells[4, ARow];
  20.  
  21.   if s = 'AntiqueWhite' then begin
  22.       lCanvas.Brush.Color := TColor($FAEBD7);
  23.       end else if s = 'Aqua' then begin
  24.       lCanvas.Brush.Color := TColor($00FFFF);
  25.       end else if s = 'Aquamarine' then begin
  26.       lCanvas.Brush.Color := TColor($7FFFD4);
  27.       end else if s = 'Azure' then begin
  28.       lCanvas.Brush.Color := TColor($F0FFFF);
  29.       end else if s = 'Beige' then begin
  30.       lCanvas.Brush.Color := TColor($F5F5DC);
  31.       end;
  32. end;
  33.  

Dear wp,
I followed your advice but it seems there is still an issue to sort out.
1. The component does not print/preview wordwrapped cells. Actually, its like it dose not recognize that the cells' contents have been wordwrapped. Check on the first four cells in the Billing Address Column.
2. The set row colours are also not priviewed and printed!

I've attached a sample project for you to review. Please, help me sort these issues out for me.

Thank you

wp

  • Hero Member
  • *****
  • Posts: 11854
Re: [SOLVED] How Do I Print a StringGrid with all its Contents?
« Reply #39 on: December 06, 2022, 10:55:39 pm »
Wordwrap requires the following conditions:
- Canvas.TextStyle.WordWrap must be true, of course.
- Canvas.TextStyle.SingleLine must be false
- Canvas.TextStyle.EndEllipsis must be false. BUT: The grid Option goCellEllipsis when active sets this to true. This is happening in your grid. Therefore wordwrap is not working. --> Remove it from the Options, and the wordwrap will work.

The other issue is that the row colors in your grid do not appear in the preview. This is because you do not assign the StringGrid1PrepareCanvas event handler to GridPrinter1.OnPrepareCanvas: Select the GridPrinter1 component in the form, find the OnPrepareCanvas event and select StringGrid1PrepareCanvas from the dropdown list.

PasCoder

  • New Member
  • *
  • Posts: 34
Re: [SOLVED] How Do I Print a StringGrid with all its Contents?
« Reply #40 on: December 09, 2022, 04:10:35 pm »
Wordwrap requires the following conditions:
- Canvas.TextStyle.WordWrap must be true, of course.
- Canvas.TextStyle.SingleLine must be false
- Canvas.TextStyle.EndEllipsis must be false. BUT: The grid Option goCellEllipsis when active sets this to true. This is happening in your grid. Therefore wordwrap is not working. --> Remove it from the Options, and the wordwrap will work.

The other issue is that the row colors in your grid do not appear in the preview. This is because you do not assign the StringGrid1PrepareCanvas event handler to GridPrinter1.OnPrepareCanvas: Select the GridPrinter1 component in the form, find the OnPrepareCanvas event and select StringGrid1PrepareCanvas from the dropdown list.

Dear wp,
Thank you very much. It has now worked as expected!

 

TinyPortal © 2005-2018