dcsimg
Results 1 to 10 of 10

Thread: [RESOLVED] print problem with drawstring method

  1. #1

    Thread Starter
    Member
    Join Date
    Jul 2012
    Posts
    60

    Resolved [RESOLVED] print problem with drawstring method

    Following is a rather lengthy bit of code. The problem is with the printing. The strings are formatted perfectly as verified with 'debug.print' in several locations. But, the printed copy is not formatted correctly. Specifically, in the section marked *** when the 'else' is called, everything in that line shifts to the left. I am assuming it is something stupid and I will be embarrassed at the error, but it is making me nuts. Thanks for whatever help you may offer.

    Sub PrintDocument1_PrintPage(ByVal sender As System.Object, ByVal e As System.Drawing.Printing.PrintPageEventArgs) Handles PrintDocument1.PrintPage
    Dim sString As String
    Static i As Integer
    Dim nmbrlines As Integer = 0
    Dim s As Integer
    Dim FitArray(NmbrWOrecords) As Date
    Dim drawFont As New Font("Arial", 9)


    On Error GoTo handler

    Dim dmyToday As Date = Date.Now
    sString = dmyToday.ToString("MM/dd/yyyy")
    e.Graphics.DrawString(sString, drawFont, Brushes.Black, 50, 32)


    sString = String.Format("{0,10}{1,20}{2,20}{3,20}{4,20}{5,20}{6,20}{7,20}", "WOnmbr", "CutJoin", "Sand", _
    "Finish", "Fit", "DUE", "# Frames", "MLDG")
    e.Graphics.DrawString(sString, drawFont, Brushes.Black, 50, 64)
    Debug.Print(sString)

    For j = i To NmbrWOrecords - 1
    With dsWO.Tables("ActiveWO")
    Dim dmyBlank As String = ""
    Dim dmy1Sched As Date = (dsWO.Tables("ActiveWO").Rows(j).Item("SchedDate"))
    Dim dmySched As String = dmy1Sched.ToString("MM/dd")

    If .Rows(j).Item("Mldg1Vendor") = "Absolute" Then
    sString = String.Format("{0,10}{1,20}{2,20}{3,20}{4,20}{5,20}{6,20}{7,20}", .Rows(j)("WOnmbr"), .Rows(j)("CutJoin"), .Rows(j)("Sand"), _
    .Rows(j)("Finish"), .Rows(j)("Fit"), dmySched, .Rows(j)("NmbrFrames"), .Rows(j)("Mldg1Vendor"))
    Debug.Print(sString)
    Else
    *** sString = String.Format("{0,10}{1,20}{2,20}{3,20}{4,20}{5,20}{6,20}{7,20}", .Rows(j)("WOnmbr"), dmyBlank, dmyBlank, _
    .Rows(j)("Finish"), .Rows(j)("Fit"), dmySched, .Rows(j)("NmbrFrames"), .Rows(j)("Mldg1Vendor"))
    Debug.Print(sString)
    End If



    nmbrlines = nmbrlines + 1
    If nmbrlines = 1 Then
    s = 96
    Else
    s = s + 20
    End If

    e.Graphics.DrawString(sString, drawFont, Brushes.Black, 50, s)
    Debug.Print(sString)

    End With

    If nmbrlines >= 45 And j < NmbrWOrecords Then
    e.HasMorePages = True
    i = j
    Exit Sub
    End If

    If j = NmbrWOrecords Then
    e.HasMorePages = False
    Exit Sub
    End If
    Next

    Exit Sub
    handler: MsgBox("Unexpected error in Sub PrintDocument1" & Err.Description.ToString)
    End Sub

  2. #2
    .NUT jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    99,452

    Re: print problem with drawstring method

    Please use appropriate formatting tags when posting code snippets for readability.
    Code:
     Sub PrintDocument1_PrintPage(ByVal sender As System.Object, ByVal e As System.Drawing.Printing.PrintPageEventArgs) Handles PrintDocument1.PrintPage
            Dim sString As String
            Static i As Integer
            Dim nmbrlines As Integer = 0
            Dim s As Integer
            Dim FitArray(NmbrWOrecords) As Date
            Dim drawFont As New Font("Arial", 9)
           
    
            On Error GoTo handler
    
            Dim dmyToday As Date = Date.Now
            sString = dmyToday.ToString("MM/dd/yyyy")
            e.Graphics.DrawString(sString, drawFont, Brushes.Black, 50, 32)
    
    
            sString = String.Format("{0,10}{1,20}{2,20}{3,20}{4,20}{5,20}{6,20}{7,20}", "WOnmbr", "CutJoin", "Sand", _
                 "Finish", "Fit", "DUE", "# Frames", "MLDG")
            e.Graphics.DrawString(sString, drawFont, Brushes.Black, 50, 64)
            Debug.Print(sString)
    
            For j = i To NmbrWOrecords - 1
                With dsWO.Tables("ActiveWO")
               Dim dmyBlank As String = ""
               Dim dmy1Sched As Date = (dsWO.Tables("ActiveWO").Rows(j).Item("SchedDate"))
               Dim dmySched As String = dmy1Sched.ToString("MM/dd")
    
                    If .Rows(j).Item("Mldg1Vendor") = "Absolute" Then
                        sString = String.Format("{0,10}{1,20}{2,20}{3,20}{4,20}{5,20}{6,20}{7,20}", .Rows(j)("WOnmbr"), .Rows(j)("CutJoin"), .Rows(j)("Sand"), _
                        .Rows(j)("Finish"), .Rows(j)("Fit"), dmySched, .Rows(j)("NmbrFrames"), .Rows(j)("Mldg1Vendor"))
                        Debug.Print(sString)
                    Else
                       sString = String.Format("{0,10}{1,20}{2,20}{3,20}{4,20}{5,20}{6,20}{7,20}", .Rows(j)("WOnmbr"), dmyBlank, dmyBlank, _
                       .Rows(j)("Finish"), .Rows(j)("Fit"), dmySched, .Rows(j)("NmbrFrames"), .Rows(j)("Mldg1Vendor"))
                        Debug.Print(sString)
                    End If
    
                   
    
                    nmbrlines = nmbrlines + 1
                    If nmbrlines = 1 Then
                        s = 96
                    Else
                        s = s + 20
                    End If
    
                        e.Graphics.DrawString(sString, drawFont, Brushes.Black, 50, s)
                        Debug.Print(sString)
                    
                End With
                
                If nmbrlines >= 45 And j < NmbrWOrecords Then
                    e.HasMorePages = True
                    i = j
                    Exit Sub
                End If
    
                If j = NmbrWOrecords Then
                    e.HasMorePages = False
                    Exit Sub
                End If
            Next
    
            Exit Sub
    handler: MsgBox("Unexpected error in Sub PrintDocument1" & Err.Description.ToString)
        End Sub
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  3. #3

    Thread Starter
    Member
    Join Date
    Jul 2012
    Posts
    60

    Re: print problem with drawstring method

    Actually, in the 'edit' window it was properly formatted ... somehow it all got lost when I posted. Probably something else I don't know.

  4. #4
    .NUT jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    99,452

    Re: print problem with drawstring method

    Quote Originally Posted by vbchJack View Post
    Actually, in the 'edit' window it was properly formatted ... somehow it all got lost when I posted. Probably something else I don't know.
    That's how HTML works. That's why you need tags: to preprocess the code and ensure that the resulting HTML honours the original format.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  5. #5
    Sinecure devotee
    Join Date
    Aug 2013
    Location
    Southern Tier NY
    Posts
    4,541

    Re: print problem with drawstring method

    What happens if you set dmyBlank to a number of spaces instead of an empty string?

  6. #6

    Thread Starter
    Member
    Join Date
    Jul 2012
    Posts
    60

    Re: print problem with drawstring method

    I did try that. "CutJoin" and "Sand" are typically 5 characters. So, at one time I had < dim dmyBlank as String=" " >
    It had no effect.
    I also printed to pdf to see if it was my printer doing something but the pdf file has the same anomaly as the hard printed copy.

  7. #7
    .NUT jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    99,452

    Re: print problem with drawstring method

    I would tend not to write it like this to begin with:
    Code:
                    If .Rows(j).Item("Mldg1Vendor") = "Absolute" Then
                        sString = String.Format("{0,10}{1,20}{2,20}{3,20}{4,20}{5,20}{6,20}{7,20}", .Rows(j)("WOnmbr"), .Rows(j)("CutJoin"), .Rows(j)("Sand"), _
                        .Rows(j)("Finish"), .Rows(j)("Fit"), dmySched, .Rows(j)("NmbrFrames"), .Rows(j)("Mldg1Vendor"))
                        Debug.Print(sString)
                    Else
                       sString = String.Format("{0,10}{1,20}{2,20}{3,20}{4,20}{5,20}{6,20}{7,20}", .Rows(j)("WOnmbr"), dmyBlank, dmyBlank, _
                       .Rows(j)("Finish"), .Rows(j)("Fit"), dmySched, .Rows(j)("NmbrFrames"), .Rows(j)("Mldg1Vendor"))
                        Debug.Print(sString)
                    End If
    I would only call String.Format once like this:
    Code:
    Dim cutJoin As String = String.Empty
    Dim sand As String = String.Empty
    
    If CStr(.Rows(j)("Mldg1Vendor")) = "Absolute" Then
        cutJoin = CStr(.Rows(j)("CutJoin"))
        sand = CStr(.Rows(j)("Sand"))
    End If
    
    sString = String.Format(...)
    I'd be interested to know whether you see the same behaviour in that case.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  8. #8
    Frenzied Member
    Join Date
    Jul 2011
    Location
    UK
    Posts
    1,151

    Re: print problem with drawstring method

    I suspect the problem is due to using a proportional font.


    In your format string, you are specifying field widths:
    Code:
    sString = String.Format("{0,10}{1,20}{2,20}.....
    So you want your second and third fields to each be 20 characters wide.

    If the value you are passing to the field is less than the desired width , the field is padded with Space characters. An "empty" field is actually going to be a field of 20 Space characters.


    Now your problem is that you are using Arial as your Font.

    Arial is a proportional font: each character has a different width. The Space character is one of the narrowest characters.

    So a field of 20 Spaces is going to be narrower than a field of 15 Spaces plus 5 digits/letters.

    In your Else clause, you specify 2 "empty" fields next to each other. That's doubling the difference in displayed field width, so everything following these fields will be noticeably shifted to the left.


    The easy fix is to use a monospaced (fixed width) Font like Courier New, or Segoe UI Mono. Unfortunately the resultant text lines will probably be much wider than you are seeing now, so you'll have to adjust the field widths if you can, (which is another good reason you should only define the format string the one time, as suggested by JMC).

  9. #9

    Thread Starter
    Member
    Join Date
    Jul 2012
    Posts
    60

    Re: print problem with drawstring method

    Hey jmcilhinney
    I like your code better than mine ... more efficient and 'elegant.' But, it did not solve the problem.
    Inferrd nailed it ... the proportional font was the problem.

  10. #10

    Thread Starter
    Member
    Join Date
    Jul 2012
    Posts
    60

    Re: print problem with drawstring method

    Thanks Inferred ... the proportional font was the problem. Using Courier New does widen the overall spread but there was enough room. All of the "20s" were heavily padded to fill a page with Arial so it was just a matter of reducing that by half.
    Thanks again, it was driving me crazy!

Tags for this Thread

Posting Permissions

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



Featured


Click Here to Expand Forum to Full Width