First Report in VB2005 - Print Preview

H

Hector M Banda

Hi all,
Need i simple example on how print/preview data from a database.
I already have some code but I am lost because the preview does not create
multiple pages and only shows the first page and the printing goes over the
margins of the page. My problem is that I cannot figure out how to control
new page when page is getting full. Another problem is that I would like to
have a common place to set headers and footers to be printed every time a
new page is sent.

I would like to have a little code sample even with just printing lines.

This is part of the code that I am using with MySQL database just going thru
some records:

While datareader.Read()

e.Graphics.DrawString(datareader("upc").ToString, sfont, Brushes.Black,
lblpos(0) + 0.5, YPosition)

e.Graphics.DrawString(datareader("description").ToString, sfont,
Brushes.Black, lblpos(1) + 0.5, YPosition)

e.Graphics.DrawString(datareader("onhand").ToString, sfont, Brushes.Black,
lblpos(2) + 0.5, YPosition)

e.Graphics.DrawString(datareader("min").ToString, sfont, Brushes.Black,
lblpos(3) + 0.5, YPosition)

e.Graphics.DrawString(datareader("cost").ToString, sfont, Brushes.Black,
lblpos(4) + 0.5, YPosition)

e.Graphics.DrawString(datareader("price").ToString, sfont, Brushes.Black,
lblpos(5) + 0.5, YPosition)

CountLine += 1

YPosition = TopMargin + (CountLine * myFont.GetHeight(e.Graphics))

Console.WriteLine(">>>" & YPosition)

If CountLine = 20 Then

e.HasMorePages = False

CountLine = 1

Else

e.HasMorePages = True

End If

End While

datareader.Close()

Some of the code has been omitted.



Thanks,

-hb
 
C

Chris Dunaway

Hector said:
I would like to have a little code sample even with just printing lines.

This is part of the code that I am using with MySQL database just going thru
some records:

While datareader.Read()

e.Graphics.DrawString(datareader("upc").ToString, sfont, Brushes.Black,
lblpos(0) + 0.5, YPosition)

CountLine += 1

YPosition = TopMargin + (CountLine * myFont.GetHeight(e.Graphics))

Console.WriteLine(">>>" & YPosition)

If CountLine = 20 Then

e.HasMorePages = False

CountLine = 1

Else

e.HasMorePages = True

End If

End While

datareader.Close()

Some of the code has been omitted.

When you set HasMorePages = True, all that does is tell the printing
engine to call the print page event again. Normallty, when you set
HasMorePages to true, you should exit the method. You need to keep
track of where you are in the datareader so that when the PrintPage
event is fired again, you pick up printing from that point. The
DataReader should be created *outside* of the PrintPage method.

When you reach the end of the page you are currently drawing on, if you
still have more to print, you should set HasMorePages to true and then
exit the PrintPage event. It will get fired again and you can pick up
where you left off.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top