1

is it possible to print a form in vb.net using different listviews? For example I have 10 listviews and I want it all to be printed is a single form.

This is my code for my print document (So far it works with 2 listviews, but when I tried printing the 3rd listview, it overlaps the 2nd listview)

'Texts
    Dim time As String
    time = String.Format("{0:T}", DateTime.Now)
    Dim dateNow As String = DateString
    '   Dim totalcash As String = Convert.ToDecimal(p_cashTotal).ToString("N2")

    Dim fontTitle As New Font("Arial", 15, FontStyle.Bold)
    Dim font1 As New Font("Arial", 10, FontStyle.Bold)
    Dim font2 As New Font("Arial", 9, FontStyle.Bold)
    Dim font3 As New Font("Arial", 9, FontStyle.Regular)

    e.Graphics.DrawString("Company name", fontTitle, Brushes.Black, 270, 20)
    e.Graphics.DrawString("Report name", font1, Brushes.Black, 350, 40)
    ' e.Graphics.DrawImage(pbxLogo.Image, 160, 10)
    '   e.Graphics.DrawString("SALES REPORT - " + salesType, font2, Brushes.Black, 277, 55)
    ' e.Graphics.DrawString("Description: List of customer who wasn't able to return items", font3, Brushes.Black, 133, 80)
    e.Graphics.DrawString("Date/Time Printed:", font2, Brushes.Black, 133, 80)
    e.Graphics.DrawString(dateNow, font3, Brushes.Black, 250, 80)
    e.Graphics.DrawString(time, font3, Brushes.Black, 320, 80)
    e.Graphics.DrawString("Date/Time Printed:", font2, Brushes.Black, 133, 80)
    '   e.Graphics.DrawString("Total Earnings:", font2, Brushes.Black, 133, 95)
    '  e.Graphics.DrawString("Php " + totalcash, font3, Brushes.Black, 240, 95)

    Dim CurrRow As Integer = 0
    Dim Ratio As Single = 0
    Dim c As ColumnHeader
    Dim g As Graphics = e.Graphics
    Dim l As Integer = 120 'stores current left  
    Dim iCount As Integer
    Dim f As Font = LVLevel1.Font
    Dim FontBold As New System.Drawing.Font("Arial", 10, FontStyle.Regular)
    Dim b As Brush = Brushes.Black
    Dim currentY As Integer = 0
    Dim maxY As Integer = 0
    Dim gap As Integer = 5
    Dim lvsi As ListViewItem.ListViewSubItem
    Dim colLefts(LVLevel1.Columns.Count) As Integer
    Dim colWidths(LVLevel1.Columns.Count) As Integer
    Dim idx As Integer = 0
    Dim ii As Integer
    Dim lr As RectangleF

    'LVLevel2 
    Dim colLefts2(LVLevel2.Columns.Count) As Integer
    Dim colWidths2(LVLevel2.Columns.Count) As Integer

    'LVLevel4
    Dim colLefts3(LVLevel3.Columns.Count) As Integer
    Dim colWidths3(LVLevel3.Columns.Count) As Integer


    e.HasMorePages = False
    ' g.DrawRectangle(Pens.Black, 115, 140, 430, 30)
    Dim sfc As New StringFormat
    sfc.LineAlignment = StringAlignment.Center
    sfc.Alignment = StringAlignment.Center


    'LVLevel1
    'Headings  
    currentY = 150
    For Each c In LVLevel1.Columns
        maxY = Math.Max(maxY, g.MeasureString(c.Text, f, c.Width).Height)
        colLefts(idx) = l
        colWidths(idx) = c.Width
        lr = New RectangleF(colLefts(idx), currentY, colWidths(idx), maxY)
        If lr.Width > 0 Then g.DrawString(c.Text, FontBold, b, lr, sfc)
        l += c.Width
        idx += 1
    Next
    currentY += maxY + gap
    'g.DrawLine(Pens.Black, 0, currentY, e.PageBounds.Width, currentY)
    currentY += gap
    'Rows  
    iCount = LVLevel1.Items.Count - 1
    For ii = CurrRow To iCount
        If (currentY + maxY + maxY) > e.PageBounds.Height Then 'jump down another line to see if this line will fit  
            CurrRow = ii - 2
            e.HasMorePages = True

            currentY += maxY + gap

            Exit For 'does next page  

        End If

        l = 0
        maxY = 0
        idx = 0

        For Each lvsi In LVLevel1.Items(ii).SubItems
            maxY = Math.Max(maxY, g.MeasureString(lvsi.Text, f, colWidths(idx)).Height)

            lr = New RectangleF(colLefts(idx), currentY, colWidths(idx), maxY)

            If LVLevel1.Columns(idx).Text <> "Name" Then
                If lr.Width > 0 Then g.DrawString(lvsi.Text, f, b, lr, sfc)
            Else
                If lr.Width > 0 Then g.DrawString(lvsi.Text, f, b, lr)
            End If
            idx += 1
        Next
        currentY += maxY + gap
    Next

    'LVLEvel2
    currentY = 150
    For Each c In LVLevel2.Columns
        maxY = Math.Max(maxY, g.MeasureString(c.Text, f, c.Width).Height)
        colLefts2(idx) = l
        colWidths2(idx) = c.Width
        lr = New RectangleF(colLefts2(idx), currentY, colWidths2(idx), maxY)
        If lr.Width > 0 Then g.DrawString(c.Text, FontBold, b, lr, sfc)
        l += c.Width
        idx += 1
    Next
    currentY += maxY + gap
    'g.DrawLine(Pens.Black, 0, currentY, e.PageBounds.Width, currentY)
    currentY += gap
    'Rows  
    iCount = LVLevel2.Items.Count - 1
    For ii = CurrRow To iCount
        If (currentY + maxY + maxY) > e.PageBounds.Height Then 'jump down another line to see if this line will fit  
            CurrRow = ii - 2
            e.HasMorePages = True

            currentY += maxY + gap

            Exit For 'does next page  

        End If

        l = 0
        maxY = 0
        idx = 0

        For Each lvsi In LVLevel2.Items(ii).SubItems
            maxY = Math.Max(maxY, g.MeasureString(lvsi.Text, f, colWidths2(idx)).Height)

            lr = New RectangleF(colLefts2(idx), currentY, colWidths(idx), maxY)

            If LVLevel2.Columns(idx).Text <> "Name" Then
                If lr.Width > 0 Then g.DrawString(lvsi.Text, f, b, lr, sfc)
            Else
                If lr.Width > 0 Then g.DrawString(lvsi.Text, f, b, lr)
            End If
            idx += 1
        Next
        currentY += maxY + gap
    Next

this are the additional variables

Protected WithEvents pd As Printing.PrintDocument 'used by Print sub 
Protected Ratio As Single = 0, CurrRow As Integer = 0

this are my codes for the button PrintPreview, which opens print preview dialog

pdocDlines.DefaultPageSettings.Landscape = True
    ppreviewDlines.PrintPreviewControl.Zoom = 1
    ppreviewDlines.ShowDialog()

I just got this codes from some forums, what I want to do is to position the data from different listviews properly. Thanks

0 Answers0