printing ?

didgydont

Active member
Joined
Mar 2, 2009
Messages
37
Programming Experience
Beginner
hi all i am trying to print and have setup a print priview i would like to set paper size and make it so when it goes to a certain amount it prints on next but when i try this page count just keeps going up i have tried a few things with no luck is there a guide or does anybody know what i am doing wrong ?
VB.NET:
Private Sub PrintDocument2_PrintPage(ByVal sender As System.Object, ByVal e As System.Drawing.Printing.PrintPageEventArgs) Handles PrintDocument2.PrintPage
        Dim txt As String = ""
        Dim txt2 As String = ""
        Dim txt3 As String = ""
        '################################
        Dim myFont As New Font("Times New Roman", 30, FontStyle.Bold, GraphicsUnit.Pixel)
        Dim value As String = ""
        'value = "This is a test" & Space(25) & "And scores"
        value = My.Settings.CompanyName
        Using sf As New StringFormat()
            Dim rect As New Rectangle(3 * 15, 20, 700, 40)
            sf.Alignment = StringAlignment.Center
            'e.Graphics.DrawRectangle(Pens.Green, rect)
            e.Graphics.DrawString(value, myFont, Brushes.Black, rect, sf)
            'e.Graphics.DrawString(value, myFont, Brushes.Green)
        End Using




        For Each item As String In ListBox1.Items
            txt &= vbCrLf & item
        Next item
        For Each item2 As String In ListBox2.Items

            txt2 &= vbCrLf & FormatCurrency(item2)
        Next item2


        txt3 = txt2.Substring(vbCrLf.Length)
        txt = txt.Substring(vbCrLf.Length)
        Dim jpk
        jpk = ListBox2.Items.Count * 32
        Dim itemcounter As Integer = 0


        '###########################drawing starts here#########################3
        '#############drawing line################
        Dim blackPen As New Pen(Color.Black, 1)
        ' Create points that define line.
        Dim point1 As New Point(10, 60)
        Dim point2 As New Point(815, 60)
        ' Draw line to screen.
        e.Graphics.DrawLine(blackPen, point1, point2)
        '#############drawing line################



        Using the_font As New Font("Times New Roman", 20, FontStyle.Regular, GraphicsUnit.Point)
            ' Left aligned.
            Using sf As New StringFormat()
                Dim rect As New Rectangle(1 * 100, 100, 400, jpk)
                sf.Alignment = StringAlignment.Near
                'e.Graphics.DrawRectangle(Pens.Red, rect)
                e.Graphics.DrawString(txt, the_font, Brushes.Black, rect, sf)
            End Using

            ' Centered.
            'Using sf As New StringFormat()
            'Dim rect As New Rectangle(3 * 100, 100, 150, 250)
            'sf.Alignment = StringAlignment.Center
            'e.Graphics.DrawRectangle(Pens.Green, rect)
            'e.Graphics.DrawString(txt, the_font, Brushes.Green, _
            '   rect, sf)
            'End Using

            ' Right aligned.
            Using sf As New StringFormat()
                Dim rect As New Rectangle(5 * 100, 100, 100, jpk)
                sf.Alignment = StringAlignment.Far
                'e.Graphics.DrawRectangle(Pens.Blue, rect)
                e.Graphics.DrawString(txt3, the_font, Brushes.Black, _
                    rect, sf)
                ' itemcounter += 1
                'If itemcounter = 4 Then
                'itemcounter = 0
                ' End If
            End Using
        End Using
        'If (3 > itemcounter) Then
        'e.HasMorePages = True
        'Else
        'e.HasMorePages = False
        'End If
        '#############drawing line################
        Dim jpk2 As Integer
        jpk2 = jpk + 100
        Dim blackPen2 As New Pen(Color.Black, 1)
        ' Create points that define line.
        Dim point3 As New Point(10, jpk2)
        Dim point4 As New Point(815, jpk2)
        ' Draw line to screen.
        e.Graphics.DrawLine(blackPen2, point3, point4)
        '#############drawing line################

        '###Displays total in lable1#####
        Dim decTotalCost As Decimal
        Dim X As Integer = 1
        ' Add items from list for display 
        Dim tempstring As String
        For X = 0 To (ListBox2.Items.Count - 1)
            If Not ListBox2.Items.Item(X) = "0" Then
                tempstring = CStr(ListBox2.Items.Item(X))
                decTotalCost = decTotalCost + CDec(tempstring)

            End If
        Next
        Dim jpk3 As Integer
        jpk3 = jpk + 110
        value = "Total  " & FormatCurrency(decTotalCost)
        e.Graphics.DrawString(value, myFont, Brushes.Black, 410, jpk3)
        '#################################################
        If ListBox2.Items.Count = 5 Then
            e.HasMorePages = True
        Else
            e.HasMorePages = False

        End If





    End Sub
 
thank you
this is the code i now have is there anyway to make it smaller ?
i mean like not have to do x2 and y2 ?

VB.NET:
Private Sub PrintDocument1_PrintPage(ByVal sender As System.Object, ByVal e As System.Drawing.Printing.PrintPageEventArgs) Handles PrintDocument1.PrintPage
        intPageNumber += 1
        Dim itemsperpage As Integer = 3
        Dim itemscount As Integer = 0
        Dim itemspages As Integer = 1
        Dim txt As String = ""
        Dim txt2 As String = ""
        Dim fontsize As Integer = 30
        Dim g
        g = fontsize * 2
        Dim drawFont As New Font("Microsoft Sans Serif", fontsize, FontStyle.Regular)
        Dim drawBrush As New SolidBrush(Color.Black)

        Dim x As Single = e.MarginBounds.Left
        Dim y As Single = e.PageBounds.Top
        
        '#################draw item######################
        For Each item As String In ListBox1.Items
            itemscount += 1
            If intPageNumber = itemspages Then
                txt = item
                e.Graphics.DrawString(txt, drawFont, drawBrush, x + 5, y + g)
                y += g
            End If
            If itemscount = itemsperpage Then
                itemspages += 1
                itemscount = 0
            End If
        Next
        '################draw price#####################3
        itemspages = 1
        itemscount = 0
        Dim x2 As Single = e.MarginBounds.Left
        Dim y2 As Single = e.PageBounds.Top

        For Each item2 As String In ListBox2.Items
            itemscount += 1
            If intPageNumber = itemspages Then
                txt2 = FormatCurrency(item2)
                e.Graphics.DrawString(txt2, drawFont, drawBrush, x2 + 450, y2 + g)
                y2 += g
            End If
            If itemscount = itemsperpage Then
                itemspages += 1
                itemscount = 0
            End If
        Next
        If intPageNumber = itemspages Then
            '#############drawing line################


            Dim blackPen2 As New Pen(Color.Black, 1)
            ' Create points that define line.
            Dim point3 As New Point(10, y2 + g)
            Dim point4 As New Point(815, y2 + g)
            ' Draw line to screen.
            e.Graphics.DrawLine(blackPen2, point3, point4)
            '#############drawing line################
            Dim txt3
            txt3 = "Total:    " & Label1.Text
            e.Graphics.DrawString(txt3, drawFont, drawBrush, x2 + 300, y2 + g)
            y2 += g
        End If







        'If intPageNumber < intTotalPages Then
        If intPageNumber < itemspages Then
            e.HasMorePages = True
        Else
            e.HasMorePages = False
        End If

    End Sub
 
Last edited:
Back
Top