473,659 Members | 3,631 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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, PrintPreviewDia log1, PageSetupDialog 1... 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(B yVal sender As System.Object, ByVal eg As
System.EventArg s) Handles Button1.Click

Dim pageWidth As Integer
PageSetupDialog 1.PageSettings = PrintDocument1. DefaultPageSett ings
PageSetupDialog 1.AllowOrientat ion = False
PrintDocument1. DefaultPageSett ings.Margins.To p = 75
PrintDocument1. DefaultPageSett ings.Margins.Bo ttom = 50
PrintDocument1. DefaultPageSett ings.Margins.Le ft = 50
PrintDocument1. DefaultPageSett ings.Margins.Ri ght = 50
PrintDocument1. DefaultPageSett ings.Landscape = True

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

With PrintDocument1. DefaultPageSett ings
pageWidth = .PaperSize.Heig ht - _
.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
PrintPreviewDia log1.Document = PrintDocument1
PrintPreviewDia log1.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.PrintP ageEventArgs)
Dim str As String

Y = PrintDocument1. DefaultPageSett ings.Margins.To p
str = "Time Sheet Header"
e.Graphics.Draw String(str, tableFont, Brushes.Black, X1, Y)
Y = Y + 25
e.Graphics.Draw String("Job Information", tableFont, Brushes.Black, X1, Y)
e.Graphics.Draw String("Cost Code Information", tableFont, Brushes.Black,
X2, Y)
e.Graphics.Draw String("Sat", tableFont, Brushes.Black, X3, Y)
e.Graphics.Draw String("Sun", tableFont, Brushes.Black, X3 + 40, Y)
e.Graphics.Draw String("Mon", tableFont, Brushes.Black, X3 + 80, Y)
e.Graphics.Draw String("Tue", tableFont, Brushes.Black, X3 + 120, Y)
e.Graphics.Draw String("Wed", tableFont, Brushes.Black, X3 + 160, Y)
e.Graphics.Draw String("Thr", tableFont, Brushes.Black, X3 + 205, Y)
e.Graphics.Draw String("Fri", tableFont, Brushes.Black, X3 + 245, Y)
e.Graphics.Draw String("Total", tableFont, Brushes.Black, X3 + 283, Y)
Y = Y + 20
' Draw Break Lines
With PrintDocument1. DefaultPageSett ings
e.Graphics.Draw Line(Pens.Black , X1, Y, .PaperSize.Heig ht - 50, Y)
e.Graphics.Draw Line(Pens.Black , X1, Y + 2, .PaperSize.Heig ht - 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.PrintP ageEventArgs) 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.Draw String("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.Draw String(str, tableFont, Brushes.Black, R1)
e.Graphics.Draw String(str, tableFont, Brushes.Black, R2)
e.Graphics.Draw String(str, tableFont, Brushes.Black, R3)
e.Graphics.Draw String(str, tableFont, Brushes.Black, R4)

' Week Day Hours
str = lbWeekItems
e.Graphics.Draw String(str, tableFont, Brushes.Black, R5)
e.Graphics.Draw String(str, tableFont, Brushes.Black, R6)
e.Graphics.Draw String(str, tableFont, Brushes.Black, R7)
e.Graphics.Draw String(str, tableFont, Brushes.Black, R8)
e.Graphics.Draw String(str, tableFont, Brushes.Black, R9)
e.Graphics.Draw String(str, tableFont, Brushes.Black, R10)
e.Graphics.Draw String(str, tableFont, Brushes.Black, R11)
e.Graphics.Draw String("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.Draw String(str, tableFont1, Brushes.Black, R13)
' Reset Font
tableFont1 = New Font("Arial", 9)

' Draw Break Line
With PrintDocument1. DefaultPageSett ings
Y = Y + 20
e.Graphics.Draw Line(Pens.Black , X1, Y, .PaperSize.Heig ht - 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. DefaultPageSett ings
e.Graphics.Draw String("Weeks Total Hours: ", tableFont, Brushes.Black,
875, 775)
e.Graphics.Draw String("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 2152
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(ByVa l sender As Object, ByVal e As
System.Drawing. Printing.PrintE ventArgs) 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@_spa moff_mrgsoft.co m> wrote in message
news:%2******** ********@TK2MSF TNGP12.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@_spa moff_mrgsoft.co m> 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(ByVa l sender As Object, ByVal e As
System.Drawing .Printing.Print EventArgs) 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.c om> wrote in message
news:sm******** *************** *********@4ax.c om...
With Deft Fingers, "Justin Weinberg" <jweinberg@_spa moff_mrgsoft.co m> 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(ByVa l sender As Object, ByVal e As
System.Drawing .Printing.Print EventArgs) 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@_spa moff_mrgsoft.co m> 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
4962
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: http://wiki.wxpython.org/index.cgi/Printing but is impossible to use this script even if I do exactly as said there. I think the script is buggy or I am not able to use it, even if seems very simple to use... Anyone can give me an hint on how to easily and simply print...
0
1823
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 with the file system (including printing) from the web browser. How do I disabled this so I can get my windows form control to work within IE? Also, do any have any printing code or know how I can implement multipage printing using the...
0
1049
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 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,...
2
4226
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 PrintDocument, but don't know how to set up the page size to be only one label.
3
1642
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 then it creates another file and adds the header the file contents of the old file. and the file with the header is then renamed to file format i want which is txt.
1
1406
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) <Flights> <Flight> <FlightLeg > <BookingClassCodeList> <BookingCode Value="C" /> <BookingCode Value="Y" /> </BookingClassCodeList>
0
1106
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 code axWebbrowser1.exeWB(MOZILLACONTROLLib.OLECMDID.OLECMDID_PRINTPREVIEW, MOZILLACONTROLLib.OLECMDEXECOPT.OLECMDEXECOPT_DONTPROMPTUSER) I get an innerException error something on reflection... I don't know how to fix it... thanks....
0
1381
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 tested the application using my pc as a server everything worked perfectly. Of course this is because i hadnt realize that the server was the one sending the print promtp to the printer which was ALSO connected to my printer so when i published the...
0
1538
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 goes for color too. Any help would be most appreciated. My code is below. Thanks -B Imports System Imports System.Windows.Forms Imports System.Drawing Imports System.Runtime.InteropServices Imports System.Drawing.Printing
0
8428
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8337
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8851
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8628
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7359
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
5650
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4175
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4335
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2754
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system

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.