Page 1 of 1

passing text to preview on a seperate form

PostPosted: May 7th, 2003, 4:40 am
by azarroth
How would I pass text in a richedit component to the printpreview component on a seperate form?

PostPosted: May 8th, 2003, 7:16 pm
by Kambiz
Actually there is no difference between using PrintPreview component in the same form or another form.

For example suppose you have two forms: TForm1 in unit1 and TForm2 (with PrintPreview) in unit2. You should add unit2 in uses clause of unit1 and after use a code like the following code to print the content of your RichEdit control:

Code: Select all
procedure TForm1.Button1Click(Sender: TObject);
var
  R: TRect;
  OneInch: Integer;
begin
  Form2.Show;
  with Form2.PrintPreview do
  begin
    OneInch := ConvertUnit(10, mmLoEnglish, Units);
    SetRect(R, 0, 0, PaperWidth, PaperHeight);
    InflateRect(R, -OneInch, -OneInch);
    PaintRichText(R, RichEdit1, 0, nil);
  end;
end;


However, I suggest to add a public method to TForm2 and write your print code inside. For example the following code can be converted as follow:

Code: Select all
procedure TForm2.PrintRichEdit(RichEdit: TCustomRichEdit);
var
  R: TRect;
  OneInch: Integer;
begin
  with PrintPreview do
  begin
    OneInch := ConvertUnit(10, mmLoEnglish, Units);
    SetRect(R, 0, 0, PaperWidth, PaperHeight);
    InflateRect(R, -OneInch, -OneInch);
    PaintRichText(R, RichEdit, 0, nil);
  end;
end;


You can call the above function in the first unit as follow:

Code: Select all
procedure TForm1.Button1Click(Sender: TObject);
begin
  Form2.Show;
  Form2.PrintRichEdit(RichEdit1);
end;


Hope that it helps.

Kambiz