We help IT Professionals succeed at work.
Get Started

Problem with linespacing on Graphics.Drawstring

thearniec
thearniec asked
on
2,748 Views
Last Modified: 2012-05-06
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

Open in new window

Comment
Watch Question
This problem has been solved!
Unlock 1 Answer and 10 Comments.
See Answer
Why Experts Exchange?

Experts Exchange always has the answer, or at the least points me in the correct direction! It is like having another employee that is extremely experienced.

Jim Murphy
Programmer at Smart IT Solutions

When asked, what has been your best career decision?

Deciding to stick with EE.

Mohamed Asif
Technical Department Head

Being involved with EE helped me to grow personally and professionally.

Carl Webster
CTP, Sr Infrastructure Consultant
Ask ANY Question

Connect with Certified Experts to gain insight and support on specific technology challenges including:

  • Troubleshooting
  • Research
  • Professional Opinions
Did You Know?

We've partnered with two important charities to provide clean water and computer science education to those who need it most. READ MORE