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

    Printing Multiple Lines VB.NET 2015

    Hello! I am new so hi, anyways, I am searching for a way to print multiple lines of vertical numbers. Here is a picture: Name:  error.jpg
Views: 1399
Size:  22.5 KB. There should be more numbers but those never printed due to size but I have no idea on how to then have it go to the top and continue. Here is my code:
    Code:
        Private Sub PrintDocument1_PrintPage(sender As Object, e As PrintPageEventArgs) Handles PrintDocument1.PrintPage
            Dim font As New Font("Times New Roman", 12, FontStyle.Regular)
            e.Graphics.DrawString(rtbDisplay.Text, font, Brushes.Black, 100, 200)
        End Sub
    Code:
            ElseIf (e.KeyCode = Keys.F3) Then
                'PrintPreviewDialog1.ShowDialog()
                PrintDialog1.ShowDialog()
                PrintDocument1.Print()
            End If

  2. #2
    Join Date
    Jul 2001
    Location
    Sunny South Africa
    Posts
    11,283

    Re: Printing Multiple Lines VB.NET 2015

    This may nudge you in the right direction:

    Code:
    ' Figure out width of each column
    Private Function FindColumnWidths(gr As Graphics, header_font As Font, body_font As Font, headers As String(), values As String(,)) As Integer()
    	
    	Dim widths As Integer() = New Integer(headers.Length - 1) {}
    
    	' Find width for each column.
    	For col As Integer = 0 To widths.Length - 1
    		
    		widths(col) = CInt(gr.MeasureString(headers(col), header_font).Width)
    
    		
    		For row As Integer = 0 To values.GetUpperBound(0)
    			Dim value_width As Integer = CInt(gr.MeasureString(values(row, col), body_font).Width)
    			If widths(col) < value_width Then
    				widths(col) = value_width
    			End If
    		Next
    
    		widths(col) += 20
    	Next
    
    	Return widths
    End Function
    Code:
        Private Sub PrintDocument1_PrintPage(sender As Object, e As PrintPageEventArgs) Handles PrintDocument1.PrintPage	
    	Using header_font As New Font("Times New Roman", 14, FontStyle.Bold)
    		Using body_font As New Font("Times New Roman", 12)
    			
    			Dim line_spacing As Integer = 20
    
    			Dim column_widths As Integer() = FindColumnWidths(e.Graphics, header_font, body_font, Headers, Data)
    
    			' Start at the left margin.
    			Dim x As Integer = e.MarginBounds.Left
    
    			' Print by columns.
    			For col As Integer = 0 To Headers.Length - 1
    				' Print header.
    				Dim y As Integer = e.MarginBounds.Top
    				e.Graphics.DrawString(Headers(col), header_font, Brushes.Blue, x, y)
    				y += CInt(line_spacing * 1.5)
    
    				' Print the items in the column.
    				For row As Integer = 0 To Data.GetUpperBound(0)
    					e.Graphics.DrawString(Data(row, col), body_font, Brushes.Black, x, y)
    					y += line_spacing
    				Next
    
    				x += column_widths(col)
    				
    			Next
    			
    		End Using
    	End Using
    	
    	e.HasMorePages = False
    End Sub

Tags for this Thread

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