Salve a tutti.
vorrei fare una stampa di una listview ma sto avendo problemi con la stampa su + pagine. non sono sicuro di aver capito benissimo l'uso di e.hasmorepages e pertanto ho molti problemi.
per esempio ho postato il codice (che potete provare incollandolo tutto in un nuovo progetto) che dovrebbe stampare l'intestazione della mia listview su più pagine ma invece di cambiare pagina mi sovrascrive sempre sulla stessa.... come mai?
codice:
Public Class Form1
Inherits System.Windows.Forms.Form
#Region " Codice generato da Progettazione Windows Form "
Public Sub New()
MyBase.New()
InitializeComponent()
End Sub
Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)
If disposing Then
If Not (components Is Nothing) Then
components.Dispose()
End If
End If
MyBase.Dispose(disposing)
End Sub
Private components As System.ComponentModel.IContainer
Friend WithEvents ListView1 As System.Windows.Forms.ListView
Friend WithEvents Button1 As System.Windows.Forms.Button
<System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
Me.ListView1 = New System.Windows.Forms.ListView()
Me.Button1 = New System.Windows.Forms.Button()
Me.SuspendLayout()
'
'ListView1
'
Me.ListView1.Anchor = (((System.Windows.Forms.AnchorStyles.Top Or System.Windows.Forms.AnchorStyles.Bottom) _
Or System.Windows.Forms.AnchorStyles.Left) _
Or System.Windows.Forms.AnchorStyles.Right)
Me.ListView1.Location = New System.Drawing.Point(16, 16)
Me.ListView1.Name = "ListView1"
Me.ListView1.Size = New System.Drawing.Size(368, 288)
Me.ListView1.TabIndex = 0
Me.ListView1.View = System.Windows.Forms.View.Details
'
'Button1
'
Me.Button1.Location = New System.Drawing.Point(328, 312)
Me.Button1.Name = "Button1"
Me.Button1.Size = New System.Drawing.Size(56, 24)
Me.Button1.TabIndex = 1
Me.Button1.Text = "print"
'
'Form1
'
Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13)
Me.ClientSize = New System.Drawing.Size(400, 342)
Me.Controls.AddRange(New System.Windows.Forms.Control() {Me.Button1, Me.ListView1})
Me.Name = "Form1"
Me.Text = "Form1"
Me.ResumeLayout(False)
End Sub
#End Region
Dim WithEvents pDoc As Printing.PrintDocument
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Dim i As Integer, x As Integer, y As Integer
Dim item As ListViewItem
For i = 1 To 10
ListView1.Columns.Add("Colonna " & Str(i), 100, HorizontalAlignment.Left)
Next
For x = 1 To 200
item = ListView1.Items.Add("Item " & Str(x))
For y = 1 To 9
item.SubItems.Add("Subitem " & Str(y))
Next
Next
End Sub
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim PrintDialog1 As New PrintDialog()
pDoc = New Printing.PrintDocument()
PrintDialog1.Document = pDoc
PrintDialog1.AllowSomePages = True
If PrintDialog1.ShowDialog() = DialogResult.OK Then
pDoc.Print()
End If
End Sub
Private Sub pDoc_PrintPage(ByVal sender As Object, ByVal e As System.Drawing.Printing.PrintPageEventArgs) Handles pDoc.PrintPage
Dim i As Integer = 0
Dim myfont As New Font("Verdana", 10, FontStyle.Regular, GraphicsUnit.Pixel)
Dim ColWidth As Integer = 0
Dim PrintRect As Rectangle = e.MarginBounds
Dim posX As Integer = PrintRect.Left
e.Graphics.DrawLine(New Pen(Color.Black), PrintRect.Left, PrintRect.Top, PrintRect.Right, PrintRect.Top)
e.Graphics.DrawLine(New Pen(Color.Black), PrintRect.Left, PrintRect.Top + myfont.Height, PrintRect.Right, PrintRect.Top + myfont.Height)
While i < ListView1.Columns.Count
ColWidth = ListView1.Columns(i).Width
e.Graphics.DrawString(ListView1.Columns(i).Text, myfont, New SolidBrush(Color.Black), posX, PrintRect.Top)
e.Graphics.DrawLine(New Pen(Color.Black), posX + ColWidth, PrintRect.Top, posX + ColWidth, PrintRect.Top + myfont.Height)
posX = posX + ColWidth
i = i + 1
If (posX > PrintRect.Right) Then
e.HasMorePages = True
posX = PrintRect.Left
End If
End While
If i = ListView1.Columns.Count Then e.HasMorePages = False
End Sub
End Class