troubleshooting Question

Problem with linespacing on Graphics.Drawstring

Avatar of thearniec
thearniec asked on
Visual Basic.NETASP.NETASP
10 Comments1 Solution2749 ViewsLast Modified:
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?
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
ASKER CERTIFIED SOLUTION
thearniec

Our community of experts have been thoroughly vetted for their expertise and industry experience.

Join our community to see this answer!
Unlock 1 Answer and 10 Comments.
Start Free Trial
Learn from the best

Network and collaborate with thousands of CTOs, CISOs, and IT Pros rooting for you and your success.

Andrew Hancock - VMware vExpert
See if this solution works for you by signing up for a 7 day free trial.
Unlock 1 Answer and 10 Comments.
Try for 7 days

”The time we save is the biggest benefit of E-E to our team. What could take multiple guys 2 hours or more each to find is accessed in around 15 minutes on Experts Exchange.

-Mike Kapnisakis, Warner Bros