Wrox Programmer Forums

Need to download code?

View our list of code downloads.

Register | FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read
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 tens of thousands of software programmers and website developers including Wrox book authors and readers. As a guest, you can read any forum posting. By joining today you can post your own programming questions, respond to other developersí questions, and eliminate the ads that are displayed to guests. Registration is fast, simple and absolutely free .
DRM-free e-books 300x50
Reply
 
Thread Tools Display Modes
  #1 (permalink)  
Old March 14th, 2006, 09:34 PM
Registered User
 
Join Date: Mar 2006
Location: , , .
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

Reply With Quote
  #2 (permalink)  
Old March 15th, 2006, 10:02 AM
Rod Stephens's Avatar
Wrox Author
Points: 3,187, Level: 23
Points: 3,187, Level: 23 Points: 3,187, Level: 23 Points: 3,187, Level: 23
Activity: 50%
Activity: 50% Activity: 50% Activity: 50%
 
Join Date: Jan 2006
Location: , , .
Posts: 646
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
Reply With Quote
  #3 (permalink)  
Old March 15th, 2006, 12:23 PM
Rod Stephens's Avatar
Wrox Author
Points: 3,187, Level: 23
Points: 3,187, Level: 23 Points: 3,187, Level: 23 Points: 3,187, Level: 23
Activity: 50%
Activity: 50% Activity: 50% Activity: 50%
 
Join Date: Jan 2006
Location: , , .
Posts: 646
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
Reply With Quote
  #4 (permalink)  
Old March 15th, 2006, 01:59 PM
Registered User
 
Join Date: Mar 2006
Location: , , .
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.

Reply With Quote
  #5 (permalink)  
Old March 16th, 2006, 10:00 AM
Rod Stephens's Avatar
Wrox Author
Points: 3,187, Level: 23
Points: 3,187, Level: 23 Points: 3,187, Level: 23 Points: 3,187, Level: 23
Activity: 50%
Activity: 50% Activity: 50% Activity: 50%
 
Join Date: Jan 2006
Location: , , .
Posts: 646
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
Reply With Quote
  #6 (permalink)  
Old March 16th, 2006, 10:06 AM
Rod Stephens's Avatar
Wrox Author
Points: 3,187, Level: 23
Points: 3,187, Level: 23 Points: 3,187, Level: 23 Points: 3,187, Level: 23
Activity: 50%
Activity: 50% Activity: 50% Activity: 50%
 
Join Date: Jan 2006
Location: , , .
Posts: 646
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
Reply With Quote
  #7 (permalink)  
Old March 16th, 2006, 12:39 PM
Registered User
 
Join Date: Mar 2006
Location: , , .
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

Reply With Quote
  #8 (permalink)  
Old March 16th, 2006, 01:34 PM
Rod Stephens's Avatar
Wrox Author
Points: 3,187, Level: 23
Points: 3,187, Level: 23 Points: 3,187, Level: 23 Points: 3,187, Level: 23
Activity: 50%
Activity: 50% Activity: 50% Activity: 50%
 
Join Date: Jan 2006
Location: , , .
Posts: 646
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
Reply With Quote
  #9 (permalink)  
Old May 3rd, 2006, 11:16 AM
Registered User
 
Join Date: Mar 2006
Location: Hampton, VA, USA.
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
Reply With Quote
  #10 (permalink)  
Old May 5th, 2006, 08:23 AM
Rod Stephens's Avatar
Wrox Author
Points: 3,187, Level: 23
Points: 3,187, Level: 23 Points: 3,187, Level: 23 Points: 3,187, Level: 23
Activity: 50%
Activity: 50% Activity: 50% Activity: 50%
 
Join Date: Jan 2006
Location: , , .
Posts: 646
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
Reply With Quote
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off

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 01: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 10:16 AM
Printing Forms in VB.NET? gisenberg VB.NET 2002/2003 Basics 0 July 23rd, 2004 09:22 PM



All times are GMT -4. The time now is 12:36 AM.


Powered by vBulletin®
Copyright ©2000 - 2018, Jelsoft Enterprises Ltd.
© 2013 John Wiley & Sons, Inc.