again printing morepages drawstring

EDN Admin

Well-known member
Joined
Aug 7, 2010
Messages
12,794
Location
In the Machine
Private index As Integer = 0
Private Sub PrintToolStripButton_Click_1(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles PrintToolStripButton.Click

Dim result As DialogResult = PrintDialog1.ShowDialog()
If (result = DialogResult.OK) Then
PrintDocument1.PrinterSettings = PrintDialog1.PrinterSettings
PrintDocument1.DefaultPageSettings.Margins.Left = 70

PrintDocument1.Print()
End If
Private Sub PrintDocument1_PrintPage(ByVal sender As Object, ByVal e As System.Drawing.Printing.PrintPageEventArgs) Handles PrintDocument1.PrintPage
keep a static index to reference if the process has more pages.
Dim biggery As Integer
Keep track of the Line Y postition to determine if there are more pages
Dim CurrentYPosition As Integer = e.MarginBounds.Top
Dim CurrentPage As Integer = 0
For i = 0 To TabControl1.TabPages.Count - 1
Create a bitmap to the size of the current tab page and draw the
current tab page to it.
Me.TabControl1.TabPages(i).Show()
If TabControl1.TabPages(i).Controls.Count > 10 Then
Dim DpiGraphics As Graphics = Me.CreateGraphics
Dim DpiX As Integer = DpiGraphics.DpiX : Dim DpiY As Integer = DpiGraphics.DpiY
DpiGraphics.Dispose()
Dim X, Y As Integer
Dim TextRect As Rectangle = Rectangle.Empty
Dim TextLeftPad As Single = CSng(1 * (DpiX / 96)) 4 pixel pad on the left.
Dim ColumnHeaderHeight As Single = CSng(TabControl1.TabPages(i).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), TabControl1.TabPages(i).Font).Width
StringFormat.FormatFlags = StringFormatFlags.NoWrap
StringFormat.Trimming = StringTrimming.EllipsisCharacter
StringFormat.LineAlignment = StringAlignment.Center
X = CInt(e.MarginBounds.X)
Y = CInt(e.MarginBounds.Y)
For ColumnIndex As Integer = index To TabControl1.TabPages(i).Controls.Count - 1
TextRect.X = TabControl1.TabPages(i).Controls(ColumnIndex).Location.X
TextRect.Y = TabControl1.TabPages(i).Controls(ColumnIndex).Location.Y + CurrentYPosition
TextRect.Width = TabControl1.TabPages(i).Controls(ColumnIndex).Width
TextRect.Height = TabControl1.TabPages(i).Controls(ColumnIndex).Height
If TabControl1.TabPages(i).Controls(ColumnIndex).ToString.Contains("TextBox") Then
Dim bColor As Integer = TabControl1.TabPages(i).Controls(ColumnIndex).BackColor.ToArgb
Dim myBColor As Color = ColorTranslator.FromHtml(bColor)
e.Graphics.FillRectangle(New SolidBrush(myBColor), TextRect)
e.Graphics.DrawRectangle(Pens.DarkGray, TextRect)
End If
Dim TColor As Integer = TabControl1.TabPages(i).Controls(ColumnIndex).ForeColor.ToArgb
Dim myTColor As Color = ColorTranslator.FromHtml(TColor)
e.Graphics.DrawString(TabControl1.TabPages(i).Controls(ColumnIndex).Text, TabControl1.TabPages(i).Controls(ColumnIndex).Font, New SolidBrush(myTColor), TextRect, StringFormat)
X += TextRect.Width + TextLeftPad
If biggery < TextRect.Y Then
biggery = TextRect.Y
End If
this continue in y in current page
If ColumnIndex = TabControl1.TabPages(i).Controls.Count - 1 Then CurrentYPosition = biggery
If CurrentYPosition > e.MarginBounds.Bottom Then
e.HasMorePages = True

the next page wont fit.
index = i + 1
e.Graphics.DrawString(CStr(CurrentPage), TabControl1.TabPages(i).Font, Brushes.Black, (e.PageBounds.Width - PageNumberWidth) / 2, e.PageBounds.Bottom - TabControl1.TabPages(i).Font.Height * 2)
CurrentPage += 1
CurrentYPosition = e.MarginBounds.Top
biggery = 0
Else
e.HasMorePages = False
End If
Next
StringFormat.Dispose()
End If
Next
All done reset the static index.
index = 0
End Sub
hi i listen john wine advice in previous thread printing many textbox
and try until is work, but i after mixing tree type snippet of printing.
printing with draw image is not pretty as drawstring and draw.rectangle
It is printig all i want but in one page.
where put static
i also try set static currentyposition but the same result
can you help me?

View the full article
 
Back
Top