hope you all fine, Im working on .net app for my small business , i want to print report for the delegate, I already done code to print list view but what i want is print the form with labels and the list view like this :
what i want to print
but my code give me this :
what i have now
and in the list view how can i make the list view right to left in the print preview.
thanks.
this is my code :
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Dim PrintPreview As New PrintPreviewDialog
PrintPreview.Document = PrintDocument1
PrintPreview.ShowDialog()
End Sub
Private Sub PrintDocument1_PrintPage(sender As Object, e As Printing.PrintPageEventArgs) Handles PrintDocument1.PrintPage
Static CurrentYPosition As Integer = 0
If ListView1.View = View.Details Then
PrintDetails(e)
End If
End Sub
Private Sub PrintDetails(ByRef e As System.Drawing.Printing.PrintPageEventArgs)
Static LastIndex As Integer = 0
Static CurrentPage As Integer = 0
Dim DpiGraphics As Graphics = Me.CreateGraphics
Dim DpiX As Integer = DpiGraphics.DpiX
Dim DpiY As Integer = DpiGraphics.DpiY
DpiGraphics.Dispose()
Dim X, Y As Integer
Dim ImageWidth As Integer
Dim TextRect As Rectangle = Rectangle.Empty
Dim TextLeftPad As Single = CSng(-4 * (DpiX / 96))
Dim ColumnHeaderHeight As Single = CSng(ListView1.Font.Height + (10 * (DpiX / 96)))
Dim StringFormat As New StringFormat
Dim PageNumberWidth As Single = e.Graphics.MeasureString(CStr(CurrentPage), ListView1.Font).Width
StringFormat.FormatFlags = StringFormatFlags.NoWrap
StringFormat.Trimming = StringTrimming.EllipsisCharacter
StringFormat.LineAlignment = StringAlignment.Center
CurrentPage += 1
X = CInt(e.MarginBounds.X)
Y = CInt(e.MarginBounds.Y)
For ColumnIndex As Integer = 0 To ListView1.Columns.Count - 1
TextRect.X = X
TextRect.Y = Y
TextRect.Width = ListView1.Columns(ColumnIndex).Width
TextRect.Height = ColumnHeaderHeight
e.Graphics.FillRectangle(Brushes.LightGray, TextRect)
e.Graphics.DrawRectangle(Pens.DarkGray, TextRect)
TextRect.X += TextLeftPad
TextRect.Width -= TextLeftPad
e.Graphics.DrawString(ListView1.Columns(ColumnIndex).Text, ListView1.Font, Brushes.Black, TextRect, StringFormat)
X += TextRect.Width + TextLeftPad
Next
Y += ColumnHeaderHeight
For i = LastIndex To ListView1.Items.Count - 1
With ListView1.Items(i)
X = CInt(e.MarginBounds.X)
If Y + .Bounds.Height > e.MarginBounds.Bottom Then
LastIndex = i - 1
e.HasMorePages = True
StringFormat.Dispose()
e.Graphics.DrawString(CStr(CurrentPage), ListView1.Font, Brushes.Black, (e.PageBounds.Width - PageNumberWidth) / 2, e.PageBounds.Bottom - ListView1.Font.Height * 2)
Exit Sub
End If
ImageWidth = 0
If ListView1.SmallImageList IsNot Nothing Then
If Not String.IsNullOrEmpty(.ImageKey) Then
e.Graphics.DrawImage(ListView1.SmallImageList.Images(.ImageKey), X, Y)
ElseIf .ImageIndex >= 0 Then
e.Graphics.DrawImage(ListView1.SmallImageList.Images(.ImageIndex), X, Y)
End If
ImageWidth = ListView1.SmallImageList.ImageSize.Width
End If
For ColumnIndex As Integer = 0 To ListView1.Columns.Count - 1
TextRect.X = X
TextRect.Y = Y
TextRect.Width = ListView1.Columns(ColumnIndex).Width
TextRect.Height = .Bounds.Height
If ListView1.GridLines Then
e.Graphics.DrawRectangle(Pens.DarkGray, TextRect)
End If
If ColumnIndex = 0 Then TextRect.X += ImageWidth
TextRect.X += TextLeftPad
TextRect.Width -= TextLeftPad
If ColumnIndex < .SubItems.Count Then
e.Graphics.DrawString(.SubItems(ColumnIndex).Text, ListView1.Font, Brushes.Black, TextRect, StringFormat)
End If
X += TextRect.Width + TextLeftPad
Next
Y += .Bounds.Height
End With
Next
e.Graphics.DrawString(CStr(CurrentPage), ListView1.Font, Brushes.Black, (e.PageBounds.Width - PageNumberWidth) / 2, e.PageBounds.Bottom - ListView1.Font.Height * 2)
StringFormat.Dispose()
LastIndex = 0
CurrentPage = 0
End Sub
What I have tried:
Code as above