Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Printing ListView control 1

Status
Not open for further replies.

dazzer123

IS-IT--Management
Nov 24, 2003
128
0
0
GB
Does anyone know how (if at all) I can print out the contents of a listview
 
Dazzr123,

Here is some code that I wrote to print the contents of a GlacialListview (which is very similar to the MS Listview - you can download the V1.5 version free from This will (currently) only print in landscape but it will give you a starting point...

Private tx As Integer
Private i As Integer
Private Y As Integer
Private titf As Font
Private tabf As Font
Private itm As Integer
Private start As Integer
Private pagenum As Integer
Private pageWidth As Integer
Private pageHeight As Integer

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

Dim sMainTitle As String
Dim iColWidth As Integer
Dim iCellH As Integer = 18
Dim iCellW As Integer
Dim blackPen As New Pen(Color.Gray, 0.5)
Dim ControlBrush As New SolidBrush(Color.FromName("Control"))
Dim imgH As Image

tx = 80
Y = PrintDocument1.DefaultPageSettings.Margins.Top - 60
sMainTitle = "Personnel Listing"
e.Graphics.DrawString(sMainTitle, titf, Brushes.Black, pageWidth / 2, Y)
e.Graphics.DrawString("P.IN.S", titf, Brushes.Black, 100, Y)
'Place icon on left...
imgH = imgMain.Images(2)
e.Graphics.DrawImage(imgH, 80, Y - 0)

Y = Y + 30
For i = 0 To grdUserDets.Columns.Count - 1
iCellW = grdUserDets.Columns(i).Width()
e.Graphics.FillRectangle(ControlBrush, tx, Y, iCellW, iCellH)
e.Graphics.DrawRectangle(blackPen, tx, Y, iCellW, iCellH)
If iCellW > 0 Then
If Not IsDBNull(grdUserDets.Columns(i).Text) Then
e.Graphics.DrawString(grdUserDets.Columns(i).Text, titf, Brushes.Black, tx, Y)
End If
End If
tx += iCellW '80
Next
Y = Y + 30
For itm = start To grdUserDets.Items.Count - 1
tx = 80
For i = 0 To grdUserDets.Columns.Count - 1
iCellW = grdUserDets.Columns(i).Width()
If iCellW > 0 Then
Dim p1 As String
Dim q1 As Decimal
Dim iIndex As Integer
Dim img As Image
Dim bImgFound As Boolean
iIndex = grdUserDets.Items(itm).SubItems(i).ImageIndex
If iIndex <> -1 Then
iIndex = grdUserDets.Items(itm).SubItems(i).ImageIndex
img = imgMain.Images(iIndex)
bImgFound = True
Else
p1 = grdUserDets.Items(itm).SubItems(i).Text
bImgFound = False
End If
q1 = tx
If bImgFound Then
e.Graphics.DrawImage(img, q1, Y)
e.Graphics.DrawRectangle(blackPen, tx, Y, iCellW, iCellH)
Else
If i = 0 Then 'This is the first column...
e.Graphics.FillRectangle(ControlBrush, tx, Y, iCellW, iCellH)
e.Graphics.DrawRectangle(blackPen, tx, Y, iCellW, iCellH)
End If
e.Graphics.DrawRectangle(blackPen, tx, Y, iCellW, iCellH)
e.Graphics.DrawString(p1, tabf, Brushes.Black, q1, Y)
End If
End If
tx += iCellW '80
Next
Y = Y + 18
With PrintDocument1.DefaultPageSettings
If Y > 720 Then
pagenum += 1
Y = Y + 5
e.Graphics.DrawString("Page " & pagenum, titf, Brushes.Black, pageWidth / 2, Y)
e.HasMorePages = True
start = itm + 1
If start > grdUserDets.Items.Count - 1 Then
Exit For
End If
Exit Sub
End If
End With
Next

pagenum += 1
Y = 725 'for portrait...1120
e.Graphics.DrawString("Page " & pagenum, titf, Brushes.Black, pageWidth / 2, Y)
e.HasMorePages = False
pagenum = 0
start = 0

End Sub
Private Sub doprint()
With PrintDocument1.DefaultPageSettings
.Landscape = True
pageWidth = .PaperSize.Height - _
.Margins.Left - _
.Margins.Right
End With

PrintDocument1.DefaultPageSettings.Margins.Left = 60
start = 0
tabf = New Font("Arial", 8)
titf = New Font("Arial", 9, FontStyle.Bold Or FontStyle.Underline)
tx = 60
pagenum = 0
End Sub

PS Sorry about the formatting...

Regards,

Alan
 
Thanks for your help, found the control and it seems to work well
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top