Question : Problem with linespacing on Graphics.Drawstring

Still working on getting formatted output.  I have a routine using the Graphics.Drawstring method from the PrintPage EventArgs class in VB.NET printing output to a printer, and it works perfectly for single-line text fields, etc.  

However, when dealing with multi-line text fields where the users could insert carriage returns, and my need to insert manual line breaks, is not working.  I end up printing all lines on top of each other.

I've developed branching logic to search for VBCR in the field, and to go down a line for it but not print it.  I've also included logic to look for logical line breaks in the field.  

I'm sure this is a simple logic error but in the past 4 hours of stepping through my code I'm not seeing where the problem lies.  Anyone see?
Code Snippet:
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
31:
32:
33:
34:
35:
36:
37:
38:
39:
40:
41:
42:
43:
44:
45:
46:
47:
48:
49:
50:
51:
52:
53:
54:
55:
56:
57:
58:
59:
60:
61:
62:
63:
64:
65:
66:
67:
68:
69:
70:
71:
72:
73:
74:
75:
76:
77:
78:
79:
80:
81:
82:
83:
84:
85:
86:
87:
88:
89:
90:
91:
92:
93:
94:
95:
96:
97:
98:
99:
100:
101:
102:
103:
104:
105:
106:
107:
108:
109:
110:
111:
112:
113:
114:
115:
Private Sub PrintPage(ByVal sender As Object, ByVal ev As PrintPageEventArgs)
 
		Try
		    Dim yPos As Single = 100
		    Dim leftMargin As Single = ev.MarginBounds.Left
		    Dim topMargin As Single = ev.MarginBounds.Top
		    Dim drawBoldFont As New Font("Arial", 10, FontStyle.bold)
		    Dim drawFont As New Font("Arial", 10)
		    Dim lines as single = 17
		    Dim spaces as single = 7
		    Dim header as string
		    Dim x as integer
		    dim linesToSkip as integer
 
		    
 
 
		    'Add the page heading and the time submitted
	ev.Graphics.DrawString("THE FOLLOWING REQUEST HAS BEEN SUBMITTED", drawBoldFont, Brushes.Black, leftMargin, yPos, New StringFormat())
			yPos = yPos + (2*lines)
			 
			header = "Department:  "
			ev.Graphics.DrawString(header, drawBoldFont, Brushes.Black, leftMargin, yPos, New StringFormat())  
			ev.Graphics.DrawString(Me.txtDepartment.Text.Trim, drawFont, Brushes.Black, leftMargin+(len(header)*spaces), yPos, New StringFormat()) 
			yPos = yPos + (1*lines)
			
			header = "Contact Name:  "
			ev.Graphics.DrawString(header, drawBoldFont, Brushes.Black, leftMargin, yPos, New StringFormat())
			ev.Graphics.DrawString(Me.txtContact.Text.Trim, drawFont, Brushes.Black, leftMargin+(len(header)*spaces), yPos, New StringFormat()) 
			yPos = yPos + (1*lines)
			
			header = "Phone:  "
			ev.Graphics.DrawString(header, drawBoldFont, Brushes.Black, leftMargin, yPos, New StringFormat())
			ev.Graphics.DrawString(Me.txtPhone.Text.Trim, drawFont, Brushes.Black, leftMargin+(len(header)*spaces), yPos, New StringFormat()) 
			yPos = yPos + (2*lines)
		
'*********All of the above work fine, but when I start getting to the multi-line field below it breaks up	
			header = "Message:  "
			ev.Graphics.DrawString(header, drawBoldFont, Brushes.Black, leftMargin, yPos, New StringFormat())
			x = 1
			linesToSkip = 1
			
			dim currentChar As Integer
        		dim currentLine As Integer
            currentChar = 0
			currentLine = 1
            Dim chars As Integer
            Dim foundLineReturn As Boolean
            
            While currentChar < Me.txtMenu.Text.Length
                chars = 0
                foundLineReturn = False
                While chars < 80
                    If Me.txtMenu.Text.Substring(currentChar + chars, 1) = vbCr Then
                        foundLineReturn = True
                        If chars > 1 Then
                            If currentLine = 1 Then
                                ev.Graphics.DrawString(Me.txtMenu.Text.Substring(currentChar, chars), drawFont, Brushes.Black, leftMargin + (Len(header) * spaces), yPos, New StringFormat())
                            Else
                                ev.Graphics.DrawString(Me.txtMenu.Text.Substring(currentChar, chars), drawFont, Brushes.Black, leftMargin, yPos, New StringFormat())
                            End If
                        End If
                        yPos = yPos + (2 * lines)
                        chars += 1
                        currentChar = currentChar + chars
                        Exit While
                    Else
                        chars += 1
                    End If
                End While
 
                If foundLineReturn = False Then
                    If Me.txtMenu.Text.Substring(currentChar + chars, 1) <> " " And _
                        Me.txtMenu.Text.Substring(currentChar + chars, 1) <> "-" And _
                        Me.txtMenu.Text.Substring(currentChar + chars, 1) <> vbLf Then
                        ' As long as the last character is not space or carriage return, 
                        ' keep subtracting another character from the text to be printed.
                        chars = 80
                        If currentLine = 1 Then chars = chars - 20
                        While chars > 0 AndAlso _
                             Me.txtMenu.Text.Substring(currentChar + chars, 1) <> " " AndAlso _
                             Me.txtMenu.Text.Substring(currentChar + chars, 1) <> vbLf
                            chars -= 1
                        End While
                        chars += 1
                    End If
                    If chars = 0 Or chars = 1 Then chars = 80
 
 
                    If currentLine = 1 Then
                        ev.Graphics.DrawString(Me.txtMenu.Text.Substring(currentChar, chars), drawFont, Brushes.Black, leftMargin + (Len(header) * spaces), yPos, New StringFormat())
                    Else
                        ev.Graphics.DrawString(Me.txtMenu.Text.Substring(currentChar, chars), drawFont, Brushes.Black, leftMargin, yPos, New StringFormat())
                    End If
                    currentChar = currentChar + chars
                    currentLine += 1
                    yPos = yPos + (1 * lines)
                End If
            End While
            yPos = yPos + (1 * lines)
 
'additionally none of the below prints either        
 
	    yPos = yPos + (2*lines)
	    header = "Sent: "
	    ev.Graphics.DrawString(header, drawBoldFont, Brushes.Black, leftMargin, yPos, New StringFormat())
	    ev.Graphics.DrawString(Now.ToShortDateString & " - " & Now.ToShortTimeString, drawFont, Brushes.Black, leftMargin+(len(header)*spaces), yPos, New StringFormat()) 
	    yPos = yPos + (2*lines)
		
		 
		Catch ex As Exception
		    HandleError("Error in escort request form PrintPage routine: " & ex.Message, "PrintPage")
		End Try
 
	    End Sub

Answer : Problem with linespacing on Graphics.Drawstring

Fixed it.  Started over; scrapped all code determining line spacing and just started over.  

As there's lots of questions about this on the internet but no solutions posted, I'm posting my code here which breaks up a textfield into 80 character lines on spaces, periods, hyphens, or breaks.
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
31:
32:
33:
34:
35:
36:
37:
38:
39:
40:
41:
42:
43:
44:
45:
46:
47:
48:
49:
50:
51:
52:
53:
54:
55:
56:
57:
58:
59:
60:
61:
62:
63:
64:
65:
66:
67:
68:
69:
70:
71:
72:
73:
74:
75:
76:
77:
78:
79:
80:
81:
82:
83:
84:
85:
86:
87:
88:
			
			Dim currentChar as integer
			dim strLength as integer
			dim charsPerLine as integer
			Dim currentStart as integer
			Dim currentEnd as integer
			
            Dim foundBreak As Boolean
            Dim foundCR As Boolean
 
			
			Dim testChar as String
			dim textstring as string
 
			charsPerLine = 80
            currentStart = 1
            foundCR = False
			
			textstring = Me.txtMenu.Text.Trim
			strLength = len(textstring)
            If strLength > charsPerLine Then
                While currentStart < strLength
                    foundCR = False
                    'find end of line
                    foundBreak = False
                    'first we see if the user put in a line feed in these 80 characters
                    currentChar = 1
                    While currentChar <= 80 And foundBreak = False
                        testChar = Mid(textstring, currentStart + currentChar, 1)
                        If testChar = vbCr _
                        Or testChar = vbCrLf _
                        Or testChar = vbLf _
                        Then
                            foundBreak = True
                            foundCR = True
                            '  currentChar = currentChar - 1
                        Else
                            currentChar = currentChar + 1
                        End If
                    End While
 
                    'they did not, we put one in
                    If foundBreak = False Then currentChar = charsPerLine
                    While currentChar > 0 And foundBreak = False
                        testChar = Mid(textstring, currentStart + currentChar, 1)
                        If testChar = " " _
                        Or testChar = "-" _
                        Or testChar = vbCr _
                        Or testChar = vbCrLf _
                        Or testChar = vbLf _
                        Then
                            foundBreak = True
                        Else
                            currentChar = currentChar - 1
                        End If
                    End While
                    If currentChar = 0 Then currentChar = 80 'no graceful break found; hard break used
                    ev.Graphics.DrawString(Trim(Mid(textstring, currentStart, currentChar)), drawFont, Brushes.Black, leftMargin, yPos, New StringFormat())
                    currentStart = currentStart + currentChar
 
                    If foundCR = True Then currentStart = currentStart + 1
 
                    testChar = Mid(textstring, currentStart, 1)
 
                    While testChar = vbCr _
                        Or testChar = vbCrLf _
                        Or testChar = vbLf
                        currentStart = currentStart + 1
                        testChar = Mid(textstring, currentStart, 1)
                    End While
                    yPos = yPos + (1 * lines)
                End While
 
 
            Else
                'just one line, print it
                ev.Graphics.DrawString(Me.txtMenu.Text.Trim, drawFont, Brushes.Black, leftMargin, yPos, New StringFormat())
                Dim i As Integer
                For i = 1 To Len(Me.txtMenu.Text.Trim)
                    If Mid(Me.txtMenu.Text.Trim, i, 1) = vbCr _
                    Or Mid(Me.txtMenu.Text.Trim, i, 1) = vbLf _
                    Or Mid(Me.txtMenu.Text.Trim, i, 1) = vbCrLf Then
                        yPos = yPos + (1 * lines)
                    End If
                Next
                yPos = yPos + (1 * lines)
            End If
			
Random Solutions  
 
programming4us programming4us