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
"Ken Tucker [MVP]" <vb***@bellsouth.net> schreef in bericht
news:OA*************@TK2MSFTNGP15.phx.gbl...
Hi,
Sorry I dont have a code sample but graphics class has a drawstring
overload with accepts a rectanglef and stringformat. You can use that to
align the data properly on the page.
http://msdn.microsoft.com/library/de...ringtopic4.asp
Ken
--------------------
"Terry Olsen" <to******@hotmail.com> wrote in message
news:%2***************@TK2MSFTNGP09.phx.gbl... 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.