PrintPreview is good, but prints empty - vb.net

I'm adding a printing feature to my program. In print preview it shows all the data to be printed but it prints empty on paper.
Image is provided below
1st Preview
PrintPreview
This the code in PrintDocument1:
Private Sub PrintDocument1_PrintPage(sender As Object, e As Printing.PrintPageEventArgs) Handles PrintDocument1.PrintPage
Try
If numpages = 1 Then
'e.Graphics.DrawRectangle(Pens.Red, e.MarginBounds)
e.Graphics.DrawString("Date: " & Date.Now.ToLongDateString, fon, Brushes.Black, 590, 260)
e.Graphics.DrawString("Time: " & Date.Now.ToLongTimeString, fon, Brushes.Black, 590, 285)
e.Graphics.DrawString("Republic of the Philippines", Headerfon, Brushes.Black, 295, 40)
e.Graphics.DrawString("Cebu Technologcial University", Headerfon, Brushes.Black, 270, 70)
e.Graphics.DrawString("Daanbantayan Campuse", Headerfon, Brushes.Black, 305, 100)
e.Graphics.DrawString("( SUC Level IV, AACCUP Accredited and ISO 9001 Certified )", fon2, Brushes.Black, 220, 130)
e.Graphics.DrawString("Agujo, Daanbantayan, Cebu", fon, Brushes.Black, 332, 150)
e.Graphics.DrawString("http://daanbantayan.ctu.edu.ph", fon2, Brushes.SteelBlue, 80, 190)
e.Graphics.DrawString("Email: ctudaanbantayan#yahoo.com", fon2, Brushes.SteelBlue, 560, 190)
e.Graphics.DrawString("Fax No. (032) 437-8523", fon2, Brushes.SteelBlue, 560, 210)
e.Graphics.DrawString("Telephone No. (032) 437-8526", fon2, Brushes.SteelBlue, 80, 210)
e.Graphics.DrawLine(Pens.Black, 800, 240, 55, 240)
e.Graphics.DrawLine(Pens.Black, 800, 244, 55, 244)
e.Graphics.DrawImage(PictureBox1.Image, 60.0F, 52.0F, 120.0F, 120.0F)
e.Graphics.DrawImage(PictureBox2.Image, 610.0F, 50.0F, 120.0F, 120.0F)
End If
line1:
e.Graphics.DrawImage(PictureBox3.Image, 50.0F, 1122.0F, 500.0F, 100.0F)
While PrintI <= Form12.timelog.Rows.Count - 2
Dim ColReaderSpace As Integer = 1
For Each GridCol As DataGridViewColumn In Form12.timelog.Columns
If ColHeaderY >= 1200 Then
If PrintI >= 1 Then
ColCellY = 50
ColHeaderY = 50
ColCellSpace = 52
numpages += 1
e.HasMorePages = True
e.Graphics.DrawImage(PictureBox3.Image, 50.0F, 1122.0F, 500.0F, 100.0F)
'GoTo line1
Exit Sub
End If
End If
line2:
Dim Rect As New Rectangle(x, ColHeaderY, xWidth, yHeight)
Dim strings As New StringFormat
strings.Alignment = StringAlignment.Center
strings.LineAlignment = StringAlignment.Center
e.Graphics.FillRectangle(Brushes.WhiteSmoke, Rect)
e.Graphics.DrawRectangle(Pens.Black, Rect)
e.Graphics.DrawString(GridCol.HeaderText, fon, Brushes.Black, Rect)
If ColReaderSpace = 8 Then
ColHeaderY += 20
End If
ColHeaderY += 20
ColReaderSpace += 2
Next GridCol
For k As Integer = 0 To 7
Dim Rec As New Rectangle(y, ColCellY, CellWidth, yHeight)
e.Graphics.DrawRectangle(Pens.Black, Rec)
e.Graphics.DrawString(Form12.timelog.Rows(PrintI).Cells(k).Value.ToString, fon, Brushes.Black, CellHeight, ColCellSpace)
If ColReaderSpace = 10 Then
ColCellY += 20
ColCellSpace += 20
End If
ColCellY += 20
ColCellSpace += 20
ColReaderSpace += 1
Next
e.HasMorePages = False
PrintI += 1
End While
Catch ex As Exception
MessageBox.Show(ex.Message & vbCrLf & vbCrLf & "Please Make Sure There is Data to print", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
End Try
e.Graphics.DrawImage(PictureBox3.Image, 50.0F, 1122.0F, 500.0F, 100.0F)
End Sub
This is the code in printing:
Private Sub print_Click(sender As Object, e As EventArgs) Handles print.Click
ColHeaderY = 320
ColCellY = 320
ColCellSpace = 320
PrintI = 0
numpages = 1
PrintDialog1.PrinterSettings = PrintDocument1.PrinterSettings
If PrintDialog1.ShowDialog() = Windows.Forms.DialogResult.OK Then
PrintDocument1.PrinterSettings = PrintDialog1.PrinterSettings
End If
PrintPreviewDialog1.Document = PrintDocument1
PrintPreviewDialog1.TopMost = True
PrintPreviewDialog1.ShowDialog()
End Sub
I hope that someone can help me with this error.

Related

how do i print 2 pages using vb.net without overlapping

Hi i have problem on printing output on vb.net, I run-out of ideas in my mind. I hope you can help me. Here's my code:
If ComboBox1.Text = "Sampled" Then
For i As Integer = 0 To DataGridView1.RowCount - 1
If DataGridView1.Item(0, i).Value = "True" Then
Dim ContainerNo = DataGridView1.Item(18, i).Value
Dim TotalContainer = DataGridView1.Item(19, i).Value
'e.Graphics.DrawImage(image:=myBitmap2, point:=New Point(110, 40))
e.Graphics.DrawString("SAMPLED", font3, Brushes.Black, 120, 70)
e.Graphics.DrawString("CONTAINER No:________________OF________________", font1, Brushes.Black, 90, 140)
e.Graphics.DrawString("REMARKS:_______________________________________", font1, Brushes.Black, 90, 160)
e.Graphics.DrawString("SAMPLED BY:", font1, Brushes.Black, 170, 220)
e.Graphics.DrawString("____________________", font1, Brushes.Black, 155, 235)
e.Graphics.DrawString("QC SAMPLER/DATE", font1, Brushes.Black, 160, 250)
e.Graphics.DrawString(ContainerNo, font1, Brushes.Black, 190, 138)
e.Graphics.DrawString(TotalContainer, font1, Brushes.Black, 280, 138)
e.Graphics.DrawString(TextBox12.Text, font1, Brushes.Black, 160, 158)
' e.HasMorePages = True
index += 1
If index <= x Then
e.HasMorePages = True
MessageBox.Show("True")
Else
e.HasMorePages = False
MessageBox.Show("false")
End If
End If
Next
here's my current output now
I want my output to be like this
1st page: 2 of 3 (2 is based from Dim ContainerNo = DataGridView1.Item(18, i).Value) and (3
is from Dim TotalContainer = DataGridView1.Item(19, i).Value)
2nd page: 3 of 3
Sadly my current output now is containerNo:2 and containerNo:3 are overlapping each other on both pages, I hope you can help me
Remove the loop. This function can be called multiple times in a single print job (HasMorePages controls whether it will be called again). Each time it's called you only print one page worth of data.
Dim i As Integer = index
If ComboBox1.Text = "Sampled" AndAlso DataGridView1.Item(0, i).Value = "True" Then
Dim ContainerNo = DataGridView1.Item(18, i).Value
Dim TotalContainer = DataGridView1.Item(19, i).Value
e.Graphics.DrawString("SAMPLED", font3, Brushes.Black, 120, 70)
e.Graphics.DrawString("CONTAINER No:________________OF________________", font1, Brushes.Black, 90, 140)
e.Graphics.DrawString("REMARKS:_______________________________________", font1, Brushes.Black, 90, 160)
e.Graphics.DrawString("SAMPLED BY:", font1, Brushes.Black, 170, 220)
e.Graphics.DrawString("____________________", font1, Brushes.Black, 155, 235)
e.Graphics.DrawString("QC SAMPLER/DATE", font1, Brushes.Black, 160, 250)
e.Graphics.DrawString(ContainerNo, font1, Brushes.Black, 190, 138)
e.Graphics.DrawString(TotalContainer, font1, Brushes.Black, 280, 138)
e.Graphics.DrawString(TextBox12.Text, font1, Brushes.Black, 160, 158)
End If
'Make sure index is declared OUTSIDE THE METHOD
index += 1
e.HasMorePages = index <= DataGridView1.RowCount - 1

e.graphics.hasmorepages = true doesn't print on the next page VB.NET

I am having hard time printing multiple pages on print document. It keeps printing everything on the first page even though I have new page set up in my code. Please help. Here is my code:`
Private Sub PrintDocument1_PrintPage(ByVal sender As Object, ByVal e As System.Drawing.Printing.PrintPageEventArgs) Handles PrintDocument1.PrintPage
Dim yTop As Single = e.MarginBounds.Top
Dim leftmargin = e.MarginBounds.Left
Dim MyPen As New Pen(Color.Black, 1)
Dim regFont = New Font("Arial", 10)
Dim smallFont = New Font("Arial", 8)
Dim xsFont = New Font("Arial", 6)
Dim micrFont = New Font("MICR", 10)
Dim myprintfont = New Font("Arial", 12)
Dim Bigfont = New Font("Arial", 14, FontStyle.Bold)
Dim BldFnt = New Font("Arial", 9, FontStyle.Bold)
Dim fCtr As New StringFormat
fCtr.Alignment = StringAlignment.Center
Dim frt As New StringFormat
frt.Alignment = StringAlignment.Near
Dim flft As New StringFormat
flft.Alignment = StringAlignment.Far
'Dim mrc As String = ""
Dim lpos As Integer = My.Settings.ChkLeft
Dim blackPen As New Pen(Color.Black, 1)
Dim y As Integer = 0
Dim mrc As String = ""
Dim p As Integer = 0
Dim j As Integer = 0
Dim ct As Integer = dgChecks.SelectedRows.Count
Static totaChecksPrinted As Integer
If totaChecksPrinted < dgChecks.SelectedRows.Count - 1 Then
For Each row As DataGridViewRow In dgChecks.SelectedRows
st = row.Cells("CheckNumber").Value
Dim strSQL1 As String
strSQL1 = "SELECT * FROM QryCheckPrint WHERE CheckNumber In( " & st & ")"
If HQCon.State = ConnectionState.Closed Then HQCon.Open()
Dim ccmda As New Data.SqlClient.SqlCommand(strSQL1, HQCon)
Dim drc2 As Data.SqlClient.SqlDataReader
Dim HasRows As Boolean = False
drc2 = ccmda.ExecuteReader
If drc2.HasRows Then
While drc2.Read
y += My.Settings.ChkTop
e.Graphics.DrawString(drc2.Item("CorpName"), Bigfont, Brushes.Black, lpos, y, frt)
e.Graphics.DrawString(drc2.Item("CheckNumber"), Bigfont, Brushes.Black, lpos + 640, y + 1, frt)
y += 40
e.Graphics.DrawString(drc2.Item("Date"), smallFont, Brushes.Black, lpos + 640, y + 6, frt)
e.Graphics.DrawString("Date", myprintfont, Brushes.Black, lpos + 580, y + 5, frt)
e.Graphics.DrawLine(blackPen, lpos + 620, y + 25, lpos + 730, y + 25)
y += 60
e.Graphics.DrawString("Pay to the", smallFont, Brushes.Black, lpos, y, frt)
y += 15
e.Graphics.DrawString("order of", smallFont, Brushes.Black, lpos + 10, y, frt)
e.Graphics.DrawString(drc2.Item("Name"), myprintfont, Brushes.Black, lpos + 90, y - 10, frt)
If Not IsDBNull(drc2.Item("Amount")) Then
e.Graphics.DrawString("$" & Format(drc2.Item("Amount"), "n2"), myprintfont, Brushes.Black, lpos + 640, y - 5, frt)
End If
y += 15
e.Graphics.DrawLine(blackPen, lpos + 50, y, lpos + 640, y)
e.Graphics.DrawLine(blackPen, lpos + 50, y + 30, lpos + 640, y + 30)
e.Graphics.DrawLine(blackPen, lpos + 640, y - 25, lpos + 640, y)
e.Graphics.DrawString("**" & Functions.AmountInWords(drc2.Item("Amount")) & "**", regFont, Brushes.Black, lpos + 100, y + 10, frt)
y += 40
e.Graphics.DrawString(drc2.Item("BankName"), smallFont, Brushes.Black, lpos, y, frt)
y += 15
e.Graphics.DrawString(drc2.Item("BankAddress"), smallFont, Brushes.Black, lpos, y, frt)
y += 15
e.Graphics.DrawString(drc2.Item("City") & " " & drc2.Item("State") & ", " & drc2.Item("Zip"), smallFont, Brushes.Black, lpos, y, frt)
mrc = "o" & drc2.Item("CheckNumber") & "o T" & drc2.Item("ABA") & "T" & drc2.Item("AccountNumber") & "o"
y += 30
e.Graphics.DrawString(drc2.Item("Description"), smallFont, Brushes.Black, lpos + 80, y, frt)
e.Graphics.DrawString("For", regFont, Brushes.Black, lpos + 30, y, frt)
e.Graphics.DrawString("By", regFont, Brushes.Black, lpos + 480, y, frt)
e.Graphics.DrawString(drc2.Item("StoreCode"), xsFont, Brushes.Black, lpos + 400, y, frt)
e.Graphics.DrawLine(blackPen, lpos + 30, y + 20, lpos + 440, y + 20)
e.Graphics.DrawLine(blackPen, lpos + 500, y + 20, lpos + 720, y + 20)
y = y + My.Settings.MicrPos
e.Graphics.DrawString(mrc, micrFont, Brushes.Black, lpos + 100, y, frt)
y = y + My.Settings.ChkBottom
j += 1
totaChecksPrinted += 1
End While
End If
drc2.Close()
ccmda = Nothing
If (y > e.MarginBounds.Bottom) Then 'Print new page
e.HasMorePages = True
y = 0
End If
Next
End If
If HQCon.State = ConnectionState.Open Then HQCon.Close()
End Sub`
Each time the PrintPage event is raised, you should print only the data that goes on that page. If you have more data to print, set HasMorePages to True and then Exit Sub. Then the PrintPage method will be called again for the next page. You will need to keep track of what you have already printed between pages somehow. Something like this:
'Class level variable perhaps
Dim pageNumberToPrint As Integer = 1
Private Sub PrintDocument1_PrintPage(ByVal sender As Object, ByVal e As System.Drawing.Printing.PrintPageEventArgs) Handles PrintDocument1.PrintPage
'Print the data that goes on the current page (pageNumberToPrint)
If pageNumberToPrint = 1 Then
'Print page number 1
Else
If pageNumberToPrint = 2 Then
'Print page number 2
Else
'Print page number 3
End If
End If
'If you're at the end of the page, and still have more data to print
If (y > e.MarginBounds.Bottom) Then 'Print new page
e.HasMorePages = True
y = 0
pageNumberToPrint += 1
Exit Sub 'The PrintPage event handler will be raised again
End If
End Sub

Printing forms in vb.net with many listviews

is it possible to print a form in vb.net using different listviews? For example I have 10 listviews and I want it all to be printed is a single form.
This is my code for my print document (So far it works with 2 listviews, but when I tried printing the 3rd listview, it overlaps the 2nd listview)
'Texts
Dim time As String
time = String.Format("{0:T}", DateTime.Now)
Dim dateNow As String = DateString
' Dim totalcash As String = Convert.ToDecimal(p_cashTotal).ToString("N2")
Dim fontTitle As New Font("Arial", 15, FontStyle.Bold)
Dim font1 As New Font("Arial", 10, FontStyle.Bold)
Dim font2 As New Font("Arial", 9, FontStyle.Bold)
Dim font3 As New Font("Arial", 9, FontStyle.Regular)
e.Graphics.DrawString("Company name", fontTitle, Brushes.Black, 270, 20)
e.Graphics.DrawString("Report name", font1, Brushes.Black, 350, 40)
' e.Graphics.DrawImage(pbxLogo.Image, 160, 10)
' e.Graphics.DrawString("SALES REPORT - " + salesType, font2, Brushes.Black, 277, 55)
' e.Graphics.DrawString("Description: List of customer who wasn't able to return items", font3, Brushes.Black, 133, 80)
e.Graphics.DrawString("Date/Time Printed:", font2, Brushes.Black, 133, 80)
e.Graphics.DrawString(dateNow, font3, Brushes.Black, 250, 80)
e.Graphics.DrawString(time, font3, Brushes.Black, 320, 80)
e.Graphics.DrawString("Date/Time Printed:", font2, Brushes.Black, 133, 80)
' e.Graphics.DrawString("Total Earnings:", font2, Brushes.Black, 133, 95)
' e.Graphics.DrawString("Php " + totalcash, font3, Brushes.Black, 240, 95)
Dim CurrRow As Integer = 0
Dim Ratio As Single = 0
Dim c As ColumnHeader
Dim g As Graphics = e.Graphics
Dim l As Integer = 120 'stores current left
Dim iCount As Integer
Dim f As Font = LVLevel1.Font
Dim FontBold As New System.Drawing.Font("Arial", 10, FontStyle.Regular)
Dim b As Brush = Brushes.Black
Dim currentY As Integer = 0
Dim maxY As Integer = 0
Dim gap As Integer = 5
Dim lvsi As ListViewItem.ListViewSubItem
Dim colLefts(LVLevel1.Columns.Count) As Integer
Dim colWidths(LVLevel1.Columns.Count) As Integer
Dim idx As Integer = 0
Dim ii As Integer
Dim lr As RectangleF
'LVLevel2
Dim colLefts2(LVLevel2.Columns.Count) As Integer
Dim colWidths2(LVLevel2.Columns.Count) As Integer
'LVLevel4
Dim colLefts3(LVLevel3.Columns.Count) As Integer
Dim colWidths3(LVLevel3.Columns.Count) As Integer
e.HasMorePages = False
' g.DrawRectangle(Pens.Black, 115, 140, 430, 30)
Dim sfc As New StringFormat
sfc.LineAlignment = StringAlignment.Center
sfc.Alignment = StringAlignment.Center
'LVLevel1
'Headings
currentY = 150
For Each c In LVLevel1.Columns
maxY = Math.Max(maxY, g.MeasureString(c.Text, f, c.Width).Height)
colLefts(idx) = l
colWidths(idx) = c.Width
lr = New RectangleF(colLefts(idx), currentY, colWidths(idx), maxY)
If lr.Width > 0 Then g.DrawString(c.Text, FontBold, b, lr, sfc)
l += c.Width
idx += 1
Next
currentY += maxY + gap
'g.DrawLine(Pens.Black, 0, currentY, e.PageBounds.Width, currentY)
currentY += gap
'Rows
iCount = LVLevel1.Items.Count - 1
For ii = CurrRow To iCount
If (currentY + maxY + maxY) > e.PageBounds.Height Then 'jump down another line to see if this line will fit
CurrRow = ii - 2
e.HasMorePages = True
currentY += maxY + gap
Exit For 'does next page
End If
l = 0
maxY = 0
idx = 0
For Each lvsi In LVLevel1.Items(ii).SubItems
maxY = Math.Max(maxY, g.MeasureString(lvsi.Text, f, colWidths(idx)).Height)
lr = New RectangleF(colLefts(idx), currentY, colWidths(idx), maxY)
If LVLevel1.Columns(idx).Text <> "Name" Then
If lr.Width > 0 Then g.DrawString(lvsi.Text, f, b, lr, sfc)
Else
If lr.Width > 0 Then g.DrawString(lvsi.Text, f, b, lr)
End If
idx += 1
Next
currentY += maxY + gap
Next
'LVLEvel2
currentY = 150
For Each c In LVLevel2.Columns
maxY = Math.Max(maxY, g.MeasureString(c.Text, f, c.Width).Height)
colLefts2(idx) = l
colWidths2(idx) = c.Width
lr = New RectangleF(colLefts2(idx), currentY, colWidths2(idx), maxY)
If lr.Width > 0 Then g.DrawString(c.Text, FontBold, b, lr, sfc)
l += c.Width
idx += 1
Next
currentY += maxY + gap
'g.DrawLine(Pens.Black, 0, currentY, e.PageBounds.Width, currentY)
currentY += gap
'Rows
iCount = LVLevel2.Items.Count - 1
For ii = CurrRow To iCount
If (currentY + maxY + maxY) > e.PageBounds.Height Then 'jump down another line to see if this line will fit
CurrRow = ii - 2
e.HasMorePages = True
currentY += maxY + gap
Exit For 'does next page
End If
l = 0
maxY = 0
idx = 0
For Each lvsi In LVLevel2.Items(ii).SubItems
maxY = Math.Max(maxY, g.MeasureString(lvsi.Text, f, colWidths2(idx)).Height)
lr = New RectangleF(colLefts2(idx), currentY, colWidths(idx), maxY)
If LVLevel2.Columns(idx).Text <> "Name" Then
If lr.Width > 0 Then g.DrawString(lvsi.Text, f, b, lr, sfc)
Else
If lr.Width > 0 Then g.DrawString(lvsi.Text, f, b, lr)
End If
idx += 1
Next
currentY += maxY + gap
Next
this are the additional variables
Protected WithEvents pd As Printing.PrintDocument 'used by Print sub
Protected Ratio As Single = 0, CurrRow As Integer = 0
this are my codes for the button PrintPreview, which opens print preview dialog
pdocDlines.DefaultPageSettings.Landscape = True
ppreviewDlines.PrintPreviewControl.Zoom = 1
ppreviewDlines.ShowDialog()
I just got this codes from some forums, what I want to do is to position the data from different listviews properly. Thanks

Vb.net custom tab controls

what are some good tab control components out there for free. I have found the Krypton Navigator, but it isnt free. Others don't have the properties I need. Are there any good free tab controls with these properties:
tab page:
.backgroundimage
.image(on tab)
.color(on tab)
tabcontrol:
.selectedpage
.tabshape
.tabsizing(fixed,autosize,etc.)
Yes, there are many custom tab controls, this is one (VB.NET):
Imports System.Drawing.Drawing2D
Class DotNetBarTabcontrol
Inherits TabControl
Sub New()
SetStyle(ControlStyles.AllPaintingInWmPaint Or ControlStyles.ResizeRedraw Or ControlStyles.UserPaint Or ControlStyles.DoubleBuffer, True)
DoubleBuffered = True
SizeMode = TabSizeMode.Fixed
ItemSize = New Size(44, 136)
End Sub
Protected Overrides Sub CreateHandle()
MyBase.CreateHandle()
Alignment = TabAlignment.Left
End Sub
Function ToPen(ByVal color As Color) As Pen
Return New Pen(color)
End Function
Function ToBrush(ByVal color As Color) As Brush
Return New SolidBrush(color)
End Function
Protected Overrides Sub OnPaint(ByVal e As PaintEventArgs)
Dim B As New Bitmap(Width, Height)
Dim G As Graphics = Graphics.FromImage(B)
Try : SelectedTab.BackColor = Color.White : Catch : End Try
G.Clear(Color.White)
G.FillRectangle(New SolidBrush(Color.FromArgb(246, 248, 252)), New Rectangle(0, 0, ItemSize.Height + 4, Height))
'G.DrawLine(New Pen(Color.FromArgb(170, 187, 204)), New Point(Width - 1, 0), New Point(Width - 1, Height - 1)) 'comment out to get rid of the borders
'G.DrawLine(New Pen(Color.FromArgb(170, 187, 204)), New Point(ItemSize.Height + 1, 0), New Point(Width - 1, 0)) 'comment out to get rid of the borders
'G.DrawLine(New Pen(Color.FromArgb(170, 187, 204)), New Point(ItemSize.Height + 3, Height - 1), New Point(Width - 1, Height - 1)) 'comment out to get rid of the borders
G.DrawLine(New Pen(Color.FromArgb(170, 187, 204)), New Point(ItemSize.Height + 3, 0), New Point(ItemSize.Height + 3, 999))
For i = 0 To TabCount - 1
If i = SelectedIndex Then
Dim x2 As Rectangle = New Rectangle(New Point(GetTabRect(i).Location.X - 2, GetTabRect(i).Location.Y - 2), New Size(GetTabRect(i).Width + 3, GetTabRect(i).Height - 1))
Dim myBlend As New ColorBlend()
myBlend.Colors = {Color.FromArgb(232, 232, 240), Color.FromArgb(232, 232, 240), Color.FromArgb(232, 232, 240)}
myBlend.Positions = {0.0F, 0.5F, 1.0F}
Dim lgBrush As New LinearGradientBrush(x2, Color.Black, Color.Black, 90.0F)
lgBrush.InterpolationColors = myBlend
G.FillRectangle(lgBrush, x2)
G.DrawRectangle(New Pen(Color.FromArgb(170, 187, 204)), x2)
G.SmoothingMode = SmoothingMode.HighQuality
Dim p() As Point = {New Point(ItemSize.Height - 3, GetTabRect(i).Location.Y + 20), New Point(ItemSize.Height + 4, GetTabRect(i).Location.Y + 14), New Point(ItemSize.Height + 4, GetTabRect(i).Location.Y + 27)}
G.FillPolygon(Brushes.White, p)
G.DrawPolygon(New Pen(Color.FromArgb(170, 187, 204)), p)
If ImageList IsNot Nothing Then
Try
If ImageList.Images(TabPages(i).ImageIndex) IsNot Nothing Then
G.DrawImage(ImageList.Images(TabPages(i).ImageIndex), New Point(x2.Location.X + 8, x2.Location.Y + 6))
G.DrawString(" " & TabPages(i).Text, Font, Brushes.DimGray, x2, New StringFormat With {.LineAlignment = StringAlignment.Center, .Alignment = StringAlignment.Center})
Else
G.DrawString(TabPages(i).Text, New Font(Font.FontFamily, Font.Size, FontStyle.Bold), Brushes.DimGray, x2, New StringFormat With {.LineAlignment = StringAlignment.Center, .Alignment = StringAlignment.Center})
End If
Catch ex As Exception
G.DrawString(TabPages(i).Text, New Font(Font.FontFamily, Font.Size, FontStyle.Bold), Brushes.DimGray, x2, New StringFormat With {.LineAlignment = StringAlignment.Center, .Alignment = StringAlignment.Center})
End Try
Else
G.DrawString(TabPages(i).Text, New Font(Font.FontFamily, Font.Size, FontStyle.Bold), Brushes.DimGray, x2, New StringFormat With {.LineAlignment = StringAlignment.Center, .Alignment = StringAlignment.Center})
End If
G.DrawLine(New Pen(Color.FromArgb(200, 200, 250)), New Point(x2.Location.X - 1, x2.Location.Y - 1), New Point(x2.Location.X, x2.Location.Y))
G.DrawLine(New Pen(Color.FromArgb(200, 200, 250)), New Point(x2.Location.X - 1, x2.Bottom - 1), New Point(x2.Location.X, x2.Bottom))
Else
Dim x2 As Rectangle = New Rectangle(New Point(GetTabRect(i).Location.X - 2, GetTabRect(i).Location.Y - 2), New Size(GetTabRect(i).Width + 3, GetTabRect(i).Height + 1))
G.FillRectangle(New SolidBrush(Color.FromArgb(246, 248, 252)), x2)
G.DrawLine(New Pen(Color.FromArgb(170, 187, 204)), New Point(x2.Right, x2.Top), New Point(x2.Right, x2.Bottom))
If ImageList IsNot Nothing Then
Try
If ImageList.Images(TabPages(i).ImageIndex) IsNot Nothing Then
G.DrawImage(ImageList.Images(TabPages(i).ImageIndex), New Point(x2.Location.X + 8, x2.Location.Y + 6))
G.DrawString(" " & TabPages(i).Text, Font, Brushes.DimGray, x2, New StringFormat With {.LineAlignment = StringAlignment.Center, .Alignment = StringAlignment.Center})
Else
G.DrawString(TabPages(i).Text, Font, Brushes.DimGray, x2, New StringFormat With {.LineAlignment = StringAlignment.Center, .Alignment = StringAlignment.Center})
End If
Catch ex As Exception
G.DrawString(TabPages(i).Text, Font, Brushes.DimGray, x2, New StringFormat With {.LineAlignment = StringAlignment.Center, .Alignment = StringAlignment.Center})
End Try
Else
G.DrawString(TabPages(i).Text, Font, Brushes.DimGray, x2, New StringFormat With {.LineAlignment = StringAlignment.Center, .Alignment = StringAlignment.Center})
End If
End If
Next
e.Graphics.DrawImage(B.Clone, 0, 0)
G.Dispose() : B.Dispose()
End Sub
End Class
How to use:
Create a new class then paste the code.
Save your project or debug it, then you will see the new tab page in the toolbox.
Enjoy you new awesome tab page.

Printing More than One Page with a Loop From DataGridView

I have a code am working that prints a sales report from a DataGridView. The Code prints well but can only print one page when the Value of the control Variable I goes out of the printable area. I really Need your help. Below is the code.
Dim fntAddress As New Font("Comic Sans MS", 10, FontStyle.Regular)
Dim fntHeader As New Font("Calibri", 20, FontStyle.Bold)
Dim fntBodyText As New Font("Calibri", 12, FontStyle.Regular)
Dim fntHeaderText As New Font("Calibri", 13, FontStyle.Bold)
Dim strTotalSale = txtTotal.Text
e.Graphics.DrawString("SFC POINT OF SALE AND INVENTORY MANAGEMENT", fntHeader, Brushes.Black, 100, 0)
e.Graphics.DrawString("GENERATED SALES REPORT", New Font("Calibri", 18, FontStyle.Bold), Brushes.Black, 250, 30)
Dim strDateString As String = ""
If mtbStartDate.Text = " / /" Or mtbEndDate.Text = " / /" Then
strDateString = ""
ElseIf mtbStartDate.Text = mtbEndDate.Text Then
strDateString = "Report For Date Of : " & mtbStartDate.Text
ElseIf mtbStartDate.Text <> mtbEndDate.Text Then
strDateString = "Report For Dates Of : " & mtbStartDate.Text & " - " & mtbEndDate.Text
End If
e.Graphics.DrawString(strDateString, New Font("Courier New", 15, FontStyle.Regular), Brushes.Black, 5, 70)
e.Graphics.DrawRectangle(Pens.Black, New Rectangle(5, 100, 770, 35))
e.Graphics.DrawString("Item Barcode", fntHeaderText, Brushes.Black, 10, 107)
e.Graphics.DrawString("Item Name", fntHeaderText, Brushes.Black, 160, 107)
e.Graphics.DrawString("Quantity", fntHeaderText, Brushes.Black, 360, 107)
e.Graphics.DrawString("Unit Cost", fntHeaderText, Brushes.Black, 450, 107)
e.Graphics.DrawString("Sub Total", fntHeaderText, Brushes.Black, 560, 107)
e.Graphics.DrawString("Date of Sale", fntHeaderText, Brushes.Black, 660, 107)
Dim RowCount As Integer = dgvSales.Rows.Count - 1
Static i As Integer = 139
Dim x1 = 10
Dim x2 = 700
Dim y1 = 155
Dim n As Integer = 0
While n < RowCount
e.Graphics.DrawRectangle(Pens.Black, New Rectangle(5, i - 5, 770, 35))
e.Graphics.DrawString(dgvSales.Rows(n).Cells(0).Value, fntBodyText, Brushes.Black, 16, i)
e.Graphics.DrawString(dgvSales.Rows(n).Cells(1).Value, fntBodyText, Brushes.Black, 160, i)
e.Graphics.DrawString(dgvSales.Rows(n).Cells(2).Value, fntBodyText, Brushes.Black, 360, i)
e.Graphics.DrawString(dgvSales.Rows(n).Cells(3).Value, fntBodyText, Brushes.Black, 450, i)
e.Graphics.DrawString(dgvSales.Rows(n).Cells(4).Value, fntBodyText, Brushes.Black, 560, i)
e.Graphics.DrawString(dgvSales.Rows(n).Cells(5).Value, fntBodyText, Brushes.Black, 660, i)
i = i + 35
n = n + 1
End While
e.HasMorePages = False
e.Graphics.DrawLine(Pens.Black, 150, 100, 150, i - 5)
e.Graphics.DrawLine(Pens.Black, 350, 100, 350, i - 5)
e.Graphics.DrawLine(Pens.Black, 440, 100, 440, i - 5)
e.Graphics.DrawLine(Pens.Black, 550, 100, 550, i - 5)
e.Graphics.DrawLine(Pens.Black, 650, 100, 650, i - 5)
e.Graphics.DrawRectangle(Pens.Black, New Rectangle(340, i + 40, 174, 50))
e.Graphics.DrawRectangle(Pens.Black, New Rectangle(341, i + 41, 172, 48))
e.Graphics.DrawRectangle(Pens.Black, New Rectangle(342, i + 42, 170, 46))
e.Graphics.DrawRectangle(Pens.Black, New Rectangle(343, i + 43, 168, 44))
e.Graphics.DrawString("Total Sales.", New Font("Times New Roman (Headings CS)", 22, FontStyle.Bold), Brushes.Black, 341, i + 47)
e.Graphics.DrawRectangle(Pens.Black, New Rectangle(525, i + 40, 250, 50))
e.Graphics.DrawRectangle(Pens.Black, New Rectangle(526, i + 41, 248, 48))
e.Graphics.DrawRectangle(Pens.Black, New Rectangle(527, i + 42, 246, 46))
e.Graphics.DrawRectangle(Pens.Black, New Rectangle(528, i + 43, 244, 44))
e.Graphics.DrawString(strTotalSale, New Font("Times New Roman (Headings CS)", 23, FontStyle.Bold), Brushes.Black, 538, i + 47)
Honestly, I haven't written one line of VB code in my life, but the printing mechanism at a glance seems extremely similar to the Java Printing API.
You have to mark the fact that the document you're printing has more pages yourself, using e.hasMorePages = True.
Here is a working example:
http://www.dreamincode.net/forums/topic/139447-help-with-hasmorepages-please/
Take into consideration the fact that you must always know the place you left off on the former page. Here is another, more realistic example: http://www.dreamincode.net/forums/topic/128639-ehasmorepages-and-logic-to-use-it/
Hope you understand that it could be complex because you have to calculate fontheight, papersize height, etc.
In this case, consider that there would be a fixed setting
Dim nMaxLine as Integer = 30 ' ----------> lines count per page
Dim x as Integer
Dim n as Integer
For x = 0 to datagridview.rows.count - 1
' ..
e.Graphics.DrawString( ... )
' ..
n += 1
If n > nMaxLine Then
e.hasMorePages = True
n = 0
End If
Next