Wrox Programmer Forums
|
BOOK: Visual Basic 2005 Programmer's Reference
This is the forum to discuss the Wrox book Visual Basic 2005 Programmer's Reference by Rod Stephens; ISBN: 9780764571985
Welcome to the p2p.wrox.com Forums.

You are currently viewing the BOOK: Visual Basic 2005 Programmer's Reference section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old March 14th, 2006, 09:34 PM
Registered User
 
Join Date: Mar 2006
Posts: 3
Thanks: 0
Thanked 0 Times in 0 Posts
Default Printing in VB.NET 2005

I created a class based on your example in Chapter 23 pages 643-646. It all works great when I print to the PrintPreviewDialog (formatting is fine and page breaks are as expected), but as soon as I click the Print button in the dialog or try to print directly, it acts as though it has an infinitely long piece of paper so much of the text is printed "off" the end of the paper.

The code suggests that the MeasureString function will determine the number of characters that can be fitted in the available rectangle below ymin and, even when the layout_rect.Height is a negative number it continues to print the next line. My paragraph collection is mostly single line paragraphs, very few exceed one printed line.

I tried forcing the page break by exiting the loop when ymin exceeded e.MarginBounds.Bottom and that works, but I lose the last paragraph on that page. What is the fundamental difference between the paper page and the print preview that prevents the PrintPage routine from exiting when the layout_rect is too small to print a line and how can I make it break correctly?

Thanks much.

Al Mowbray

 
Old March 15th, 2006, 10:02 AM
Rod Stephens's Avatar
Wrox Author
 
Join Date: Jan 2006
Posts: 647
Thanks: 2
Thanked 96 Times in 95 Posts
Default

I'll take a look, although I don't think I'll have time until tomorrow. Rod

Rod
RodStephens@vb-helper.com
Author of "Visual Basic 2005 Programmer's Reference"
http://www.vb-helper.com/vb_prog_ref.htm
 
Old March 15th, 2006, 12:23 PM
Rod Stephens's Avatar
Wrox Author
 
Join Date: Jan 2006
Posts: 647
Thanks: 2
Thanked 96 Times in 95 Posts
Default

Just a quick suggestion. It looks like MeasureString returns an incorrect result if the layout rectangle has a negative height. Try something like this after setting the layout rectangle:

    If layout_rect.Height < 0 Then layout_rect.Height = 0

Or you may need to set it to 1 instead of 0. I don't know if it will have the same problem with 0 (and I can't check right now).

Let me know whether that works and I'll try it out tonight.



Rod
RodStephens@vb-helper.com
Author of "Visual Basic 2005 Programmer's Reference"
http://www.vb-helper.com/vb_prog_ref.htm
 
Old March 15th, 2006, 01:59 PM
Registered User
 
Join Date: Mar 2006
Posts: 3
Thanks: 0
Thanked 0 Times in 0 Posts
Default

You had that statement in your original code and I incorporated it into mine, but that did not fix the problem. It seems that when the layout_rect.Height is less than one line height, the text_size function should return 0 characters_filled and 0 lines. That should make the routine exit and set e.HasMorePages to True. It does work this way for the PrintPreview dialog, but it doesn't work on an actual printer (I tried it on 2). I don't understand why the formatting is correct in the dialog, but incorrect on paper??

I am getting closer to a solution by setting the e.MarginBounds.Bottom to a larger number and it seems to work most of the time. I will keep trying.

Thanks for the reply.

 
Old March 16th, 2006, 10:00 AM
Rod Stephens's Avatar
Wrox Author
 
Join Date: Jan 2006
Posts: 647
Thanks: 2
Thanked 96 Times in 95 Posts
Default

I think this is a bug in the MeasureString method (or perhaps whatever it is talking to). Even if it required the layout rectangle's height to be greater than 1, it should be consistent.

It seems that MeasureString is always allowing you to print at least one line. I think the fact that you are printing lots of single-line paragraphs is relevant.

It seems to work if you compare the available height with the font's m-height before printing like this:

    Dim m_height As Single = e.Graphics.MeasureString("M", the_font).Height
    If e.MarginBounds.Bottom - ymin < m_height Then
        ' There's no room for anything.
        characters_fitted = 0 ' This breaks out of the loop.
    Else
        ' Print as before ...
    End If

You can download a revised project at http://www.vb-helper.com/VbProgRef/U...intBooklet.zip.

Let me know whether it works on your system.

Rod

Rod
RodStephens@vb-helper.com
Author of "Visual Basic 2005 Programmer's Reference"
http://www.vb-helper.com/vb_prog_ref.htm
 
Old March 16th, 2006, 10:06 AM
Rod Stephens's Avatar
Wrox Author
 
Join Date: Jan 2006
Posts: 647
Thanks: 2
Thanked 96 Times in 95 Posts
Default

(Note that the forum software cleverly included the trailing period in the revised project's link in the previous posting. Copy and paste the link text into your browser and remove the period at the end.)

Rod
RodStephens@vb-helper.com
Author of "Visual Basic 2005 Programmer's Reference"
http://www.vb-helper.com/vb_prog_ref.htm
 
Old March 16th, 2006, 12:39 PM
Registered User
 
Join Date: Mar 2006
Posts: 3
Thanks: 0
Thanked 0 Times in 0 Posts
Default

That solution worked. The formatting remains different for the Print Preview dialog as compared to the paper, but at least it is generating a new page at the appropriate place and there are no dropped lines or paragraphs. I am sure it has something to do with the single line paragraphs as you mentioned.

Thanks a lot for the help - great book, by the way!

Al Mowbray

 
Old March 16th, 2006, 01:34 PM
Rod Stephens's Avatar
Wrox Author
 
Join Date: Jan 2006
Posts: 647
Thanks: 2
Thanked 96 Times in 95 Posts
Default

> That solution worked.
Excellent!

> The formatting remains different for the Print Preview
> dialog as compared to the paper, ...
Hmm. Why don't you zip up the project and email it to me (RodStephens@vb-helper.com) and I'll take a look to see if I can suggest anything. (Could there be a font difference? Perhaps the system and printer are picking slightly different fonts.)

> Thanks a lot for the help
No problem. Let me know if you have other problems, comments, etc. either here or vis direct email. I'll post any updates on my Web site.

> - great book, by the way!
Thanks! Always nice to hear someone finds this all useful. Post a review on Amazon!

Best wishes,


Rod
RodStephens@vb-helper.com
Author of "Visual Basic 2005 Programmer's Reference"
http://www.vb-helper.com/vb_prog_ref.htm
 
Old May 3rd, 2006, 10:16 AM
Registered User
 
Join Date: Mar 2006
Posts: 9
Thanks: 0
Thanked 0 Times in 0 Posts
Default

This is a wonderful example of many aspects of Object oriented programming for an old VB 6 programmer like myself who is just learning.

However, I think I have found two comment errors (in both the book and the revised downloadabled version) in the "PrintPage" event handler, in the 'if-then-else' statement where the Page # is being printed in the margin. The first comment for "even" pages:

    "'The gutter is on the left"

is incorrect. It should be changed to:

    "'The page # is printed on the left, opposite the gutter on the right"

Similarly, the comment that follows for "odd" pages also needs to be changed.

Truman Byron Thomas
 
Old May 5th, 2006, 07:23 AM
Rod Stephens's Avatar
Wrox Author
 
Join Date: Jan 2006
Posts: 647
Thanks: 2
Thanked 96 Times in 95 Posts
Default

Yes, you're right. I'll post a note on the book's Web page.


Rod
RodStephens@vb-helper.com
Author of "Visual Basic 2005 Programmer's Reference"
http://www.vb-helper.com/vb_prog_ref.htm





Similar Threads
Thread Thread Starter Forum Replies Last Post
reg conn to sql server 2005 from vb.net 2005.. veda SQL Server 2005 2 July 1st, 2008 12:16 AM
Printing in VB 2005 BroadwayLion Visual Basic 2005 Basics 7 February 19th, 2008 11:28 AM
Should I go VB.NET or VB 2005 from VB6? HB Visual Studio 2005 1 December 9th, 2005 03:13 AM
printing from datagrid in VB.NET emilyjeany VB Databases Basics 0 September 22nd, 2004 09:16 AM
Printing Forms in VB.NET? gisenberg VB.NET 2002/2003 Basics 0 July 23rd, 2004 08:22 PM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.