从Listview打印列标题

时间:2014-07-24 15:51:55

标签: vb.net winforms listview

如何添加要打印的Listview的列项?所有打印的都是物品,那我该怎么做呢?

这是我的代码:

Public Class Form9

  Private Sub Button3_Click(sender As Object, e As EventArgs) Handles Button3.Click
    Dim prn As New Printing.PrintDocument
    prn.PrinterSettings.PrinterName.ToString()
    AddHandler prn.PrintPage, AddressOf Me.PrintPageHandler
    prn.Print()
    RemoveHandler prn.PrintPage, AddressOf Me.PrintPageHandler
  End Sub

  Private Sub PrintPageHandler(sender As Object, args As PrintPageEventArgs)
    Dim lvwItem As ListViewItem
    Dim lvwSubItem As ListViewItem.ListViewSubItem
    Dim xPos As Integer = 0
    Dim yPos As Integer = 0
    Dim listviewcount As Integer = 1

    For Each lvwItem In lvPeople.Items
      xPos = 0
      For Each lvwSubItem In lvwItem.SubItems
        xPos += 100
        yPos = 100 + (listviewcount * 15)
        args.Graphics.DrawString(lvwSubItem.Text(), _
                New Font("Arial", 10, FontStyle.Regular), _
                Brushes.Black, xPos, yPos)
      Next
      listviewcount += 1
    Next
  End Sub
End Class

1 个答案:

答案 0 :(得分:0)

在循环浏览项目之前,只需浏览Columns集合:

yPos = 100
Using headerFont As New Font("Arial", 10, FontStyle.Bold)
  For Each ch As ColumnHeader In lvPeople.Columns
    xPos += 100
    args.Graphics.DrawString(ch.Text, headerFont, Brushes.Black, xPos, yPos)
  Next
End Using