What I'm trying to do is run an SQL query, have the results build a string and then print the string. I have found that with the string if I put & vbCrLf twice after the string the first two pages are blank and it starts printing from the 24th record, if I only have one &vbCrLf it leaves one page blank and starts printing from the 16th record. However if I replace the string with a "test string" it prints from the first page fine, also if I leave out the LOCATION1 field it prints from the first page no worries. I have tested the string for "non-printable" characters and changed fonts as some charaters aren't avaliable in certain font's without success.
I have the string echo into a textbox at the end which ALWAYS displays fine. Fair to say I'm out of ideas! I am using Visual Studio 2010
Don't pay attention to the color coding of the code, I have comments in it that this forum isn't liking to much
This is the code I have so far
Imports System.IO
Imports System.Drawing.Printing
Imports System.Data
Imports System.Data.SqlClient
Imports System.Text
Public Class rptClocN
Private Sub rptClocN_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
PrintPreviewDialog1.WindowState = FormWindowState.Maximized
PrintPreviewDialog1.ShowDialog()
End Sub
Dim Counter As Decimal = 1
Dim EndCount As Decimal = 0
Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
If Counter = 4 Then
Label1.Text = "Printing..."
Counter = 0
End If
If Counter = 3 Then
Label1.Text = "Printing.."
End If
If Counter = 2 Then
Label1.Text = "Printing."
End If
If Counter = 1 Then
Label1.Text = "Printing"
End If
Counter = Counter + 1
EndCount = EndCount + 1
If EndCount = 20 Then
End If
End Sub
Private Sub Timer1_Dispose(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Disposed
Me.Close()
MenuReports.Show()
End Sub
Public Sub PrintDocument1_PrintPage(ByVal sender As Object, ByVal e As System.Drawing.Printing.PrintPageEventArgs) Handles PrintDocument1.PrintPage
Dim strHeading As String
strHeading = "This is a heading"
'strText = "This is a test string" & vbCrLf & "This is a test string" & vbCrLf & "This is a test string" & vbCrLf & "This is a test string" & vbCrLf & "This is a test string" & vbCrLf & "This is a test string"& vbCrLf & "This is a test string"& vbCrLf & "This is a test string"& vbCrLf & "This is a test string"& vbCrLf & "This is a test string"& vbCrLf & "This is a test string"& vbCrLf & "This is a test string"& vbCrLf & "This is a test string"& vbCrLf & "This is a test string"& vbCrLf & "This is a test string"& vbCrLf & "This is a test string"& vbCrLf & "This is a test string"& vbCrLf & "This is a test string"& vbCrLf & "This is a test string"& vbCrLf & "This is a test string"& vbCrLf & "This is a test string"& vbCrLf & "This is a test string"& vbCrLf & "This is a test string"& vbCrLf & "This is a test string"& vbCrLf & "This is a test string"& vbCrLf & "This is a test string"& vbCrLf & "This is a test string"& vbCrLf & "This is a test string"& vbCrLf & "This is a test string"& vbCrLf & "This is a test string"& vbCrLf & "This is a test string"& vbCrLf & "This is a test string"& vbCrLf & "This is a test string"& vbCrLf & "This is a test string"& vbCrLf & "This is a test string"& vbCrLf & "This is a test string"& vbCrLf & "This is a test string"& vbCrLf & "This is a test string"& vbCrLf & "This is a test string"& vbCrLf & "This is a test string"& vbCrLf & "This is a test string"& vbCrLf & "This is a test string"& vbCrLf & "This is a test string"& vbCrLf & "This is a test string"& vbCrLf & "This is a test string"& vbCrLf & "This is a test string"& vbCrLf & "This is a test string"& vbCrLf & "This is a test string"& vbCrLf & "This is a test string"& vbCrLf & "This is a test string"& vbCrLf & "This is a test string"& vbCrLf & "This is a test string"& vbCrLf & "This is a test string"& vbCrLf & "This is a test string"& vbCrLf & "This is a test string"& vbCrLf & "This is a test string"& vbCrLf & "This is a test string"& vbCrLf & "This is a test string"& vbCrLf & "This is a test string"& vbCrLf & "This is a test string"& vbCrLf & "This is a test string"& vbCrLf & "This is a test string"& vbCrLf & "This is a test string"& vbCrLf & "This is a test string"& vbCrLf & "This is a test string"& vbCrLf & "This is a test string"& vbCrLf & "This is a test string"& vbCrLf & "This is a test string"& vbCrLf & "This is a test string"& vbCrLf & "This is a test string"& vbCrLf & "This is a test string"& vbCrLf & "This is a test string"& vbCrLf & "This is a test string"& vbCrLf & "This is a test string"& vbCrLf & "This is a test string"& vbCrLf & "This is a test string"& vbCrLf & "This is a test string"& vbCrLf & "This is a test string"& vbCrLf & "This is a test string"& vbCrLf & "This is a test string"& vbCrLf & "This is a test string"& vbCrLf & "This is a test string"& vbCrLf & "This is a test string"& vbCrLf & "This is a test string"& vbCrLf & "This is a test string"& vbCrLf & "This is a test string"& vbCrLf & "This is a test string"& vbCrLf & "This is a test string"& vbCrLf & "This is a test string"& vbCrLf & "This is a test string"& vbCrLf & "This is a test string"& vbCrLf & "This is a test string"& vbCrLf & "This is a test string"& vbCrLf & "This is a test string"& vbCrLf & "This is a test string"& vbCrLf & "This is a test string"& vbCrLf & "This is a test string"& vbCrLf & "This is a test string"& vbCrLf & "This is a test string"& vbCrLf & "This is a test string"& vbCrLf & "This is a test string"& vbCrLf & "This is a test string"& vbCrLf & "This is a test string"& vbCrLf & "This is a test string"& vbCrLf & "This is a test string"& vbCrLf & "This is a test string"& vbCrLf & "This is a test string"& vbCrLf & "This is a test string"& vbCrLf & "This is a test string"& vbCrLf & "This is a test string"& vbCrLf & "This is a test string"& vbCrLf & "This is a test string"
'Fix for forum
Static page_number As Int32
Static errorlevel As Boolean = False
Static strText As String
page_number = page_number + 1
If page_number = 1 Then
strText = ""
Dim cn As SqlConnection = New SqlConnection()
Dim cmd As SqlCommand = New SqlCommand()
Dim dr As SqlDataReader
Dim strSqlCommand As String
strSqlCommand = "SELECT CLOCMAST.CUST_NO, CLOCMAST.CUST_LOCTN, CLOCMAST.LOCATION1, CLOCMAST.LOCATION2, CLOCMAST.LOCATION3, CUSTMAST.CUST_NO, CUSTMAST.CUST_NAME FROM CLOCMAST, CUSTMAST WHERE CLOCMAST.CUST_NO = CUSTMAST.CUST_NO ORDER BY CLOCMAST.CUST_NO, CLOCMAST.CUST_LOCTN"
cn.ConnectionString = "Data Source=GUNWS31\TESTINGSQLSERVER;Initial Catalog=manage;User Id=sa;Password=F@1ry,16;"
cmd.Connection = cn
cmd.CommandText = strSqlCommand
Try
cn.Open()
Try
dr = cmd.ExecuteReader()
Dim loc1, loc2, loc3 As String
While dr.Read()
If IsDBNull(dr.Item("LOCATION1")) = True Then
loc1 = " "
Else
loc1 = cleanString(Trim(dr.Item("LOCATION1")))
End If
If IsDBNull(dr.Item("LOCATION2")) = True Then
loc2 = " "
Else
loc2 = dr.Item("LOCATION2")
End If
If IsDBNull(dr.Item("LOCATION3")) = True Then
loc3 = " "
Else
loc3 = dr.Item("LOCATION3")
End If
'strText = strText & "Customer Number: " & dr.Item("CUST_NO") & " Location: " & dr.Item("CUST_LOCTN") & vbCrLf & "Customer Name: " & dr.Item("CUST_NAME") & vbCrLf & "Address: " & dr.Item("LOCATION1") & " " & dr.Item("LOCATION2") & " " & dr.Item("LOCATION3") & vbCrLf & vbCrLf
'fix
strText = strText & CStr("Customer Number: " & dr.Item("CUST_NO").ToString & " Location: " & dr.Item("CUST_LOCTN").ToString & vbCrLf & "Customer Name: " & dr.Item("CUST_NAME").ToString & vbCrLf & "Address: ")
strText = strText & CStr(loc1.ToString & " ")
strText = strText & CStr(loc2.ToString & " " & loc3.ToString) & vbCrLf & vbCrLf
'strText = strText & "This is a test String" & vbCrLf & vbCrLf
'fix
End While
dr.Close()
Catch ex As Exception
MsgBox("Error 1: " & ex.Message)
PrintPreviewDialog1.Close()
errorlevel = True
End Try
Catch ex As Exception
MsgBox("Error 2: " & ex.Message)
PrintPreviewDialog1.Close()
errorlevel = True
Finally
cmd.Dispose()
cn.Close()
cn.Dispose()
End Try
End If
'strText = "This is a test string" & vbCrLf & "This is a test string" & vbCrLf & "This is a test string" & vbCrLf & "This is a test string" & vbCrLf & "This is a test string" & vbCrLf & "This is a test string" & vbCrLf & "This is a test string" & vbCrLf & "This is a test string" & vbCrLf & "This is a test string" & vbCrLf & "This is a test string" & vbCrLf & "This is a test string" & vbCrLf & "This is a test string" & vbCrLf & "This is a test string" & vbCrLf & "This is a test string" & vbCrLf & "This is a test string" & vbCrLf & "This is a test string" & vbCrLf & "This is a test string" & vbCrLf & "This is a test string" & vbCrLf & "This is a test string" & vbCrLf & "This is a test string" & vbCrLf & "This is a test string" & vbCrLf & "This is a test string" & vbCrLf & "This is a test string" & vbCrLf & "This is a test string" & vbCrLf & "This is a test string" & vbCrLf & "This is a test string" & vbCrLf & "This is a test string" & vbCrLf & "This is a test string" & vbCrLf & "This is a test string" & vbCrLf & "This is a test string" & vbCrLf & "This is a test string" & vbCrLf & "This is a test string" & vbCrLf & "This is a test string" & vbCrLf & "This is a test string" & vbCrLf & "This is a test string" & vbCrLf & "This is a test string" & vbCrLf & "This is a test string" & vbCrLf & "This is a test string" & vbCrLf & "This is a test string" & vbCrLf & "This is a test string" & vbCrLf & "This is a test string" & vbCrLf & "This is a test string" & vbCrLf & "This is a test string" & vbCrLf & "This is a test string" & vbCrLf & "This is a test string" & vbCrLf & "This is a test string" & vbCrLf & "This is a test string" & vbCrLf & "This is a test string" & vbCrLf & "This is a test string" & vbCrLf & "This is a test string" & vbCrLf & "This is a test string" & vbCrLf & "This is a test string" & vbCrLf & "This is a test string" & vbCrLf & "This is a test string" & vbCrLf & "This is a test string" & vbCrLf & "This is a test string" & vbCrLf & "This is a test string" & vbCrLf & "This is a test string" & vbCrLf & "This is a test string" & vbCrLf & "This is a test string" & vbCrLf & "This is a test string" & vbCrLf & "This is a test string" & vbCrLf & "This is a test string" & vbCrLf & "This is a test string" & vbCrLf & "This is a test string" & vbCrLf & "This is a test string" & vbCrLf & "This is a test string" & vbCrLf & "This is a test string" & vbCrLf & "This is a test string" & vbCrLf & "This is a test string" & vbCrLf & "This is a test string" & vbCrLf & "This is a test string" & vbCrLf & "This is a test string" & vbCrLf & "This is a test string" & vbCrLf & "This is a test string" & vbCrLf & "This is a test string" & vbCrLf & "This is a test string" & vbCrLf & "This is a test string" & vbCrLf & "This is a test string" & vbCrLf & "This is a test string" & vbCrLf & "This is a test string" & vbCrLf & "This is a test string" & vbCrLf & "This is a test string" & vbCrLf & "This is a test string" & vbCrLf & "This is a test string" & vbCrLf & "This is a test string" & vbCrLf & "This is a test string" & vbCrLf & "This is a test string" & vbCrLf & "This is a test string" & vbCrLf & "This is a test string" & vbCrLf & "This is a test string" & vbCrLf & "This is a test string" & vbCrLf & "This is a test string" & vbCrLf & "This is a test string" & vbCrLf & "This is a test string" & vbCrLf & "This is a test string" & vbCrLf & "This is a test string" & vbCrLf & "This is a test string" & vbCrLf & "This is a test string" & vbCrLf & "This is a test string" & vbCrLf & "This is a test string" & vbCrLf & "This is a test string" & vbCrLf & "This is a test string" & vbCrLf & "This is a test string" & vbCrLf & "This is a test string" & vbCrLf & "This is a test string" & vbCrLf & "This is a test string" & vbCrLf & "This is a test string" & vbCrLf & "This is a test string" & vbCrLf & "This is a test string" & vbCrLf & "This is a test string" & vbCrLf & "This is a test string"
'fix
strText = cleanString(strText)
If errorlevel = False Then
Static intCurrentChar As Int32
Dim fontHeading As New Font("Times New Roman", 14, FontStyle.Bold)
Dim fontText As New Font("Times New Roman", 12, FontStyle.Regular)
Dim printAreaHeight, printAreaWidth, marginLeft, marginTop As Int32
With PrintDocument1.DefaultPageSettings
printAreaHeight = .PaperSize.Height - .Margins.Top - .Margins.Bottom
printAreaWidth = .PaperSize.Width - .Margins.Left - .Margins.Bottom
marginLeft = .Margins.Left
marginTop = .Margins.Top
End With
If PrintDocument1.DefaultPageSettings.Landscape Then
Dim intTemp As Int32
intTemp = printAreaHeight
printAreaHeight = printAreaWidth
printAreaWidth = intTemp
End If
Dim intLineCount As Int32
intLineCount = CInt(printAreaHeight / fontHeading.Height)
Dim rectPrintingArea As New RectangleF(marginLeft, marginTop + 20, printAreaWidth, printAreaHeight - 20)
Dim intLinesFilled, intCharsFitted As Int32
Dim fmt As New StringFormat
fmt.Alignment = StringAlignment.Near
fmt.FormatFlags = StringFormatFlags.LineLimit
e.Graphics.MeasureString(Mid(strHeading, intCurrentChar + 1), fontHeading, New SizeF(printAreaWidth, printAreaHeight), fmt, intCharsFitted, intLinesFilled)
e.Graphics.DrawString(strHeading, fontHeading, Brushes.Black, 300, marginTop - 50, fmt)
fmt.Alignment = StringAlignment.Near
If IsNumeric(strText.Length) = True Then
Do Until intLinesFilled >= 47
If intCurrentChar >= strText.Length Then
intLinesFilled = 48
intCurrentChar = strText.Length
Else
e.Graphics.MeasureString(Mid(strText, intCurrentChar + 1), fontText, New SizeF(printAreaWidth, printAreaHeight), fmt, intCharsFitted, intLinesFilled)
e.Graphics.DrawString(Mid(strText, intCurrentChar + 1), fontText, Brushes.Black, rectPrintingArea, fmt)
intCurrentChar = intCurrentChar + intCharsFitted
End If
Loop
End If
If intCurrentChar >= strText.Length Then
e.HasMorePages = False
Else
e.HasMorePages = True
End If
End If
TextBox1.Text = strText
End Sub
Public Shared Function cleanString(ByVal strEdit As String) As String
Dim strBuilder As New StringBuilder(strEdit)
Dim count As Integer = 0
Dim len As Integer = strBuilder.Length
Dim ch As Char
While (count < len)
ch = strBuilder.Chars(count)
Dim chasc As Integer = 0
Try
chasc = Asc(ch)
Catch ex As Exception
MsgBox("Failed on " & count & ": " & ch)
End Try
If chasc < 32 And chasc > 126 And chasc <> 10 And chasc <> 13 Then
strBuilder.Replace(ch, "@")
End If
count = count + 1
End While
Return strBuilder.ToString
End Function
End Class
This post has been edited by KamakaZ: 09 November 2010 - 07:15 PM

Help


Back to top








