I'm really a C# web developer who suddenly was assigned a VB.Net Windows forms project. The last time i did VB was the 1990's. I'm struggling with printing using the printdocument control. It works fine if my data fits on one page. But now I have a list of orders that needs to print on 2 pages. I cannot get this to work. Presently, I get 3 pages of print but it's the exact same data on each page. I don't know how to use the e.HasMorePages property. It seems like I need to add the hasMorePages = true in the loop where I have the comment 'Print Orders. But I could not get it to work. I counted and it seems I can get 39 order lines on my report. This is in addition to the my header stuff. But after the 39th order prints, I want to skip to a new page, print headings again, and then continue with the 40th order and so on. Can anyone help me figure this out? Here is my code:
VB.NET:
[/FONT][/COLOR][COLOR=#454545][FONT=Segoe UI]Public Sub OrderReport()[/FONT][/COLOR]
[COLOR=#454545][FONT=Segoe UI] Dim oOrder As New Order(connStringNW)[/FONT][/COLOR]
[COLOR=#454545][FONT=Segoe UI] ' Loop thru all rows[/FONT][/COLOR]
[COLOR=#454545][FONT=Segoe UI] ds = SqlHelper.ExecuteDataset(connStringHG, "GetOrdersByOrderDates", DateTimePicker1.Value.ToShortDateString() + " 00:00:00.000",[/FONT][/COLOR]
[COLOR=#454545][FONT=Segoe UI] DateTimePicker2.Value.ToShortDateString() + " 23:59:59.999")[/FONT][/COLOR]
[COLOR=#454545][FONT=Segoe UI] For Each row As DataRow In ds.Tables(0).Rows[/FONT][/COLOR]
[COLOR=#454545][FONT=Segoe UI] lId.Add(CStr(row("Id")))[/FONT][/COLOR]
[COLOR=#454545][FONT=Segoe UI] lName.Add(CStr(row("CustomerName")))[/FONT][/COLOR]
[COLOR=#454545][FONT=Segoe UI] lTot.Add(CStr(row("OrderTotal")))[/FONT][/COLOR]
[COLOR=#454545][FONT=Segoe UI] ' Select the printer[/FONT][/COLOR]
[COLOR=#454545][FONT=Segoe UI] Dim sDefaultPrinter As String = GetDefaultPrinterName()[/FONT][/COLOR]
[COLOR=#454545][FONT=Segoe UI] PrintDocument4.PrinterSettings.PrinterName = cboPrinter.Text[/FONT][/COLOR]
[COLOR=#454545][FONT=Segoe UI] ' PrintDocument4.PrinterSettings.[/FONT][/COLOR]
[COLOR=#454545][FONT=Segoe UI] PrintDialog4.Document = PrintDocument4[/FONT][/COLOR]
[COLOR=#454545][FONT=Segoe UI] PrintDocument4.PrinterSettings.Copies = 1[/FONT][/COLOR]
[COLOR=#454545][FONT=Segoe UI] Next[/FONT][/COLOR]
[COLOR=#454545][FONT=Segoe UI] PrintDocument4.Print()[/FONT][/COLOR]
[COLOR=#454545][FONT=Segoe UI] btnExit.Visible = True[/FONT][/COLOR]
[COLOR=#454545][FONT=Segoe UI] End Sub[/FONT][/COLOR]
[COLOR=#454545][FONT=Segoe UI]Private Sub PrintDocument4_PrintPage(ByVal sender As System.Object, ByVal e As System.Drawing.Printing.PrintPageEventArgs) Handles PrintDocument4.PrintPage[/FONT][/COLOR]
[COLOR=#454545][FONT=Segoe UI] Static loopIndex As Integer = 0[/FONT][/COLOR]
[COLOR=#454545][FONT=Segoe UI] '------------------------------------------------------[/FONT][/COLOR]
[COLOR=#454545][FONT=Segoe UI] ' Set Fonts[/FONT][/COLOR]
[COLOR=#454545][FONT=Segoe UI] '------------------------------------------------------[/FONT][/COLOR]
[COLOR=#454545][FONT=Segoe UI] printFont18B = New Font("Tahoma", 18, FontStyle.Bold)[/FONT][/COLOR]
[COLOR=#454545][FONT=Segoe UI] printFont11B = New Font("Tahoma", 11, FontStyle.Bold)[/FONT][/COLOR]
[COLOR=#454545][FONT=Segoe UI] printFont18 = New Font("Tahoma", 18)[/FONT][/COLOR]
[COLOR=#454545][FONT=Segoe UI] printFont11 = New Font("Tahoma", 11)[/FONT][/COLOR]
[COLOR=#454545][FONT=Segoe UI] iLine = 100
[/FONT][/COLOR] iPageNum += 1 If iPageNum <= iTotalPages Then
e.HasMorePages = True
Else
e.HasMorePages = False
iPageNum = 0
End If
[COLOR=#454545][FONT=Segoe UI] '------------------------------------------------------[/FONT][/COLOR]
[COLOR=#454545][FONT=Segoe UI] ' Print Header Info[/FONT][/COLOR]
[COLOR=#454545][FONT=Segoe UI] '------------------------------------------------------[/FONT][/COLOR]
[COLOR=#454545][FONT=Segoe UI] e.Graphics.DrawString("Modern Robotics Inc.", printFont11B, Brushes.Black, 20, 30)[/FONT][/COLOR]
[COLOR=#454545][FONT=Segoe UI] e.Graphics.DrawString("Orders", printFont18B, Brushes.Black, 340, 20)[/FONT][/COLOR]
[COLOR=#454545][FONT=Segoe UI] e.Graphics.DrawString("Date: ", printFont11B, Brushes.Black, 660, 30)[/FONT][/COLOR]
[COLOR=#454545][FONT=Segoe UI] e.Graphics.DrawString(Today(), printFont11B, Brushes.Black, 710, 30)[/FONT][/COLOR]
[COLOR=#454545][FONT=Segoe UI] e.Graphics.DrawString("From Start Date: ", printFont11B, Brushes.Black, 260, 65)[/FONT][/COLOR]
[COLOR=#454545][FONT=Segoe UI] e.Graphics.DrawString(DateTimePicker1.Value.ToShortDateString(), printFont11B, Brushes.Black, 400, 65)[/FONT][/COLOR]
[COLOR=#454545][FONT=Segoe UI] e.Graphics.DrawString("To: ", printFont11B, Brushes.Black, 495, 65)[/FONT][/COLOR]
[COLOR=#454545][FONT=Segoe UI] e.Graphics.DrawString(DateTimePicker2.Value.ToShortDateString(), printFont11B, Brushes.Black, 530, 65)[/FONT][/COLOR]
[COLOR=#454545][FONT=Segoe UI] e.Graphics.DrawString("Order Id", printFont11B, Brushes.Black, 100, 110)[/FONT][/COLOR]
[COLOR=#454545][FONT=Segoe UI] e.Graphics.DrawString("Customer", printFont11B, Brushes.Black, 180, 110)[/FONT][/COLOR]
[COLOR=#454545][FONT=Segoe UI] e.Graphics.DrawString("Order Total", printFont11B, Brushes.Black, 555, 110)[/FONT][/COLOR]
[COLOR=#454545][FONT=Segoe UI] iLine = 140[/FONT][/COLOR]
[COLOR=#454545][FONT=Segoe UI] ' Print Orders[/FONT][/COLOR]
[COLOR=#454545][FONT=Segoe UI] Dim j As Integer[/FONT][/COLOR]
[COLOR=#454545][FONT=Segoe UI] For j = 0 To lId.Count - 1[/FONT][/COLOR]
[COLOR=#454545][FONT=Segoe UI] e.Graphics.DrawString(lId(j).ToString(), printFont11, Brushes.Black, 100, iLine)[/FONT][/COLOR]
[COLOR=#454545][FONT=Segoe UI] e.Graphics.DrawString(lName(j).ToString(), printFont11, Brushes.Black, 180, iLine)[/FONT][/COLOR]
[COLOR=#454545][FONT=Segoe UI] e.Graphics.DrawString(Format(Convert.ToSingle(lTot(j)), "0.00"), printFont11, Brushes.Black, 555, iLine)[/FONT][/COLOR]
[COLOR=#454545][FONT=Segoe UI] iLine += 25[/FONT][/COLOR][COLOR=#454545][FONT=Segoe UI] [/FONT][/COLOR]
[COLOR=#454545][FONT=Segoe UI] Next j[/FONT][/COLOR]
[COLOR=#454545][FONT=Segoe UI] iLine = 0[/FONT][/COLOR]
[COLOR=#454545][FONT=Segoe UI] End Sub
[/FONT][/COLOR][COLOR=#454545][FONT=Segoe UI]