Results 1 to 8 of 8

Thread: [RESOLVED] Center Title when printing

  1. #1

    Thread Starter
    Fanatic Member
    Join Date
    Dec 2012
    Posts
    540

    Resolved [RESOLVED] Center Title when printing

    I am printing a listview and in the preview it looks fine. But when it actually prints this is what i get. Attachment 95807

    This is the code associated in the print code.

    Code:
                If title <> "" Then
                    Dim tFont As New Drawing.Font(lv.Font.FontFamily, CType(lv.Font.Size * 1.5, Single), FontStyle.Bold)
    
                    Dim r As New Rectangle(startX + CInt(columnSum / 2) - CInt(e.Graphics.MeasureString(title, tFont).Width / 2), startY + 2, CInt(columnSum / 2), tFont.Height)
                    e.Graphics.DrawString(title, tFont, Brushes.Black, r)
                    startY += tFont.Height + 2
                End If
    Im not sure where i fix it so that the title is centered.

  2. #2
    Super Moderator dday9's Avatar
    Join Date
    Mar 2011
    Posts
    12,397

    Re: Center Title when printing

    Set up a breakpoint where you declare the rectangle and I'm sure you'll find that the x position is off. You'll have to adjust your equation based on what you get when you set up the breakpoint.
    "Code is like humor. When you have to explain it, it is bad." - Cory House
    VbLessons | HtmlLessons | CssLessons | Code Tags | Sword of Fury - Jameram

  3. #3

    Thread Starter
    Fanatic Member
    Join Date
    Dec 2012
    Posts
    540

    Re: Center Title when printing

    Whats even weirder is that every copy i print the title moves more to the right..... I don't understand.

  4. #4
    PowerPoster dunfiddlin's Avatar
    Join Date
    Jun 2012
    Posts
    8,245

    Re: Center Title when printing

    Quote Originally Posted by dday9 View Post
    Set up a breakpoint where you declare the rectangle and I'm sure you'll find that the x position is off. You'll have to adjust your equation based on what you get when you set up the breakpoint.
    If this was true (and it isn't) then it should show up in the Print Preview as well. The mystery is how the print position can differ so wildly between preview and actual print and at the moment I've gotta say I have absolutely no idea how that's possible especially since StartX is essentially a constant used for all the subsequent lines too.
    As the 6-dimensional mathematics professor said to the brain surgeon, "It ain't Rocket Science!"

    Reviews: "dunfiddlin likes his DataTables" - jmcilhinney

    Please be aware that whilst I will read private messages (one day!) I am unlikely to reply to anything that does not contain offers of cash, fame or marriage!

  5. #5
    PowerPoster dunfiddlin's Avatar
    Join Date
    Jun 2012
    Posts
    8,245

    Re: Center Title when printing

    Ok I've run this through a few times and I'm getting some very weird things happening on subsequent copies of the same document which don't match the OP's experience at all even without running a title so something is broken somewhere but I can't for the life of me see what at the moment. I just don't understand how you can run the same code with all the values essentially the same (most of them declared in the Sub, after all!) and get totally different results on preview, print, and print 2. I may not sleep tonight!
    As the 6-dimensional mathematics professor said to the brain surgeon, "It ain't Rocket Science!"

    Reviews: "dunfiddlin likes his DataTables" - jmcilhinney

    Please be aware that whilst I will read private messages (one day!) I am unlikely to reply to anything that does not contain offers of cash, fame or marriage!

  6. #6

    Thread Starter
    Fanatic Member
    Join Date
    Dec 2012
    Posts
    540

    Re: Center Title when printing

    Thats exacly my issue..... I am with you on not being able to sleep. Driving me nuts.

  7. #7
    PowerPoster dunfiddlin's Avatar
    Join Date
    Jun 2012
    Posts
    8,245

    Re: Center Title when printing

    Got it. The column width wasn't being reset so on each pass was increasing, to the point that it eventually was off the page. So if, for example, for the preview column the value was 315, for the actual print it was 630, for the next print it was 915 and so on. As my centring calculation used this value it was skewing further and further off course. The solution ... in listviewprinter.vb ....

    vb.net Code:
    1. Private Sub pd_BeginPrint(ByVal sender As Object, ByVal e As System.Drawing.Printing.PrintEventArgs) Handles pd.BeginPrint
    2.         ''this removes the printed page margins
    3.         pd.OriginAtMargins = True
    4.         pd.DefaultPageSettings.Margins = New Drawing.Printing.Margins(location.X, 0, location.Y, 0)
    5.  
    6.         pages = New Dictionary(Of Integer, pageDetails)
    7.  
    8.         Dim maxWidth As Integer = CInt(pd.DefaultPageSettings.PrintableArea.Width) - 40
    9.         Dim maxHeight As Integer = CInt(pd.DefaultPageSettings.PrintableArea.Height) - 40
    10.  
    11.         Dim pageCounter As Integer = 0
    12.         pages.Add(pageCounter, New pageDetails With {.headerIndices = New List(Of Integer)})
    13.  
    14.         Dim columnCounter As Integer = 0
    15.         columnSum = 0 'ADD THIS LINE TO RESET VALUE BEFORE NEW CALCULATION
    As the 6-dimensional mathematics professor said to the brain surgeon, "It ain't Rocket Science!"

    Reviews: "dunfiddlin likes his DataTables" - jmcilhinney

    Please be aware that whilst I will read private messages (one day!) I am unlikely to reply to anything that does not contain offers of cash, fame or marriage!

  8. #8

    Thread Starter
    Fanatic Member
    Join Date
    Dec 2012
    Posts
    540

    Re: Center Title when printing

    That did it Thanks.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width