CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 1 of 1
  1. #1
    Join Date
    Apr 2014
    Posts
    1

    Listview dont fint in print preview vb

    Hellou.

    I have problem with my code. The listview column heder can not fit in printpreview. Please help.
    Here is my code:
    Code:
     Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
            Dim newMargins As System.Drawing.Printing.Margins
            newMargins = New System.Drawing.Printing.Margins(20, 20, 20, 20)
    
            Dim PrintPreview As New PrintPreviewDialog
            PrintDocument1.DefaultPageSettings.Landscape = True
            PrintPreview.WindowState = FormWindowState.Maximized
            PrintPreview.PrintPreviewControl.Zoom = 1.0
            PrintPreview.Document = PrintDocument1
            PrintPreview.ShowDialog()
        End Sub
    
    
        Private Sub PrintDocument1_PrintPage(ByVal sender As Object, ByVal e As System.Drawing.Printing.PrintPageEventArgs) Handles PrintDocument1.PrintPage
           
            If ListView1.View = View.Details Then
                PrintDetails(e)
            End If
        End Sub
        Private Sub PrintDetails(ByRef e As System.Drawing.Printing.PrintPageEventArgs)
            PrintDocument1.OriginAtMargins = True
            PrintDocument1.DefaultPageSettings.Margins = New Margins(0, 0, 0, 0)
            Dim maxWidth As Integer = CInt(PrintDocument1.DefaultPageSettings.PrintableArea.Width) - 40
            Dim maxHeight As Integer = CInt(PrintDocument1.DefaultPageSettings.PrintableArea.Height) - 40 + My.Settings.zag.Length
            Static LastIndex As Integer = 0
            Static CurrentPage As Integer = 0
           
            Dim DpiGraphics As Graphics = Me.CreateGraphics
            Dim DpiX As Integer = CInt(DpiGraphics.DpiX)
            Dim DpiY As Integer = CInt(DpiGraphics.DpiY)
            DpiGraphics.Dispose()
            Dim X, Y As Integer
            Dim ImageWidth As Integer
            Dim TextRect As System.Drawing.Rectangle = System.Drawing.Rectangle.Empty
            Dim TextLeftPad As Single = CSng(4 * (DpiX / 96)) '4 pixel pad on the left.
            Dim ColumnHeaderHeight As Single = CSng(lvRec1.Font.Height + (10 * (DpiX / 96))) '5 pixel pad on the top an bottom
            Dim StringFormat As New StringFormat
            Dim PageNumberWidth As Single = e.Graphics.MeasureString(CStr(CurrentPage), lvRec1.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 lvRec1.Columns.Count - 1
                TextRect.X = X
                TextRect.Y = Y
                TextRect.Width = lvRec1.Columns(ColumnIndex).Width
                TextRect.Height = CInt(ColumnHeaderHeight)
                e.Graphics.FillRectangle(Brushes.LightGray, TextRect)
                e.Graphics.DrawRectangle(Pens.DarkGray, TextRect)
               
                TextRect.X = CInt(TextRect.X + TextLeftPad)
                TextRect.Width = CInt(TextRect.Width - TextLeftPad)
                e.Graphics.DrawString(lvRec1.Columns(ColumnIndex).Text, lvRec1.Font, Brushes.Black, TextRect, StringFormat)
               
                X = CInt(X + (TextRect.Width + TextLeftPad))
            Next
           
            Y = CInt(Y + ColumnHeaderHeight)
          
            For i = LastIndex To lvRec1.Items.Count - 1
                With lvRec1.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), lvRec1.Font, Brushes.Black, (e.PageBounds.Width - PageNumberWidth) / 2, e.PageBounds.Bottom - lvRec1.Font.Height * 2)
                        Exit Sub
                    End If
                    
                    ImageWidth = 0
                    If lvRec1.SmallImageList IsNot Nothing Then
                        
                        If Not String.IsNullOrEmpty(.ImageKey) Then
                            e.Graphics.DrawImage(lvRec1.SmallImageList.Images(.ImageKey), X, Y)
                        ElseIf .ImageIndex >= 0 Then
                            e.Graphics.DrawImage(lvRec1.SmallImageList.Images(.ImageIndex), X, Y)
                        End If
                        ImageWidth = lvRec1.SmallImageList.ImageSize.Width
                    End If
                   
                    For ColumnIndex As Integer = 0 To lvRec1.Columns.Count - 1
                        TextRect.X = X
                        TextRect.Y = Y
                        TextRect.Width = lvRec1.Columns(ColumnIndex).Width
                        TextRect.Height = .Bounds.Height
                        If lvRec1.GridLines Then
                            e.Graphics.DrawRectangle(Pens.DarkGray, TextRect)
                        End If
                       
                        If ColumnIndex = 0 Then TextRect.X += ImageWidth
    
                        TextRect.X = CInt(TextRect.X + TextLeftPad)
                        TextRect.Width = CInt(TextRect.Width - TextLeftPad)
                        If ColumnIndex < .SubItems.Count Then
                            
                            e.Graphics.DrawString(.SubItems(ColumnIndex).Text, lvRec1.Font, Brushes.Black, TextRect, StringFormat)
                        End If
                       
                        X = CInt(X + (TextRect.Width + TextLeftPad))
                    Next
    
                    Y += .Bounds.Height
                End With
            Next
    
            e.Graphics.DrawString(CStr(CurrentPage), lvRec1.Font, Brushes.Black, (e.PageBounds.Width - PageNumberWidth) / 2, e.PageBounds.Bottom - lvRec1.Font.Height * 2)
            StringFormat.Dispose()
            LastIndex = 0
            CurrentPage = 0
        End Sub
    Last edited by DataMiser; April 10th, 2014 at 09:07 AM. Reason: added code tags

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured