📄 printtableform.vb
字号:
BookItem = New ListViewItem()
BookItem.Text = "0060637633"
BookItem.SubItems.Add("Basic Writings : From Being and Time (1927 to the Task of Thinking)")
BookItem.SubItems.Add("Martin Heidegger")
BookItem.SubItems.Add("David Farrell Krell")
ListView1.Items.Add(BookItem)
BookItem = New ListViewItem()
BookItem.Text = "1566632625"
BookItem.SubItems.Add("Locke in 90 Minutes")
BookItem.SubItems.Add("Paul Strathern")
ListView1.Items.Add(BookItem)
BookItem = New ListViewItem()
BookItem.Text = "0860917045"
BookItem.SubItems.Add("Minima Moralia : Reflections from Damaged Life")
BookItem.SubItems.Add("Theodor Wiesengrund Adorno")
BookItem.SubItems.Add("E.F.Jephcott")
ListView1.Items.Add(BookItem)
BookItem = New ListViewItem()
BookItem.Text = "1890318663"
BookItem.SubItems.Add("The Defamation of Pius XII")
BookItem.SubItems.Add("Ralph M. McInerny")
BookItem.SubItems.Add("Ralph McLnerny")
ListView1.Items.Add(BookItem)
BookItem = New ListViewItem()
BookItem.Text = "0156445085"
BookItem.SubItems.Add("In Search of the Miraculous : Fragments of an Unknown Teaching ")
BookItem.SubItems.Add("P. D. Ouspensky")
ListView1.Items.Add(BookItem)
BookItem = New ListViewItem()
BookItem.Text = "0472065211"
BookItem.SubItems.Add("Simulacra and Simulation")
BookItem.SubItems.Add("Jean Baudrillard")
BookItem.SubItems.Add("Sheila Faria Glaser")
ListView1.Items.Add(BookItem)
BookItem = New ListViewItem()
BookItem.Text = "0060637633"
BookItem.SubItems.Add("Basic Writings : From Being and Time (1927 to the Task of Thinking)")
BookItem.SubItems.Add("Martin Heidegger")
BookItem.SubItems.Add("David Farrell Krell")
ListView1.Items.Add(BookItem)
BookItem = New ListViewItem()
BookItem.Text = "1566632625"
BookItem.SubItems.Add("Locke in 90 Minutes")
BookItem.SubItems.Add("Paul Strathern")
ListView1.Items.Add(BookItem)
BookItem = New ListViewItem()
BookItem.Text = "0860917045"
BookItem.SubItems.Add("Minima Moralia : Reflections from Damaged Life")
BookItem.SubItems.Add("Theodor Wiesengrund Adorno")
BookItem.SubItems.Add("E.F.Jephcott")
ListView1.Items.Add(BookItem)
BookItem = New ListViewItem()
BookItem.Text = "1890318663"
BookItem.SubItems.Add("The Defamation of Pius XII")
BookItem.SubItems.Add("Ralph M. McInerny")
BookItem.SubItems.Add("Ralph McLnerny")
ListView1.Items.Add(BookItem)
BookItem = New ListViewItem()
BookItem.Text = "0156445085"
BookItem.SubItems.Add("In Search of the Miraculous : Fragments of an Unknown Teaching ")
BookItem.SubItems.Add("P. D. Ouspensky")
ListView1.Items.Add(BookItem)
BookItem = New ListViewItem()
BookItem.Text = "0472065211"
BookItem.SubItems.Add("Simulacra and Simulation")
BookItem.SubItems.Add("Jean Baudrillard")
BookItem.SubItems.Add("Sheila Faria Glaser")
ListView1.Items.Add(BookItem)
BookItem = New ListViewItem()
BookItem.Text = "0060637633"
BookItem.SubItems.Add("Basic Writings : From Being and Time (1927 to the Task of Thinking)")
BookItem.SubItems.Add("Martin Heidegger")
BookItem.SubItems.Add("David Farrell Krell")
ListView1.Items.Add(BookItem)
BookItem = New ListViewItem()
BookItem.Text = "1566632625"
BookItem.SubItems.Add("Locke in 90 Minutes")
BookItem.SubItems.Add("Paul Strathern")
ListView1.Items.Add(BookItem)
BookItem = New ListViewItem()
BookItem.Text = "0860917045"
BookItem.SubItems.Add("Minima Moralia : Reflections from Damaged Life")
BookItem.SubItems.Add("Theodor Wiesengrund Adorno")
BookItem.SubItems.Add("E.F.Jephcott")
ListView1.Items.Add(BookItem)
BookItem = New ListViewItem()
BookItem.Text = "1890318663"
BookItem.SubItems.Add("The Defamation of Pius XII")
BookItem.SubItems.Add("Ralph M. McInerny")
BookItem.SubItems.Add("Ralph McLnerny")
ListView1.Items.Add(BookItem)
BookItem = New ListViewItem()
BookItem.Text = "0156445085"
BookItem.SubItems.Add("In Search of the Miraculous : Fragments of an Unknown Teaching ")
BookItem.SubItems.Add("P. D. Ouspensky")
ListView1.Items.Add(BookItem)
BookItem = New ListViewItem()
BookItem.Text = "0472065211"
BookItem.SubItems.Add("Simulacra and Simulation")
BookItem.SubItems.Add("Jean Baudrillard")
BookItem.SubItems.Add("Sheila Faria Glaser")
ListView1.Items.Add(BookItem)
BookItem = New ListViewItem()
BookItem.Text = "0060637633"
BookItem.SubItems.Add("Basic Writings : From Being and Time (1927 to the Task of Thinking)")
BookItem.SubItems.Add("Martin Heidegger")
BookItem.SubItems.Add("David Farrell Krell")
ListView1.Items.Add(BookItem)
BookItem = New ListViewItem()
BookItem.Text = "1566632625"
BookItem.SubItems.Add("Locke in 90 Minutes")
BookItem.SubItems.Add("Paul Strathern")
ListView1.Items.Add(BookItem)
BookItem = New ListViewItem()
BookItem.Text = "0860917045"
BookItem.SubItems.Add("Minima Moralia : Reflections from Damaged Life")
BookItem.SubItems.Add("Theodor Wiesengrund Adorno")
BookItem.SubItems.Add("E.F.Jephcott")
ListView1.Items.Add(BookItem)
BookItem = New ListViewItem()
BookItem.Text = "1890318663"
BookItem.SubItems.Add("The Defamation of Pius XII")
BookItem.SubItems.Add("Ralph M. McInerny")
BookItem.SubItems.Add("Ralph McLnerny")
ListView1.Items.Add(BookItem)
End Sub
' The following variables must be accessed from within
' multiple subroutines, as well as maintain their values
' between multiple invocations of the PrintPage event handler
Dim tableFont, TitleFont As Font
Dim X1, X2, X3 As Integer
Dim W1, W2, W3 As Integer
Dim Y As Integer
' itm is the index of the current item in the ListView control
' and it's increased by one every time another item is printed
Dim itm As Integer
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
Dim pageWidth As Integer
PageSetupDialog1.PageSettings = PrintDocument1.DefaultPageSettings
PageSetupDialog1.AllowOrientation = False
If PageSetupDialog1.ShowDialog() = DialogResult.OK Then
PrintDocument1.DefaultPageSettings = PageSetupDialog1.PageSettings
End If
tableFont = New Font("Arial", 10)
TitleFont = New Font("Arial", 12, FontStyle.Bold)
X1 = PrintDocument1.DefaultPageSettings.Margins.Left
With PrintDocument1.DefaultPageSettings
pageWidth = .PaperSize.Width - _
.Margins.Left - _
.Margins.Right
End With
' X1, X2 and X3 are the X coordinates of the left edge of the three columns
' W1, W2 and W3 are the widths of the three columns
X2 = X1 + 100
X3 = CInt(X2 + pageWidth * 0.65)
W1 = X2 - X1
W2 = X3 - X2
W3 = pageWidth - W1 - W2
PrintPreviewDialog1.Document = PrintDocument1
PrintPreviewDialog1.ShowDialog()
itm = 0
End Sub
Private Sub PrintDocument1_PrintPage(ByVal sender As Object, ByVal e As System.Drawing.Printing.PrintPageEventArgs) Handles PrintDocument1.PrintPage
Y = PrintDocument1.DefaultPageSettings.Margins.Top + 20
e.Graphics.DrawString("ISBN", TitleFont, Brushes.Black, X1, Y)
e.Graphics.DrawString("Title", TitleFont, Brushes.Black, X2, Y)
e.Graphics.DrawString("Author(s)", TitleFont, Brushes.Black, X3, Y)
Y = Y + 30
While itm < ListView1.Items.Count
Dim str As String
str = ListView1.Items(itm).Text
e.Graphics.DrawString(str, tableFont, Brushes.Black, X1, Y)
str = ListView1.Items(itm).SubItems(1).Text
Dim R As New RectangleF(X2, Y, W2, 80)
e.Graphics.DrawString(str, tableFont, Brushes.Black, R)
Dim lines, cols As Integer
e.Graphics.MeasureString(str, tableFont, New SizeF(W2, 50), New StringFormat(), cols, lines)
Dim subitm As Integer, Yc As Integer
Yc = Y
For subitm = 2 To ListView1.Items(itm).SubItems.Count - 1
str = ListView1.Items(itm).SubItems(subitm).Text
e.Graphics.DrawString(str, tableFont, Brushes.Black, X3, Yc)
Yc = Yc + tableFont.Height + 2
Next
Y = Y + lines * tableFont.Height + 5
Y = Math.Max(Y, Yc)
With PrintDocument1.DefaultPageSettings
e.Graphics.DrawLine(Pens.Black, .Margins.Left, Y, .PaperSize.Width - .Margins.Right, Y)
If Y > 0.95 * (.PaperSize.Height - .Margins.Bottom) Then
e.HasMorePages = True
Exit Sub
End If
End With
itm = itm + 1
End While
e.HasMorePages = False
End Sub
End Class
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -