473,395 Members | 1,968 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

HELP: Any Printing Guru's? (PrintPreview OK...PrintDoc... NOT!)

This is driving me NUTZ!!! I've been screwing around on this for a week now.
And I have tried to find examples similar to what I have (nada). Got lots of
streaming a TXT file... bah!

I am really stuck here (probably the ol' Not seeing the tree cause of the
forest thingy). But I just can not get this to work.

The following is my Test example code. Just needs a Button1 on a blank Form.
Add in PrintDocument1, PrintPreviewDialog1, PageSetupDialog1... and run it.

It does everything that I want it to... EXCEPT print! I get the multiple page
preview. My formating is okay. But all it prints is a Single Blank page with
my "Weeks Total Hours: " on the page... Why??? Why can I preview OK... but
not print?
MY Test CODE (watch for word wrap):

Imports System.IO
Imports System.Drawing.Printing
' ************************************************** *******************
' Print Out Variables
Dim itm, rowTtl As Integer
Dim rwCntr As Integer ' Print Rows Counter
Dim pageNo As Integer ' Page Number
Dim PtrCtr As Integer
Dim tableFont, tableFont1 As Font
Dim X1, X2, X3 As Integer
Dim W1, W2, W3 As Integer
Dim Y As Integer
Dim lbWeekItems As Integer = 30
' ************************************************** *******************

' ************************************************** *******************
Private Sub Button1_Click(ByVal sender As System.Object, ByVal eg As
System.EventArgs) Handles Button1.Click

Dim pageWidth As Integer
PageSetupDialog1.PageSettings = PrintDocument1.DefaultPageSettings
PageSetupDialog1.AllowOrientation = False
PrintDocument1.DefaultPageSettings.Margins.Top = 75
PrintDocument1.DefaultPageSettings.Margins.Bottom = 50
PrintDocument1.DefaultPageSettings.Margins.Left = 50
PrintDocument1.DefaultPageSettings.Margins.Right = 50
PrintDocument1.DefaultPageSettings.Landscape = True

tableFont = New Font("Arial", 10)
tableFont1 = New Font("Arial", 9)

With PrintDocument1.DefaultPageSettings
pageWidth = .PaperSize.Height - _
.Margins.Left - _
.Margins.Right
' Measured in 1/100ths of an Inch
End With

PtrCtr = 0
rwCntr = 0
pageNo = 0

' X's are coordinates of the left edge of the columns
' W's are the widths of the columns
W1 = 75
W2 = 250
W3 = 40
X1 = 50
X2 = 410
X3 = 720

Try
PrintPreviewDialog1.Document = PrintDocument1
PrintPreviewDialog1.ShowDialog()
Catch exc As Exception
MsgBox("Print Operation Failed... " & vbCrLf & exc.Message)
End Try

End Sub
' ************************************************** *******************
Protected Sub PrintDocHeader(ByVal e As
System.Drawing.Printing.PrintPageEventArgs)
Dim str As String

Y = PrintDocument1.DefaultPageSettings.Margins.Top
str = "Time Sheet Header"
e.Graphics.DrawString(str, tableFont, Brushes.Black, X1, Y)
Y = Y + 25
e.Graphics.DrawString("Job Information", tableFont, Brushes.Black, X1, Y)
e.Graphics.DrawString("Cost Code Information", tableFont, Brushes.Black,
X2, Y)
e.Graphics.DrawString("Sat", tableFont, Brushes.Black, X3, Y)
e.Graphics.DrawString("Sun", tableFont, Brushes.Black, X3 + 40, Y)
e.Graphics.DrawString("Mon", tableFont, Brushes.Black, X3 + 80, Y)
e.Graphics.DrawString("Tue", tableFont, Brushes.Black, X3 + 120, Y)
e.Graphics.DrawString("Wed", tableFont, Brushes.Black, X3 + 160, Y)
e.Graphics.DrawString("Thr", tableFont, Brushes.Black, X3 + 205, Y)
e.Graphics.DrawString("Fri", tableFont, Brushes.Black, X3 + 245, Y)
e.Graphics.DrawString("Total", tableFont, Brushes.Black, X3 + 283, Y)
Y = Y + 20
' Draw Break Lines
With PrintDocument1.DefaultPageSettings
e.Graphics.DrawLine(Pens.Black, X1, Y, .PaperSize.Height - 50, Y)
e.Graphics.DrawLine(Pens.Black, X1, Y + 2, .PaperSize.Height - 50, Y +
2)
End With
Y = Y + 4

End Sub
' ************************************************** *******************
Private Sub PrintDocument1_PrintPage(ByVal sender As System.Object, ByVal e
As System.Drawing.Printing.PrintPageEventArgs) Handles
PrintDocument1.PrintPage

While PtrCtr < lbWeekItems
Dim str As String
rwCntr = rwCntr + 1

' Print Page Header
If rwCntr = 1 Then
PrintDocHeader(e)
' Print Page Number
pageNo = pageNo + 1
e.Graphics.DrawString("Page " & pageNo, tableFont, Brushes.Black,
1000, 800)
End If

Dim R1 As New RectangleF(X1, Y, W1, 80) ' Col 1 Job
Dim R2 As New RectangleF(X1 + 80, Y, W2, 80) ' Col 2 Job Desc
Dim R3 As New RectangleF(X2, Y, W1, 80) ' Col 3 cCode
Dim R4 As New RectangleF(X2 + 60, Y, W2, 80) ' Col 4 cCode Desc
Dim R5 As New RectangleF(X3, Y, W3, 80) ' Col 5 Sat
Dim R6 As New RectangleF(X3 + 40, Y, W3, 80) ' Col 6
Dim R7 As New RectangleF(X3 + 80, Y, W3, 80) ' Col 7
Dim R8 As New RectangleF(X3 + 120, Y, W3, 80) ' Col 8 Tue
Dim R9 As New RectangleF(X3 + 160, Y, W3, 80) ' Col 9
Dim R10 As New RectangleF(X3 + 205, Y, W3, 80) ' Col 10
Dim R11 As New RectangleF(X3 + 245, Y, W3, 80) ' Col 11 Fri
Dim R12 As New RectangleF(X3 + 285, Y, W3, 80) ' Col 12 Total
Dim R13 As New RectangleF(X1, Y + 17, 1050, 80) ' Col 13 cCode Desc

str = "Item"
e.Graphics.DrawString(str, tableFont, Brushes.Black, R1)
e.Graphics.DrawString(str, tableFont, Brushes.Black, R2)
e.Graphics.DrawString(str, tableFont, Brushes.Black, R3)
e.Graphics.DrawString(str, tableFont, Brushes.Black, R4)

' Week Day Hours
str = lbWeekItems
e.Graphics.DrawString(str, tableFont, Brushes.Black, R5)
e.Graphics.DrawString(str, tableFont, Brushes.Black, R6)
e.Graphics.DrawString(str, tableFont, Brushes.Black, R7)
e.Graphics.DrawString(str, tableFont, Brushes.Black, R8)
e.Graphics.DrawString(str, tableFont, Brushes.Black, R9)
e.Graphics.DrawString(str, tableFont, Brushes.Black, R10)
e.Graphics.DrawString(str, tableFont, Brushes.Black, R11)
e.Graphics.DrawString("22", tableFont, Brushes.Black, R12)
Y = Y + 15

str = "Line Item: " & lbWeekItems
If Len(str) <= 165 Then
tableFont1 = New Font("Arial", 9)
Else
tableFont1 = New Font("Arial", 8)
End If
e.Graphics.DrawString(str, tableFont1, Brushes.Black, R13)
' Reset Font
tableFont1 = New Font("Arial", 9)

' Draw Break Line
With PrintDocument1.DefaultPageSettings
Y = Y + 20
e.Graphics.DrawLine(Pens.Black, X1, Y, .PaperSize.Height - 50, Y)
End With

' That's it for this page...
If rwCntr = 18 Then
e.HasMorePages = True
PtrCtr = PtrCtr + 1
rwCntr = 0
Exit Sub
End If
PtrCtr = PtrCtr + 1
End While

' Total Weeks Hours (last page)
With PrintDocument1.DefaultPageSettings
e.Graphics.DrawString("Weeks Total Hours: ", tableFont, Brushes.Black,
875, 775)
e.Graphics.DrawString("44", tableFont, Brushes.Black, 1000, 775)
End With

' No More Pages to Print
e.HasMorePages = False

End Sub
' ************************************************** *******************

Any kind soul can spot the trouble?

Regards,

Bruce
Jul 21 '05 #1
5 2112
I'm able to preview your code OK also. I'm not at a printer at the moment,
so I can't test printed output, but I believe I know what your problem is.

When you invoke "print", the document goes through the onPrintPage series
of events all over again. Since you initialized values in the click event
for your loops, these values are already past their points when you click on
print.

You can verify this without printing by adding a second button with a
preview. In that button, do not initialize your loopers. Click on the
normal button first and you'll see your nice print preview (nice job on
working with PrintDocuments by the way). Click on the second button next,
and you'll see the problem you are reporting.

The solution is to move your loop initializers into the printhandling code.

One more thing, you might want to turn option strict on at the top of your
code. You have an implicit cast from integer to string in your printing
handler.

Justin Weinberg

Designing a PrintDocument or creating .NET graphics?
Save time with GDI+ Architect.
For more information, visit http://www.mrgsoft.com

<Long code section snipped>
Jul 21 '05 #2
Slight correction. Use the begin print event for your initializers since
your using them to determine continuation in your PrintPage event.

Private Sub PrintDocument1_BeginPrint(ByVal sender As Object, ByVal e As
System.Drawing.Printing.PrintEventArgs) Handles PrintDocument1.BeginPrint
PtrCtr = 0
rwCntr = 0
pageNo = 0
End Sub
--
Justin Weinberg

Designing a PrintDocument or creating .NET graphics?
Save time with GDI+ Architect.
For more information, visit http://www.mrgsoft.com

"Justin Weinberg" <jweinberg@_spamoff_mrgsoft.com> wrote in message
news:%2****************@TK2MSFTNGP12.phx.gbl...
I'm able to preview your code OK also. I'm not at a printer at the moment, so I can't test printed output, but I believe I know what your problem is.

When you invoke "print", the document goes through the onPrintPage series of events all over again. Since you initialized values in the click event
for your loops, these values are already past their points when you click on print.

You can verify this without printing by adding a second button with a
preview. In that button, do not initialize your loopers. Click on the
normal button first and you'll see your nice print preview (nice job on
working with PrintDocuments by the way). Click on the second button next,
and you'll see the problem you are reporting.

The solution is to move your loop initializers into the printhandling code.
One more thing, you might want to turn option strict on at the top of your
code. You have an implicit cast from integer to string in your printing
handler.

Justin Weinberg

Designing a PrintDocument or creating .NET graphics?
Save time with GDI+ Architect.
For more information, visit http://www.mrgsoft.com

<Long code section snipped>

Jul 21 '05 #3
With Deft Fingers, "Justin Weinberg" <jweinberg@_spamoff_mrgsoft.com> wrote:
Slight correction. Use the begin print event for your initializers since
your using them to determine continuation in your PrintPage event.

Private Sub PrintDocument1_BeginPrint(ByVal sender As Object, ByVal e As
System.Drawing.Printing.PrintEventArgs) Handles PrintDocument1.BeginPrint
PtrCtr = 0
rwCntr = 0
pageNo = 0
End Sub

OMG!!!! YES!!! That's got it! THANK YOU THANK YOU THANK YOU THANK YOU...

Damn... I knew I was 'close'... but (belive it or not)... this is my first
attempt at printing. And of course, what I do it pick the worst possible
thing to do... then once I got it, everything is downhill!!

Many Thanks!!!

Oh and the Option Strict On... thanks. Just was testing out my stuff...

Regards,

Bruce
Jul 21 '05 #4
Glad I could help!

--
Justin Weinberg

Designing a PrintDocument or creating .NET graphics?
Save time with GDI+ Architect.
For more information, visit http://www.mrgsoft.com

"Mr. B" <Us**@NoWhere.com> wrote in message
news:sm********************************@4ax.com...
With Deft Fingers, "Justin Weinberg" <jweinberg@_spamoff_mrgsoft.com> wrote:
Slight correction. Use the begin print event for your initializers since
your using them to determine continuation in your PrintPage event.

Private Sub PrintDocument1_BeginPrint(ByVal sender As Object, ByVal e As
System.Drawing.Printing.PrintEventArgs) Handles PrintDocument1.BeginPrint
PtrCtr = 0
rwCntr = 0
pageNo = 0
End Sub

OMG!!!! YES!!! That's got it! THANK YOU THANK YOU THANK YOU THANK YOU...

Damn... I knew I was 'close'... but (belive it or not)... this is my first
attempt at printing. And of course, what I do it pick the worst possible
thing to do... then once I got it, everything is downhill!!

Many Thanks!!!

Oh and the Option Strict On... thanks. Just was testing out my stuff...

Regards,

Bruce

Jul 21 '05 #5
With Deft Fingers, "Justin Weinberg" <jweinberg@_spamoff_mrgsoft.com> wrote:
Glad I could help!


Me too (:

While I won't pretend to understand 'why' your suggestion worked... I'm just
glad it did (grin). I'll have to digest the whole thing some more to get an
understanding on it.

Thanks again...

Regards,

Bruce
Jul 21 '05 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

10
by: Mario | last post by:
Hello all, I'm trying hard to make possible to print some simple text from python to the default printer using wxPython, after days of internet searches I found this page:...
0
by: Nigel | last post by:
I successfully create a .NET Component (Visual Basic .NET) that would print, unfortunately when used within a web browser it appears that .NET security doesn't allow you to run code that interacts...
0
by: Mr. B | last post by:
This is driving me NUTZ!!! I've been screwing around on this for a week now. And I have tried to find examples similar to what I have (nada). Got lots of streaming a TXT file... bah! I am...
2
by: Kevin | last post by:
In my VB2005 Windows Forms program I want to be able to click a button and have my Dot Matrix printer print one 15/16 x 3 1/2 address label from continuous forms. I'm trying to use a...
3
by: jonathan184 | last post by:
script is printing output correct but not the actual output. Basically what the script is doing it taking a 1 flat file then it is splits the file into smaller files in 1000 record increments ...
1
by: byquestion | last post by:
Hi there xslt gurus, i am kinda new to xslt and having difficulty to implement my some iterations. i need to recreate an xml file by using xslt. here is the sample xml file(input for xslt) ...
0
by: vench | last post by:
Hi! I have a vb.net project that uses mozilla activeX control, there is no problem in navigating or loading the url in the control, the problem that I have encountered is when printpreview using the...
0
by: rafiki31 | last post by:
I have been hitting walls trying to find the right way to print from a web application. Here is the thing, im implementing a webapplication for a kiosk. The kiosk has its own card printer. When i...
0
by: bschomp | last post by:
Hi, when I try to print from my richtextbox it's not printing what I have typed in. I want to be able to print the image that it shows. So if I type in Hello World it will print out Hello World. This...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.