B
bh052699
Guest
1996 was the last time I developed a VB windows form. Ive only done web development since then. So Im struggling with printing a report.
I want to print packing slips. I ensure that each order will fit on one page. I first populate a DataGridView control on a windows form. I have a "Print Packing Slips" button on the form and when clicked, will print all the orders. I also added a PrintDocument control to my form. Here is my code for the button click and PrintDocument1_PrintPage events:
Private Sub btnPrint_Click(sender As Object, e As EventArgs) Handles btnPrint.Click
Dim oOrder As New Order(connStringNW)
For Each band As DataGridViewBand In dgvOrder.Rows
iID = CInt(dgvOrder.Rows(band.Index).Cells(0).Value)
OrderDate = CDate(dgvOrder.Rows(band.Index).Cells(1).Value).ToShortDateString
ShipToName = CStr(dgvOrder.Rows(band.Index).Cells(2).Value) & " " & CStr(dgvOrder.Rows(band.Index).Cells(3).Value)
ShipToAdd1 = CStr(dgvOrder.Rows(band.Index).Cells(4).Value)
ShipToAdd2 = CStr(dgvOrder.Rows(band.Index).Cells(13).Value)
ShipToCSZ = CStr(dgvOrder.Rows(band.Index).Cells(5).Value) & ", " & CStr(dgvOrder.Rows(band.Index).Cells(6).Value) & " " & CStr(dgvOrder.Rows(band.Index).Cells(7).Value)
ShipToCountry = CStr(dgvOrder.Rows(band.Index).Cells(8).Value)
Email = CStr(dgvOrder.Rows(band.Index).Cells(10).Value)
Phone = CStr(dgvOrder.Rows(band.Index).Cells(9).Value)
AESNumber = CStr(dgvOrder.Rows(band.Index).Cells(14).Value)
ShippingMethod = CStr(dgvOrder.Rows(band.Index).Cells(15).Value)
CustomValuesXml = CStr(dgvOrder.Rows(band.Index).Cells(16).Value)
Dim sHoldCode As String = CStr(dgvOrder.Rows(band.Index).Cells(11).Value)
Dim sStatusCode As String = CStr(dgvOrder.Rows(band.Index).Cells(12).Value)
Dim dt As New DataTable()
Dim sql As String = "SELECT oi.ProductId As Item, p.Name As Description, oi.Quantity " + _
"FROM OrderItem oi INNER JOIN " + _
"Product p ON oi.ProductId = p.Id " + _
"WHERE oi.OrderId = " & iID
Dim connection As New SqlConnection(connStringNW)
Dim dataadapter As New SqlDataAdapter(sql, connStringNW)
Dim ds As New DataSet()
connection.Open()
Try
dataadapter.Fill(ds, "OrderItem")
For Each dr As DataRow In ds.Tables(0).Rows
lItem.Add(CStr(dr("Item")))
lDesc.Add(CStr(dr("Description")))
lQty.Add(CStr(dr("Quantity")))
oOrder.UpdateOrderHoldCode(iID, sHoldCode, "P")
Next
PrintDocument1.PrinterSettings.Copies = 2
Dim PrintPreview As New PrintPreviewDialog
PrintPreview.Document = PrintDocument1
PrintPreview.ShowDialog()
PrintDocument1.Print()
Catch ex As Exception
Dim sBob As String = "XXX"
Finally
connection.Close()
End Try
Next
dgvOrder.Columns.Clear()
BindGrid()
End Sub
Private Sub PrintDocument1_PrintPage(ByVal sender As System.Object, ByVal e As System.Drawing.Printing.PrintPageEventArgs) Handles PrintDocument1.PrintPage
------------------------------------------------------
Set Image Files
------------------------------------------------------
Dim img1 As Image = Image.FromFile("C:\Users\Bob\Documents\Visual Studio 2013\Projects\CompanyPortal\ShipmentProcessing\logo_left.png")
------------------------------------------------------
Set Fonts
------------------------------------------------------
printFont18B = New Font("Tahoma", 18, FontStyle.Bold)
printFont11B = New Font("Tahoma", 11, FontStyle.Bold)
printFont18 = New Font("Tahoma", 18)
printFont11 = New Font("Tahoma", 11)
------------------------------------------------------
Create Rectangles and Boxes
------------------------------------------------------
Dim R2 As Rectangle
R2 = New Rectangle(10, 350, 70, 35) E-mail Heading
Dim R3 As Rectangle
R3 = New Rectangle(230, 350, 120, 35) Phone Heading
Dim R4 As Rectangle
R4 = New Rectangle(350, 350, 135, 35) AES Heading
Dim R5 As Rectangle
R5 = New Rectangle(500, 350, 115, 35) P.O. Number Heading
Dim R6 As Rectangle
R6 = New Rectangle(615, 350, 225, 35) Ship Method Heading
Dim R7 As Rectangle
R7 = New Rectangle(10, 385, 220, 35) Email
Dim R8 As Rectangle
R8 = New Rectangle(230, 385, 120, 35) Phone
Dim R9 As Rectangle
R9 = New Rectangle(350, 385, 150, 35) AES
Dim R10 As Rectangle
R10 = New Rectangle(500, 385, 115, 35) P.O. Number
Dim R11 As Rectangle
R11 = New Rectangle(615, 385, 225, 35) Ship Method
Dim R12 As Rectangle
R12 = New Rectangle(580, 40, 120, 35) Date Heading
Dim R13 As Rectangle
R13 = New Rectangle(700, 40, 120, 35) Packing Slip Heading
Dim R14 As Rectangle
R14 = New Rectangle(580, 75, 120, 35) Date
Dim R15 As Rectangle
R15 = New Rectangle(700, 75, 120, 35) Order Id
Dim R16 As Rectangle
R16 = New Rectangle(10, 440, 150, 35) Item Code Heading
Dim R17 As Rectangle
R17 = New Rectangle(160, 440, 520, 35) Description Heading
Dim R18 As Rectangle
R18 = New Rectangle(680, 440, 160, 35) Quantity Heading
Dim R19 As Rectangle
R19 = New Rectangle(10, 475, 150, 600) Item Code Box
Dim R20 As Rectangle
R20 = New Rectangle(160, 475, 520, 600) Description Box
Dim R21 As Rectangle
R21 = New Rectangle(680, 475, 160, 600) Quantity Box
------------------------------------------------------
Print Header Info
------------------------------------------------------
e.Graphics.DrawImage(img1, 1, 1)
e.Graphics.DrawString("Dataport Systems, Inc", printFont11B, Brushes.Black, 1, 110)
e.Graphics.DrawString("www.ModernRoboticsInc", printFont11, Brushes.Black, 1, 130)
e.Graphics.DrawString("", printFont11, Brushes.Black, 1, 150)
e.Graphics.DrawString("12347 SW 132 CT", printFont11, Brushes.Black, 1, 170)
e.Graphics.DrawString("Miami, FL 33186", printFont11, Brushes.Black, 1, 190)
Print Ship To
Dim R As Rectangle
If ShipToAdd2.Length = 0 Then
R = New Rectangle(30, 225, 250, 110)
Else
R = New Rectangle(30, 225, 250, 130)
End If
Dim bp1 As New Pen(Color.Black, 1)
Dim p1 As New Point(30, 248)
Dim p2 As New Point(280, 248)
e.Graphics.DrawRectangle(Pens.Black, R)
e.Graphics.DrawString("Ship To", printFont11B, Brushes.Black, 35, 230)
e.Graphics.DrawLine(bp1, p1, p2)
e.Graphics.DrawString(ShipToName + vbCrLf, printFont11, Brushes.Black, 35, 250)
e.Graphics.DrawString(ShipToAdd1 + vbCrLf, printFont11, Brushes.Black, 35, 270)
If ShipToAdd2.Length = 0 Then
e.Graphics.DrawString(ShipToCSZ + vbCrLf, printFont11, Brushes.Black, 35, 290)
e.Graphics.DrawString(ShipToCountry + vbCrLf, printFont11, Brushes.Black, 35, 310)
Else
e.Graphics.DrawString(ShipToAdd2 + vbCrLf, printFont11, Brushes.Black, 35, 290)
e.Graphics.DrawString(ShipToCSZ + vbCrLf, printFont11, Brushes.Black, 35, 310)
e.Graphics.DrawString(ShipToCountry + vbCrLf, printFont11, Brushes.Black, 35, 330)
End If
Print Top Header Packing Slip Info
e.Graphics.DrawString("Packing Slip", printFont18B, Brushes.Black, 610, 1)
e.Graphics.DrawRectangle(Pens.Black, R12)
e.Graphics.DrawString("Date", printFont11B, Brushes.Black, 582, 50)
e.Graphics.DrawRectangle(Pens.Black, R13)
e.Graphics.DrawString("Order#", printFont11B, Brushes.Black, 702, 50)
e.Graphics.DrawRectangle(Pens.Black, R14)
e.Graphics.DrawString(OrderDate, printFont11, Brushes.Black, 582, 75)
e.Graphics.DrawRectangle(Pens.Black, R15)
e.Graphics.DrawString(iID, printFont11, Brushes.Black, 702, 75)
Print Email, Phone, AES, P.O., and Ship Method Info
e.Graphics.DrawRectangle(Pens.Black, R2)
e.Graphics.DrawString("E-mail", printFont11B, Brushes.Black, 12, 360)
e.Graphics.DrawRectangle(Pens.Black, R3)
e.Graphics.DrawString("Phone", printFont11B, Brushes.Black, 232, 360)
e.Graphics.DrawRectangle(Pens.Black, R4)
e.Graphics.DrawString("AES", printFont11B, Brushes.Black, 352, 360)
e.Graphics.DrawRectangle(Pens.Black, R5)
e.Graphics.DrawString("P.O. Number", printFont11B, Brushes.Black, 502, 360)
e.Graphics.DrawRectangle(Pens.Black, R6)
e.Graphics.DrawString("Ship Method", printFont11B, Brushes.Black, 617, 360)
e.Graphics.DrawRectangle(Pens.Black, R7)
e.Graphics.DrawString(Email, printFont11, Brushes.Black, 12, 390)
e.Graphics.DrawRectangle(Pens.Black, R8)
e.Graphics.DrawString(Phone, printFont11, Brushes.Black, 231, 390)
e.Graphics.DrawRectangle(Pens.Black, R9)
e.Graphics.DrawString(AESNumber, printFont11, Brushes.Black, 351, 390)
e.Graphics.DrawRectangle(Pens.Black, R10)
e.Graphics.DrawString(CustomValuesXml, printFont11, Brushes.Black, 501, 390)
e.Graphics.DrawRectangle(Pens.Black, R11)
e.Graphics.DrawString(ShippingMethod, printFont11, Brushes.Black, 617, 390)
------------------------------------------------------
Print Order Item Boxes and Headings
------------------------------------------------------
e.Graphics.DrawRectangle(Pens.Black, R19)
e.Graphics.DrawRectangle(Pens.Black, R20)
e.Graphics.DrawRectangle(Pens.Black, R21)
e.Graphics.DrawRectangle(Pens.Black, R16)
e.Graphics.DrawString("Item Code", printFont11B, Brushes.Black, 12, 450)
e.Graphics.DrawRectangle(Pens.Black, R17)
e.Graphics.DrawString("Description", printFont11B, Brushes.Black, 162, 450)
e.Graphics.DrawRectangle(Pens.Black, R18)
e.Graphics.DrawString("Quantity", printFont11B, Brushes.Black, 682, 450)
Print Item Details
For j = 0 To lItem.Count - 1
e.Graphics.DrawString(lItem(j).ToString(), printFont11, Brushes.Black, 12, iLine)
e.Graphics.DrawString(lDesc(j).ToString(), printFont11, Brushes.Black, 162, iLine)
e.Graphics.DrawString(lQty(j).ToString(), printFont11, Brushes.Black, 682, iLine)
iLine += 15
Next j
lItem.Clear()
lDesc.Clear()
lQty.Clear()
iPrintPageCount += 1
If iPrintPageCount <= iOrderCount Then
e.HasMorePages = True
Exit Sub
Else
e.HasMorePages = False
End If
End Sub
A few questions:
1. I think my PrintDocument1.Print() method needs to be out side my for each loop. Presently, it seems to create a new print report for each page. But not sure where to move this code. Can anyone help?
2. I tried to make it so that each page would print on a 8 1/2" by 11" letter-size paper. But the it seems to be slightly off on all 4 sides (top, bottom, left, right). Is there some property that I can set to fit on this size page or do I just have to change my positioning?
3. I changed the number of copies property = 2. But it seems to only print one copy. What am I doing wrong?
Continue reading...
I want to print packing slips. I ensure that each order will fit on one page. I first populate a DataGridView control on a windows form. I have a "Print Packing Slips" button on the form and when clicked, will print all the orders. I also added a PrintDocument control to my form. Here is my code for the button click and PrintDocument1_PrintPage events:
Private Sub btnPrint_Click(sender As Object, e As EventArgs) Handles btnPrint.Click
Dim oOrder As New Order(connStringNW)
For Each band As DataGridViewBand In dgvOrder.Rows
iID = CInt(dgvOrder.Rows(band.Index).Cells(0).Value)
OrderDate = CDate(dgvOrder.Rows(band.Index).Cells(1).Value).ToShortDateString
ShipToName = CStr(dgvOrder.Rows(band.Index).Cells(2).Value) & " " & CStr(dgvOrder.Rows(band.Index).Cells(3).Value)
ShipToAdd1 = CStr(dgvOrder.Rows(band.Index).Cells(4).Value)
ShipToAdd2 = CStr(dgvOrder.Rows(band.Index).Cells(13).Value)
ShipToCSZ = CStr(dgvOrder.Rows(band.Index).Cells(5).Value) & ", " & CStr(dgvOrder.Rows(band.Index).Cells(6).Value) & " " & CStr(dgvOrder.Rows(band.Index).Cells(7).Value)
ShipToCountry = CStr(dgvOrder.Rows(band.Index).Cells(8).Value)
Email = CStr(dgvOrder.Rows(band.Index).Cells(10).Value)
Phone = CStr(dgvOrder.Rows(band.Index).Cells(9).Value)
AESNumber = CStr(dgvOrder.Rows(band.Index).Cells(14).Value)
ShippingMethod = CStr(dgvOrder.Rows(band.Index).Cells(15).Value)
CustomValuesXml = CStr(dgvOrder.Rows(band.Index).Cells(16).Value)
Dim sHoldCode As String = CStr(dgvOrder.Rows(band.Index).Cells(11).Value)
Dim sStatusCode As String = CStr(dgvOrder.Rows(band.Index).Cells(12).Value)
Dim dt As New DataTable()
Dim sql As String = "SELECT oi.ProductId As Item, p.Name As Description, oi.Quantity " + _
"FROM OrderItem oi INNER JOIN " + _
"Product p ON oi.ProductId = p.Id " + _
"WHERE oi.OrderId = " & iID
Dim connection As New SqlConnection(connStringNW)
Dim dataadapter As New SqlDataAdapter(sql, connStringNW)
Dim ds As New DataSet()
connection.Open()
Try
dataadapter.Fill(ds, "OrderItem")
For Each dr As DataRow In ds.Tables(0).Rows
lItem.Add(CStr(dr("Item")))
lDesc.Add(CStr(dr("Description")))
lQty.Add(CStr(dr("Quantity")))
oOrder.UpdateOrderHoldCode(iID, sHoldCode, "P")
Next
PrintDocument1.PrinterSettings.Copies = 2
Dim PrintPreview As New PrintPreviewDialog
PrintPreview.Document = PrintDocument1
PrintPreview.ShowDialog()
PrintDocument1.Print()
Catch ex As Exception
Dim sBob As String = "XXX"
Finally
connection.Close()
End Try
Next
dgvOrder.Columns.Clear()
BindGrid()
End Sub
Private Sub PrintDocument1_PrintPage(ByVal sender As System.Object, ByVal e As System.Drawing.Printing.PrintPageEventArgs) Handles PrintDocument1.PrintPage
------------------------------------------------------
Set Image Files
------------------------------------------------------
Dim img1 As Image = Image.FromFile("C:\Users\Bob\Documents\Visual Studio 2013\Projects\CompanyPortal\ShipmentProcessing\logo_left.png")
------------------------------------------------------
Set Fonts
------------------------------------------------------
printFont18B = New Font("Tahoma", 18, FontStyle.Bold)
printFont11B = New Font("Tahoma", 11, FontStyle.Bold)
printFont18 = New Font("Tahoma", 18)
printFont11 = New Font("Tahoma", 11)
------------------------------------------------------
Create Rectangles and Boxes
------------------------------------------------------
Dim R2 As Rectangle
R2 = New Rectangle(10, 350, 70, 35) E-mail Heading
Dim R3 As Rectangle
R3 = New Rectangle(230, 350, 120, 35) Phone Heading
Dim R4 As Rectangle
R4 = New Rectangle(350, 350, 135, 35) AES Heading
Dim R5 As Rectangle
R5 = New Rectangle(500, 350, 115, 35) P.O. Number Heading
Dim R6 As Rectangle
R6 = New Rectangle(615, 350, 225, 35) Ship Method Heading
Dim R7 As Rectangle
R7 = New Rectangle(10, 385, 220, 35) Email
Dim R8 As Rectangle
R8 = New Rectangle(230, 385, 120, 35) Phone
Dim R9 As Rectangle
R9 = New Rectangle(350, 385, 150, 35) AES
Dim R10 As Rectangle
R10 = New Rectangle(500, 385, 115, 35) P.O. Number
Dim R11 As Rectangle
R11 = New Rectangle(615, 385, 225, 35) Ship Method
Dim R12 As Rectangle
R12 = New Rectangle(580, 40, 120, 35) Date Heading
Dim R13 As Rectangle
R13 = New Rectangle(700, 40, 120, 35) Packing Slip Heading
Dim R14 As Rectangle
R14 = New Rectangle(580, 75, 120, 35) Date
Dim R15 As Rectangle
R15 = New Rectangle(700, 75, 120, 35) Order Id
Dim R16 As Rectangle
R16 = New Rectangle(10, 440, 150, 35) Item Code Heading
Dim R17 As Rectangle
R17 = New Rectangle(160, 440, 520, 35) Description Heading
Dim R18 As Rectangle
R18 = New Rectangle(680, 440, 160, 35) Quantity Heading
Dim R19 As Rectangle
R19 = New Rectangle(10, 475, 150, 600) Item Code Box
Dim R20 As Rectangle
R20 = New Rectangle(160, 475, 520, 600) Description Box
Dim R21 As Rectangle
R21 = New Rectangle(680, 475, 160, 600) Quantity Box
------------------------------------------------------
Print Header Info
------------------------------------------------------
e.Graphics.DrawImage(img1, 1, 1)
e.Graphics.DrawString("Dataport Systems, Inc", printFont11B, Brushes.Black, 1, 110)
e.Graphics.DrawString("www.ModernRoboticsInc", printFont11, Brushes.Black, 1, 130)
e.Graphics.DrawString("", printFont11, Brushes.Black, 1, 150)
e.Graphics.DrawString("12347 SW 132 CT", printFont11, Brushes.Black, 1, 170)
e.Graphics.DrawString("Miami, FL 33186", printFont11, Brushes.Black, 1, 190)
Print Ship To
Dim R As Rectangle
If ShipToAdd2.Length = 0 Then
R = New Rectangle(30, 225, 250, 110)
Else
R = New Rectangle(30, 225, 250, 130)
End If
Dim bp1 As New Pen(Color.Black, 1)
Dim p1 As New Point(30, 248)
Dim p2 As New Point(280, 248)
e.Graphics.DrawRectangle(Pens.Black, R)
e.Graphics.DrawString("Ship To", printFont11B, Brushes.Black, 35, 230)
e.Graphics.DrawLine(bp1, p1, p2)
e.Graphics.DrawString(ShipToName + vbCrLf, printFont11, Brushes.Black, 35, 250)
e.Graphics.DrawString(ShipToAdd1 + vbCrLf, printFont11, Brushes.Black, 35, 270)
If ShipToAdd2.Length = 0 Then
e.Graphics.DrawString(ShipToCSZ + vbCrLf, printFont11, Brushes.Black, 35, 290)
e.Graphics.DrawString(ShipToCountry + vbCrLf, printFont11, Brushes.Black, 35, 310)
Else
e.Graphics.DrawString(ShipToAdd2 + vbCrLf, printFont11, Brushes.Black, 35, 290)
e.Graphics.DrawString(ShipToCSZ + vbCrLf, printFont11, Brushes.Black, 35, 310)
e.Graphics.DrawString(ShipToCountry + vbCrLf, printFont11, Brushes.Black, 35, 330)
End If
Print Top Header Packing Slip Info
e.Graphics.DrawString("Packing Slip", printFont18B, Brushes.Black, 610, 1)
e.Graphics.DrawRectangle(Pens.Black, R12)
e.Graphics.DrawString("Date", printFont11B, Brushes.Black, 582, 50)
e.Graphics.DrawRectangle(Pens.Black, R13)
e.Graphics.DrawString("Order#", printFont11B, Brushes.Black, 702, 50)
e.Graphics.DrawRectangle(Pens.Black, R14)
e.Graphics.DrawString(OrderDate, printFont11, Brushes.Black, 582, 75)
e.Graphics.DrawRectangle(Pens.Black, R15)
e.Graphics.DrawString(iID, printFont11, Brushes.Black, 702, 75)
Print Email, Phone, AES, P.O., and Ship Method Info
e.Graphics.DrawRectangle(Pens.Black, R2)
e.Graphics.DrawString("E-mail", printFont11B, Brushes.Black, 12, 360)
e.Graphics.DrawRectangle(Pens.Black, R3)
e.Graphics.DrawString("Phone", printFont11B, Brushes.Black, 232, 360)
e.Graphics.DrawRectangle(Pens.Black, R4)
e.Graphics.DrawString("AES", printFont11B, Brushes.Black, 352, 360)
e.Graphics.DrawRectangle(Pens.Black, R5)
e.Graphics.DrawString("P.O. Number", printFont11B, Brushes.Black, 502, 360)
e.Graphics.DrawRectangle(Pens.Black, R6)
e.Graphics.DrawString("Ship Method", printFont11B, Brushes.Black, 617, 360)
e.Graphics.DrawRectangle(Pens.Black, R7)
e.Graphics.DrawString(Email, printFont11, Brushes.Black, 12, 390)
e.Graphics.DrawRectangle(Pens.Black, R8)
e.Graphics.DrawString(Phone, printFont11, Brushes.Black, 231, 390)
e.Graphics.DrawRectangle(Pens.Black, R9)
e.Graphics.DrawString(AESNumber, printFont11, Brushes.Black, 351, 390)
e.Graphics.DrawRectangle(Pens.Black, R10)
e.Graphics.DrawString(CustomValuesXml, printFont11, Brushes.Black, 501, 390)
e.Graphics.DrawRectangle(Pens.Black, R11)
e.Graphics.DrawString(ShippingMethod, printFont11, Brushes.Black, 617, 390)
------------------------------------------------------
Print Order Item Boxes and Headings
------------------------------------------------------
e.Graphics.DrawRectangle(Pens.Black, R19)
e.Graphics.DrawRectangle(Pens.Black, R20)
e.Graphics.DrawRectangle(Pens.Black, R21)
e.Graphics.DrawRectangle(Pens.Black, R16)
e.Graphics.DrawString("Item Code", printFont11B, Brushes.Black, 12, 450)
e.Graphics.DrawRectangle(Pens.Black, R17)
e.Graphics.DrawString("Description", printFont11B, Brushes.Black, 162, 450)
e.Graphics.DrawRectangle(Pens.Black, R18)
e.Graphics.DrawString("Quantity", printFont11B, Brushes.Black, 682, 450)
Print Item Details
For j = 0 To lItem.Count - 1
e.Graphics.DrawString(lItem(j).ToString(), printFont11, Brushes.Black, 12, iLine)
e.Graphics.DrawString(lDesc(j).ToString(), printFont11, Brushes.Black, 162, iLine)
e.Graphics.DrawString(lQty(j).ToString(), printFont11, Brushes.Black, 682, iLine)
iLine += 15
Next j
lItem.Clear()
lDesc.Clear()
lQty.Clear()
iPrintPageCount += 1
If iPrintPageCount <= iOrderCount Then
e.HasMorePages = True
Exit Sub
Else
e.HasMorePages = False
End If
End Sub
A few questions:
1. I think my PrintDocument1.Print() method needs to be out side my for each loop. Presently, it seems to create a new print report for each page. But not sure where to move this code. Can anyone help?
2. I tried to make it so that each page would print on a 8 1/2" by 11" letter-size paper. But the it seems to be slightly off on all 4 sides (top, bottom, left, right). Is there some property that I can set to fit on this size page or do I just have to change my positioning?
3. I changed the number of copies property = 2. But it seems to only print one copy. What am I doing wrong?
Continue reading...