Associating PrintPreviewDialog with TextBox

  • Thread starter Thread starter gsb58
  • Start date Start date
G

gsb58

Hi!

I have a Textbox control on a form and a PrintPreviewDialog.

How on earth can I associate the Textbox control with the
PrintPreviewDialog, so that the text from the Textbox control will show
in the PrintPreviewControl when launching?

Anybody got a clue?

Me.Name
 
Are you asking how to "print" to the print preview?

The simplest solution is to implement fixed position printing and
implement an IDraw interface. Extend the textbox control and add an
IDraw Draw method. At print preview create an array of IDraw objects and
iterate over the array calling Draw on each IDraw object. Each IDraw
object knows its position and prints itself.

http://www.geocities.com/jeff_louie/OOP/oop21.htm

So for a textbox you have a method as:

public bool Draw(IPrintEngine pe,
float xPos,
float yPos,
Graphics g,
RectangleF pageRect,
bool isSimulation)
{
Brush brush;
Font font;
StringFormat stringFormat;
if (this.isUseDocumentProperties)
{
brush= pe.Brush;
font= pe.Font;
stringFormat= pe.StringFormat;
stringFormat.SetTabStops(0.0f,pe.TabStops);
}
else
{
brush= new SolidBrush(this.ForeColor);
font= this.Font;
stringFormat= this.stringFormat;
stringFormat.SetTabStops(0.0f,pe.TabStops);
}
string text= this.Text;

if (this.isReplaceTokens)
{
text= pe.ReplaceTokens(text);
}
text= text.Trim();
if (!this.Multiline)
{
if (!isSimulation)
{
g.DrawString(text,
font,
brush,
new RectangleF(xPos+this.horizontalMargin,
yPos+this.verticalMargin,
(this.CalculateWidth(pe,g)-(this.horizontalMargin*2)),
this.Size.Height),
this.stringFormat);
}
}
else
{
if (!isSimulation)
{
g.DrawString(text,
font,
brush,
new RectangleF(xPos+this.horizontalMargin,
yPos+this.verticalMargin,
this.Size.Width,
this.Size.Height),
this.stringFormat);
}
}
return false;
}

If you are asking about the actual print dialog control, I plead
ignorance :).

Regards,
Jeff
 
Hi!

I really ment associating text from the Textbox with the PrintPreview
Control. However, I found a very clear and good answer in the given
url.

But, following the example on this page (Chapter 21) for the
PrintPreview control I still get the following message:

"object reference not set to an instance of the object".
 
Looks like you are just missing a line of code somewhere. What does the
code
look like that is causing the error.

Regards,
Jeff
following the example on this page (Chapter 21) for the
PrintPreview control I still get the following message:

"object reference not set to an instance of the object".<
 
Sorry it took so long...

ppdlg.Document = pDoc;
ppdlg.ShowDialog();

where ppdlg is the PrintPreview control, pDoc is the PrintDocument
control.

So what do you think I'm missing?

Me.Name
 
Back
Top