Re:Re:Re:Re: Delphi: Printing calculated results


[ Delphi Forum ] [ Delphi Tutorials -- by DelphiLand ]

Posted by webmaster Guido on June 25, 2003:

In Reply to: Re:Re:Re: Printing calculated results posted by Lionel on June 25, 2003:

: I would like to use a Print Button to make a hardcopy of the Sine, Cosine, Tangent, etc. The following code will display the results on the form but I would like to print out a hardcopy.
:
: procedure TForm1.calcClick(Sender: TObject);
: var
: Degree,
: Minute,
: Second : integer;
:
: [ and do on...]
----------------------------------------

Here is a description of the simplest way to print your data. Along the way, I'll also give a few tips.

1. Start by adding "Printers" to the USES statement (just below the word "interface" in the unit's code).

For example, if it reads:
uses Windows, ..., Math;

then you change it to:
uses Windows, ..., Math, PRINTERS;

2. Next, let's add a button and name it "btnPrint". In the Object Inspector, set it's property "Enabled" to FALSE (we'll only enable it when there are valid data to be printed).

3. In the beginning of the calculations procedure, disable btnPrint, and enable it at the end of the routine. That way, the user can only print if there are valid data, i.e. if nothing went wrong in the CalcClick procedure. Your changed code would look like this:

procedure TForm1.calcClick(Sender: TObject);
var
  ...variable declarations...
begin
  btnPrint.Enabled := False; // add this line 
  try
    Degree := StrToInt(deg.Text);
  ...
  Edit9.Text := FloatToStr(LogSine);
  btnPrint.Enabled := True; // add this line
end;

4. Here's a template for a simple print routine:

procedure PrintSomething;
var F: TextFile;
begin
  Printer.Title := title_of_printjob;
  Printer.Canvas.Font.Name := name_of_font;
  Printer.Canvas.Font.Size := size_of_font;
  AssignPrn(F);
  Rewrite(F);
  try
    WriteLn(F, line_to_be_printed);
    WriteLn(F, next_line_to_be_printed);
    // more lines here...
  finally
    CloseFile(F);
  end;
end;

Now for real... As you have already the formatted values sitting in the TEdit boxes, you can print them out like this:

procedure TForm1.btnPrintClick(Sender: TObject);
var
  F: TextFile;
begin
  Printer.Title := 'Trigo';
  Printer.Canvas.Font.Name := 'Arial';
  Printer.Canvas.Font.Size := 10;
  AssignPrn(F);
  Rewrite(F);
  try
    WriteLn(F, 'Trigonometric Calculations');
    WriteLn(F, '') ; // blank line
    WriteLn(F, 'Angle: ', Deg.Text, Min.Text, Sec.Text;
    WriteLn(F, 'Decimal angle: ', Edit4.Text);
    WriteLn(F, 'Rad:', Edit5.Text);
    WriteLn(F, 'Sine:', Edit1.Text);
    // and so on for more lines...
  finally
    CloseFile(F);
  end;
end;

5. Finally, add some code to beautify the printout, such as: add some blank lines at the top, add some spaces at the left to simulate margins, use a bigger font for the header, and so on.

==================
Disadvantage: in this basic print example, the columns don't line up nicely. For that you have to output text the printer's "Canvas", specifying horizontal and vertical coordinates. Let me know if you're interested, and I'll brew some code.

 


Related Articles and Replies:


 


[ Delphi Forum ] [ Delphi Tutorials -- by DelphiLand ]