How to print a ListView control?

  • Thread starter Thread starter Terry Olsen
  • Start date Start date
T

Terry Olsen

Does anyone have an example of how I could print a ListView control? It'd
sure be easier to do this than to try to format the output in DrawStrings...

Thanks.
 
I use this one, prints the collors but it dosn't work with column replaceces

in the form:

Private Sub PrintDocument1_PrintPage(ByVal sender As System.Object, ByVal e
As System.Drawing.Printing.PrintPageEventArgs) Handles
PrintDocument1.PrintPage

psPrintListView(ListView1, sender, e, , , True)

End Sub

'---------------in a mod
file----------------------------------------------------------

Module ModPrint

Public intBezig As Integer

Public Sub psPrintListView(ByVal ListViewToPrint As ListView, ByVal sender
As System.Object, ByVal e As System.Drawing.Printing.PrintPageEventArgs,
Optional ByVal Y As Integer = 0, Optional ByVal blnHeaders As Boolean =
True, Optional ByVal blnKleur As Boolean = True)

Dim nKolom As Long

Dim lvi As ListViewItem

Dim nLVI As Integer

Dim X, Y1, nLengte As Integer

Dim sTekst As String

Dim brsBorstel As Brush

Try

If e.PageSettings.Color = False Then blnKleur = False 'Als printer geen
kleur ondersteunt altijd in zwart-wit

If intBezig = 0 AndAlso blnHeaders = True Then

X = 0

For nKolom = 0 To ListViewToPrint.Columns.Count - 1

sTekst = ListViewToPrint.Columns(nKolom).Text

Do While e.Graphics.MeasureString(sTekst, ListViewToPrint.Font).Width >
ListViewToPrint.Columns(nKolom).Width

sTekst = sTekst.Remove(sTekst.Length - 1, 1)

Loop

nLengte = e.Graphics.MeasureString(sTekst, ListViewToPrint.Font).Width

Select Case ListViewToPrint.Columns(nKolom).TextAlign

Case HorizontalAlignment.Center

e.Graphics.DrawString(sTekst, ListViewToPrint.Font, Brushes.Black, X +
(ListViewToPrint.Columns(nKolom).Width - nLengte) / 2, Y)

Case HorizontalAlignment.Right

e.Graphics.DrawString(sTekst, ListViewToPrint.Font, Brushes.Black, X +
ListViewToPrint.Columns(nKolom).Width - nLengte, Y)

Case HorizontalAlignment.Left

e.Graphics.DrawString(sTekst, ListViewToPrint.Font, Brushes.Black, X, Y)

End Select

X += ListViewToPrint.Columns(nKolom).Width

Next

Y += e.Graphics.MeasureString(" ", ListViewToPrint.Font).Height 'Verhoog Y
met de hoogte van 1 lijn

End If

For nLVI = intBezig To ListViewToPrint.Items.Count - 1

lvi = ListViewToPrint.Items(nLVI)

X = 0

For nKolom = 0 To ListViewToPrint.Columns.Count - 1

sTekst = lvi.SubItems(nKolom).Text

Do While e.Graphics.MeasureString(sTekst, ListViewToPrint.Font).Width >
ListViewToPrint.Columns(nKolom).Width

sTekst = sTekst.Remove(sTekst.Length - 1, 1)

Loop

nLengte = e.Graphics.MeasureString(sTekst, ListViewToPrint.Font).Width

If blnKleur Then

brsBorstel = pfBorstel(lvi.SubItems(nKolom).ForeColor)

Else

brsBorstel = Brushes.Black

End If

Select Case ListViewToPrint.Columns(nKolom).TextAlign

Case HorizontalAlignment.Center

e.Graphics.DrawString(sTekst, ListViewToPrint.Font, brsBorstel, X +
(ListViewToPrint.Columns(nKolom).Width - nLengte) / 2, Y)

Case HorizontalAlignment.Right

e.Graphics.DrawString(sTekst, ListViewToPrint.Font, brsBorstel, X +
ListViewToPrint.Columns(nKolom).Width - nLengte, Y)

Case HorizontalAlignment.Left

e.Graphics.DrawString(sTekst, ListViewToPrint.Font, brsBorstel, X, Y)

End Select

X += ListViewToPrint.Columns(nKolom).Width

Next

Y += e.Graphics.MeasureString(" ", ListViewToPrint.Font).Height 'Verhoog Y
met de hoogte van 1 lijn

If Y > e.MarginBounds.Height Then

Y1 = Y

Y = 0 'reset Y anders niets dan lege bladen

intBezig = nLVI + 1 'Bepaal waar volgend blad begint

e.HasMorePages = True

Exit For

End If

Next

If nLVI >= ListViewToPrint.Items.Count Then

intBezig = 0

End If

X = 0

e.Graphics.DrawLine(New Pen(Color.Black), X, 0, X, Y)

For nKolom = 0 To ListViewToPrint.Columns.Count - 1

e.Graphics.DrawLine(New Pen(Color.Black), X, 0, X, Y1)

X += ListViewToPrint.Columns(nKolom).Width

Next

e.Graphics.DrawLine(New Pen(Color.Black), X, 0, X, Y1)

Catch ex As Exception

MsgBox("Foutje: " & ex.Message)

End Try

End Sub

Public Function pfBorstel(ByVal clr As Color) As Brush

Dim myBrush As SolidBrush

myBrush = New SolidBrush(clr)

Return myBrush

End Function

End Module



'end code
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top