Asked by:
How to make page break in printing more than one page

Question
-
Hi All,
How to make page break in printing more than one page.
Kind Regards,
Hany Metry
- Moved by Xingyu ZhaoMicrosoft contingent staff Thursday, September 24, 2020 7:36 AM same thread
Monday, September 14, 2020 7:27 PM
All replies
-
Hi
I will give a suggestion just as curt as your question!
See here: https://docs.microsoft.com/en-us/dotnet/desktop/winforms/advanced/how-to-print-a-multi-page-text-file-in-windows-forms?view=netframeworkdesktop-4.8&redirectedfrom=MSDN
*
BTW, you asked and marked an answer to same question a few years ago - is your memory letting you down? https://social.msdn.microsoft.com/Forums/vstudio/en-US/78774521-3a28-4504-8320-5e6d7b26fd91/is-there-is-any-method-to-insert-page-break-at-any-location-during-printing-rich-text-box?forum=vbgeneral
Regards Les, Livingston, Scotland
Monday, September 14, 2020 8:04 PM -
Hi
I will give a suggestion just as curt as your question!
See here: https://docs.microsoft.com/en-us/dotnet/desktop/winforms/advanced/how-to-print-a-multi-page-text-file-in-windows-forms?view=netframeworkdesktop-4.8&redirectedfrom=MSDN
*
BTW, you asked and marked an answer to same question a few years ago - is your memory letting you down? https://social.msdn.microsoft.com/Forums/vstudio/en-US/78774521-3a28-4504-8320-5e6d7b26fd91/is-there-is-any-method-to-insert-page-break-at-any-location-during-printing-rich-text-box?forum=vbgeneral
Regards Les, Livingston, Scotland
Hi,
That is not a text while it is e.Graphics.DrawString format.
Kind Regards,
Hany Metry
Monday, September 14, 2020 8:41 PM -
Hi,
What? Is not all the printing done via an e.Graphics.DrawString?That is not a text while it is e.Graphics.DrawString format.
Kind Regards,
Hany Metry
Regards Les, Livingston, Scotland
Monday, September 14, 2020 8:44 PM -
Hi,
That is not a text while it is e.Graphics.DrawString format.
Kind Regards,
Hany Metry
What? Is not all the printing done via an e.Graphics.DrawString?
Regards Les, Livingston, Scotland
I think the question here is about actual printing, not just drawing.
Imports System.Collections.Generic Imports System.Drawing.Printing Public Class Form1 Dim pages As List(Of String) = New List(Of String)() Dim pageCount As Integer = 0 Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load For i As Int16 = 0 To 5 Step +1 pages.Add("This is page " + i.ToString()) Next Dim pd As PrintDocument = New PrintDocument() AddHandler pd.PrintPage, AddressOf printAPage pd.Print() End Sub Private Sub printAPage(ByVal sender As Object, ByVal e As PrintPageEventArgs) Dim b As Brush If (pageCount Mod 2 = 0) Then b = New SolidBrush(Color.Black) Else b = New SolidBrush(Color.Red) End If e.Graphics.DrawString(pages(pageCount), Me.Font, b, 0, 0) If pageCount < (pages.Count - 1) Then e.HasMorePages = True Else e.HasMorePages = False End If b.Dispose() pageCount += 1 End Sub Private Sub Form1_FormClosing(ByVal sender As System.Object, ByVal e As System.Windows.Forms.FormClosingEventArgs) Handles MyBase.FormClosing End Sub End Class
Before you can learn anything new you have to learn that there's stuff you don't know.
Monday, September 14, 2020 10:04 PM -
Or much more simply:
Imports System.Collections.Generic
Imports System.Drawing.Printing
Public Class Form1
Dim pages As Queue(Of String) = New Queue(Of String)()
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
For i As Int16 = 0 To 5 Step +1
pages.Enqueue("This is page " + i.ToString())
Next
Dim pd As PrintDocument = New PrintDocument()
AddHandler pd.PrintPage, AddressOf printAPage
pd.Print()
End Sub
Private Sub printAPage(ByVal sender As Object, ByVal e As PrintPageEventArgs)
Dim b As Brush = New SolidBrush(Color.Black)
e.Graphics.DrawString(pages.Dequeue, Me.Font, b, 0, 0)
e.HasMorePages = (pages.Count > 0)
b.Dispose()
End Sub
Private Sub Form1_FormClosing(ByVal sender As System.Object, ByVal e As System.Windows.Forms.FormClosingEventArgs) Handles MyBase.FormClosing
End Sub
End Class
Before you can learn anything new you have to learn that there's stuff you don't know.
- Edited by Andrew B. Painter Monday, September 14, 2020 10:12 PM
Monday, September 14, 2020 10:11 PM -
Hi Hany Metry,
I make a test based the document provided by leshay, and you can use PrintPreviewDialog1 control to preview multiple pages that need to be printed.
Here's the code you can refer to.
Private strToPrint As String ' Select a text file. Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click OpenFileDialog1.Title = "Open Text File" OpenFileDialog1.Filter = "txt files (*.txt)|*.txt|All files (*.*)|*.*" OpenFileDialog1.InitialDirectory = Environment.SpecialFolder.MyDocuments If OpenFileDialog1.ShowDialog() = System.Windows.Forms.DialogResult.OK Then TextBox1.Text = OpenFileDialog1.FileName End If End Sub ' Print. Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click PrintDocument1.DocumentName = TextBox1.Text Dim stream As New FileStream(PrintDocument1.DocumentName, FileMode.Open) Try Dim reader As New StreamReader(stream) Try strToPrint = reader.ReadToEnd() Finally reader.Dispose() End Try Finally stream.Dispose() End Try Dim pd As PrintDialog = New PrintDialog() pd.PrinterSettings.PrinterName = "Your printer name" If pd.ShowDialog() = System.Windows.Forms.DialogResult.OK Then PrintDocument1.Print() End If End Sub Private Sub PrintDocument1_PrintPage(sender As Object, e As Printing.PrintPageEventArgs) Handles PrintDocument1.PrintPage Dim charCount As Integer = 0 Dim lineCount As Integer = 0 ' Measure the string 'strToPrint' e.Graphics.MeasureString(strToPrint, Me.Font, e.MarginBounds.Size, StringFormat.GenericTypographic, charCount, lineCount) e.Graphics.DrawString(strToPrint, Me.Font, Brushes.Black, e.MarginBounds, StringFormat.GenericTypographic) 'Remove that part of the string that has been printed. strToPrint = strToPrint.Substring(charCount) ' Check if any more pages left for printing If strToPrint.Length > 0 Then e.HasMorePages = True Else e.HasMorePages = False End If End Sub
Hope it could be helpful.
Besides, if you need further assistance, please provide more details.
Best Regards,
Xingyu Zhao
MSDN Community Support
Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.- Edited by Xingyu ZhaoMicrosoft contingent staff Tuesday, September 15, 2020 9:29 AM
Tuesday, September 15, 2020 2:30 AM -
I have a problem with print preview dialog as per attached photo.
The printer is not connected. Is the problem because the printer is not connected? the code as below.
Friend Sub PrintPage1ToolStripMenuItem_Click(sender As Object, e As EventArgs) Handles PrintPage1ToolStripMenuItem.Click PrintDocument1.DefaultPageSettings.Landscape = False AddHandler PrintDocument1.PrintPage, AddressOf PDoc1_PrintPage PrintPreviewDialog1.ShowDialog() 'PrintDocument1.Print() End Sub
Hany Metry
Tuesday, September 15, 2020 7:26 AM -
Hi Hany Metry,
You may need 'PrintDialog' to select the printer.
Dim pd As PrintDialog = New PrintDialog() pd.PrinterSettings.PrinterName = "Your printer name" If pd.ShowDialog() = System.Windows.Forms.DialogResult.OK Then PrintDocument1.Print() End If
You can see all the printers in "Settings -> Devices -> Printers & scanners" in Windows 10, and make sure that one of the printers has been set as the default printer.
Best Regards,
Xingyu Zhao
MSDN Community Support
Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.- Edited by Xingyu ZhaoMicrosoft contingent staff Wednesday, September 16, 2020 1:22 AM
Tuesday, September 15, 2020 9:28 AM -
Hi Xingyu
I want to select printer during printing also, I think there is no OK button for print preview dialog
Friend Sub PrintPage1ToolStripMenuItem_Click(sender As Object, e As EventArgs) Handles PrintPage1ToolStripMenuItem.Click
PrintDocument1.DefaultPageSettings.Landscape = False
AddHandler PrintDocument1.PrintPage, AddressOf PDoc1_PrintPage
If PrintPreviewDialog1.ShowDialog = DialogResult.OK Then
If PrintDialog1.ShowDialog = DialogResult.OK Then
PrintDocument1.Print()
End If
End If
End SubBut even I wrote the code above but I think there is no OK button for print preview.
I have not a default printer
Kind Regards,
Hany Metry
- Edited by Morgan Mosa Tuesday, September 15, 2020 10:29 AM adjust
Tuesday, September 15, 2020 10:05 AM -
Hi Xingyu
I want to select printer during printing also, I think there is no OK button for print preview dialog
Friend Sub PrintPage1ToolStripMenuItem_Click(sender As Object, e As EventArgs) Handles PrintPage1ToolStripMenuItem.Click
PrintDocument1.DefaultPageSettings.Landscape = False
AddHandler PrintDocument1.PrintPage, AddressOf PDoc1_PrintPage
If PrintPreviewDialog1.ShowDialog = DialogResult.OK Then
If PrintDialog1.ShowDialog = DialogResult.OK Then
PrintDocument1.Print()
End If
End If
End SubBut even I wrote the code above but I think there is no OK button for print preview.
I have not a default printer
Kind Regards,
Hany Metry
Hi All,
If any one test the above code, if it work or not? because I need to purchase printer and that may take few days.
Kind Regards,
Hany Metry
Tuesday, September 15, 2020 10:41 AM -
Hi
Wait just a minute! Do you already have a printer? If so, please give details make/model, year purchased, type (dot matrix, thermal etc) and year of purchase - it may not be necessary to buy a new one.
Regards Les, Livingston, Scotland
Tuesday, September 15, 2020 3:19 PM -
Hi
Wait just a minute! Do you already have a printer? If so, please give details make/model, year purchased, type (dot matrix, thermal etc) and year of purchase - it may not be necessary to buy a new one.
Regards Les, Livingston, Scotland
Before you can learn anything new you have to learn that there's stuff you don't know.
Tuesday, September 15, 2020 4:24 PM -
Hi
Wait just a minute! Do you already have a printer? If so, please give details make/model, year purchased, type (dot matrix, thermal etc) and year of purchase - it may not be necessary to buy a new one.
Regards Les, Livingston, Scotland
I was have a laser printer purchased from 2011 or 2010 and it failed from two years and I was using my brother printer from two years and now my brother printer failed also and I need to purchase new one maybe tomorrow.
Hany Metry
Tuesday, September 15, 2020 5:19 PM -
Hi,
When I visited the computer mall yesterday to buy a printer, they told me, that your printer can be repaired, so I postpone to purchase new printer until try to repair my printer next Monday.
Kind Regards,
Hany Metry
Thursday, September 17, 2020 7:45 AM -
Thanks for updating MSDN on your printer status, Hany Metry. I overate a little yesterday due to stress.
Before you can learn anything new you have to learn that there's stuff you don't know.
Thursday, September 17, 2020 12:55 PM -
Even the printer is connected and the test page is printed but still I have the above problem
Hany Metry
- Edited by Morgan Mosa Thursday, September 17, 2020 8:47 PM
Thursday, September 17, 2020 8:46 PM -
The connected printer is bold and there is no green arrow as before (I use Window 7)
Hany Metry
- Edited by Morgan Mosa Thursday, September 17, 2020 8:51 PM
Thursday, September 17, 2020 8:49 PM -
Hi All,
When I try to make the printer as default printer to have green arrow, I find the following error
Hany Metry
Thursday, September 17, 2020 8:57 PM -
Hi All,
As per the previous three posts, I will tell you the history.
For other program, I installed dot net frame work 4.7.2 and when I find there is a problem of printing, I remove dot net frame work 4.7.2 and after remove dot net frame work 4.7.2, I find there is a problem in the program, I reinstalled dot net frame work 4.0 and dot net frame work 4.5 and after that, the problem in the program is solved but still the problem of printing not solved.
That problem of printing was not present before.
Kind Regards,
Hany Metry
Thursday, September 17, 2020 9:03 PM -
Hi
Try this. If all is OK with your system then this will enable you to set the default Printer
' BLANK Form1 Option Strict On Option Explicit On Public Class Form1 Dim PrintSet As New Printing.PrinterSettings Dim WithEvents cb As New ComboBox Dim lab1, lab2 As New Label Declare Function SetDefPrinter Lib "winspool.drv" Alias "SetDefaultPrinterA" (ByVal pszPrinter As String) As Boolean Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load Dim Prs As Printing.PrinterSettings.StringCollection = Printing.PrinterSettings.InstalledPrinters With lab1 .AutoSize = True .Location = New Point(20, 12) .Text = "Set your DEFAULT PRINTER" End With With cb For Each s As String In Prs .Items.Add(s) Next .Width = 250 .Location = New Point(20, lab1.Bottom + 20) .SelectedItem = PrintSet.PrinterName End With With lab2 .AutoSize = True .Location = New Point(20, cb.Bottom + 12) .Text = "Default Printer = " & PrintSet.PrinterName End With Controls.AddRange({cb, lab1, lab2}) End Sub Private Sub CB_SelectedIndexChanged(sender As Object, e As EventArgs) Handles cb.SelectedIndexChanged Dim s As String = cb.SelectedItem.ToString SetDefPrinter(cb.SelectedItem.ToString) PrintSet.PrinterName = s lab2.Text = "Default Printer = " & PrintSet.PrinterName End Sub End Class
Regards Les, Livingston, Scotland
Thursday, September 17, 2020 9:40 PM -
Hi Xingyu
I want to select printer during printing also, I think there is no OK button for print preview dialog
Friend Sub PrintPage1ToolStripMenuItem_Click(sender As Object, e As EventArgs) Handles PrintPage1ToolStripMenuItem.Click
PrintDocument1.DefaultPageSettings.Landscape = False
AddHandler PrintDocument1.PrintPage, AddressOf PDoc1_PrintPage
If PrintPreviewDialog1.ShowDialog = DialogResult.OK Then
If PrintDialog1.ShowDialog = DialogResult.OK Then
PrintDocument1.Print()
End If
End If
End SubBut even I wrote the code above but I think there is no OK button for print preview.
I have not a default printer
Kind Regards,
Hany Metry
Hi All,
The above problem can be solved easy by make separate tool strip menu item for print preview as well as separate tool strip menu item for print setup. In addition separate tool strip menu item for printing.
e.HasMorePages = True
I guess that the above code is the page break code, if the number of lines per page exceed the maximum limit.
I can write the above code which mean start new page.
Kind Regards,
Hany Metry
Thursday, September 17, 2020 9:43 PM -
Hi
Try this. If all is OK with your system then this will enable you to set the default Printer
' BLANK Form1 Option Strict On Option Explicit On Public Class Form1 Dim PrintSet As New Printing.PrinterSettings Dim WithEvents cb As New ComboBox Dim lab1, lab2 As New Label Declare Function SetDefPrinter Lib "winspool.drv" Alias "SetDefaultPrinterA" (ByVal pszPrinter As String) As Boolean Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load Dim Prs As Printing.PrinterSettings.StringCollection = Printing.PrinterSettings.InstalledPrinters With lab1 .AutoSize = True .Location = New Point(20, 12) .Text = "Set your DEFAULT PRINTER" End With With cb For Each s As String In Prs .Items.Add(s) Next .Width = 250 .Location = New Point(20, lab1.Bottom + 20) .SelectedItem = PrintSet.PrinterName End With With lab2 .AutoSize = True .Location = New Point(20, cb.Bottom + 12) .Text = "Default Printer = " & PrintSet.PrinterName End With Controls.AddRange({cb, lab1, lab2}) End Sub Private Sub CB_SelectedIndexChanged(sender As Object, e As EventArgs) Handles cb.SelectedIndexChanged Dim s As String = cb.SelectedItem.ToString SetDefPrinter(cb.SelectedItem.ToString) PrintSet.PrinterName = s lab2.Text = "Default Printer = " & PrintSet.PrinterName End Sub End Class
Regards Les, Livingston, Scotland
Hi Leshay,
I added the above code and get same error.
Kind Regards,
Hany Metry
Friday, September 18, 2020 6:04 AM -
Hi All,
I guess that some file in window 7 is damaged.
Is any one can advice me, how to repair window 7.
Kind Regards,
Hany Metry
Friday, September 18, 2020 6:36 AM -
Hi,
See this video,
https://www.youtube.com/watch?v=wR4bIPpP5lA
I cant change the wrong default printer.
Kind Regards,
Hany
Hany Metry
Friday, September 18, 2020 7:44 AM -
Hi All,
When I want to modify the printer, I cant and I got the following massage as per the attached photo.
Hany Metry
Friday, September 18, 2020 7:54 AM -
Hi
After you get your computer fixed, come back here and continue with the VB questions.
Regards Les, Livingston, Scotland
Friday, September 18, 2020 11:41 AM -
Hi All,
The computer problem is fixed that before changing the printer type (from regedit), you must right click to the mouse and allow to modify.
Kind Regards,
Hany Sabry
Hany Metry
Friday, September 18, 2020 11:53 AM -
Hi All,
I tried the below code but the first page is printed many time without printing the second page.
For popo = 1 To 2 If popo = 1 Then e.Graphics.DrawString(" Page 1", prFont, Brush2, margins.Left, 1100) e.HasMorePages = True Exit Sub ElseIf popo = 2 Then e.HasMorePages = False e.Graphics.DrawString(" Page 2", prFont, Brush2, margins.Left, 1100) Exit Sub End If Next
I don't know my mistake,
Can anyone advice.
Kind Regards,
Hany Metry
Friday, September 18, 2020 3:23 PM -
Hi
You are setting popo to value = 1 (first value from loop), then Exit Sub with morepages = true which then starts the loop over again with value = 1 and never ends You are setting popo to value = 1 (first value from loop), then Exit Sub with morepages = true which then starts the loop over again with value = 1 and never ends You are setting popo to value = 1 (first value from loop), then Exit Sub with morepages = true which then starts the loop over again with value = 1 and never ends You are setting popo to value = 1 (first value from loop), then Exit Sub with morepages = true which then starts the loop over again with value = 1 and never ends You are setting popo to value = 1 (first value from loop), then Exit Sub with morepages = true which then starts the loop over again with value = 1 and never ends You are setting popo to value = 1 (first value from loop), then Exit Sub with morepages = true which then starts the loop over again with value = 1 and never ends
Regards Les, Livingston, Scotland
Friday, September 18, 2020 3:28 PM -
Hi All,
Also the below code print first page many time.
If popo = 0 Then e.Graphics.DrawString(" Page 1", prFont, Brush2, margins.Left, 1100) e.HasMorePages = True popo = popo + 1 Exit Sub ElseIf popo = 1 Then e.HasMorePages = False e.Graphics.DrawString(" Page 2", prFont, Brush2, margins.Left, 1100) End If
Please advice.
Kind Regards,
Hany Metry
Friday, September 18, 2020 3:48 PM -
Hi
Same answer.
You really need to learn to step through your code ...................
Regards Les, Livingston, Scotland
Friday, September 18, 2020 3:51 PM -
Hi All,
The below code print the two pages in one page, can any one advice.
If popo = 1 Then e.Graphics.DrawString(" Page 1", prFont, Brush2, margins.Left, 1100) e.HasMorePages = True popo += 1 Exit Sub ElseIf popo = 2 Then e.Graphics.DrawString(" Page 2", prFont, Brush2, margins.Left, 1100) e.HasMorePages = False End If
- Edited by Morgan Mosa Saturday, September 19, 2020 11:27 AM adjust
Saturday, September 19, 2020 5:06 AM -
Hi All
Even the below code print the two pages in one page, Please advice.
Friend Sub PDoc1_PrintPage(ByVal sender As Object, ByVal e As System.Drawing.Printing.PrintPageEventArgs) Handles PrintDocument1.PrintPage e.Graphics.DrawString(" Page 1", prFont, Brush2, margins.Left, 1100) e.HasMorePages = True popo += 1 Exit Sub ElseIf popo = 2 Then e.Graphics.DrawString(" Page 2", prFont, Brush2, margins.Left, 1100) popo += 1 e.HasMorePages = True Exit Sub ElseIf popo > 2 Then e.HasMorePages = False End If End Sub
Hany Metry
- Edited by Morgan Mosa Saturday, September 19, 2020 11:28 AM
Saturday, September 19, 2020 6:10 AM -
Hi
Here is my last attempt. Here is some code, almost exactly as the MS example on MS Docs. This version loads a long text file and prints it to fit printer page/margins. and shows the method used to determine if it HasMorePages. You would need to adjust the path I used to point instead to a long text file on your system.
This example just goes as far as Printing to a Preview.
' STAND ALONE PRINTING EXAMPLE ' NO CONTROLS NEEDED IN DESIGNER ' AMMEND PATH IN "stringToPrint" VARIABLE ' TO A LONG TEXT FILE ON YOUR SYSTEM Option Strict On Option Explicit On Imports System.Drawing.Printing Public Class Form1 Dim WithEvents PDoc As New PrintDocument Dim pPrev As New PrintPreviewDialog Dim stringToPrint As String = IO.File.ReadAllText("C:\Users\lesha\Desktop\TESTTEXTDOC.txt") Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load pPrev.Document = PDoc pPrev.ShowDialog() End Sub Private Sub PrintDocument1_PrintPage(sender As Object, e As PrintPageEventArgs) Handles PDoc.PrintPage ' ******* FROM MS DOCS ******* Dim charactersOnPage As Integer = 0 Dim linesPerPage As Integer = 0 ' Sets the value of charactersOnPage to the number of characters ' of stringToPrint that will fit within the bounds of the page. e.Graphics.MeasureString(stringToPrint, Me.Font, e.MarginBounds.Size, StringFormat.GenericTypographic, charactersOnPage, linesPerPage) ' Draws the string within the bounds of the page e.Graphics.DrawString(stringToPrint, Me.Font, Brushes.Black, e.MarginBounds, StringFormat.GenericTypographic) ' Remove the portion of the string that has been printed. stringToPrint = stringToPrint.Substring(charactersOnPage) ' ********************************** ' ****** ATTENTION HANY METRY ****** ' ********************************** ' Check to see if more pages are to be printed. e.HasMorePages = stringToPrint.Length > 0 ' ********************************** ' ****** ATTENTION HANY METRY ****** ' ********************************** End Sub End Class
Regards Les, Livingston, Scotland
- Edited by leshay Saturday, September 19, 2020 12:23 PM
Saturday, September 19, 2020 12:22 PM -
Hi leshay,
I need to know my mistake in my code.
Kind Regards,
Hany Metry
Saturday, September 19, 2020 12:30 PM -
Hi leshay,
I need to know my mistake in my code.
Kind Regards,
Hany Metry
Hi
OK, where to start............
Where is popo defined - what is its initial value.
Where is the For statement which is required to start a For....Next loop.
What are you expecting the emboldened items to do in the snippet below?
e.Graphics.DrawString(" Page 2", prFont, Brush2,
margins.Left, 1100)
Also, just remember that you may not need to explicitly set the HasMorePages to False - it is False by default and unless you change it to true, then all you need to do is exit the sub and no more calls will be made.
Adain, LEARN TO STEP THROUGH YOUR CODE! I can also add - ONLY POST CODE THAT WILL COMPILE!Regards Les, Livingston, Scotland
Saturday, September 19, 2020 1:18 PM -
Hi leshay,
I need to know my mistake in my code.
Kind Regards,
Hany Metry
Hi
OK, where to start............
Where is popo defined - what is its initial value.
Where is the For statement which is required to start a For....Next loop.
What are you expecting the emboldened items to do in the snippet below?
e.Graphics.DrawString(" Page 2", prFont, Brush2,
margins.Left, 1100)
Also, just remember that you may not need to explicitly set the HasMorePages to False - it is False by default and unless you change it to true, then all you need to do is exit the sub and no more calls will be made.
Adain, LEARN TO STEP THROUGH YOUR CODE! I can also add - ONLY POST CODE THAT WILL COMPILE!
Regards Les, Livingston, Scotland
Pope is defined as Friend in the module by value equal 1
Also pope is set equal 1 again in the form load
I have no loop, there is no For Next (that was my old code and now my code as below.
Friend Sub PDoc1_PrintPage(ByVal sender As Object, ByVal e As System.Drawing.Printing.PrintPageEventArgs) Handles PrintDocument1.PrintPage
If popo = 1 Then e.Graphics.DrawString(" Page 1", prFont, Brush2, margins.Left, 1100) e.HasMorePages = True popo += 1 Exit Sub ElseIf popo = 2 Then e.Graphics.DrawString(" Page 2", prFont, Brush2, margins.Left, 1100) e.HasMorePages = False End If
end sub
There are many lines which included in page 1 (Popo = 1) while I write only one line which
e.Graphics.DrawString(" Page 1", prFont, Brush2, margins.Left, 1100)
and ditto for second page (popo = 2)
Friend Sub PrintPage1ToolStripMenuItem_Click(sender As Object, e As EventArgs) Handles PrintPage1ToolStripMenuItem.Click PrintDocument1.DefaultPageSettings.Landscape = False AddHandler PrintDocument1.PrintPage, AddressOf PDoc1_PrintPage 'If PrintPreviewDialog1.ShowDialog = DialogResult.OK Then ' If PrintDialog1.ShowDialog = DialogResult.OK Then ' PrintDocument1.Print() ' End If 'End If 'PrintDocument1.PrintToPrinter(1, True, 1, 1) popo = 1 PrintDocument1.Print() End Sub
Kind Regards,
Hany Metry
- Edited by Morgan Mosa Saturday, September 19, 2020 2:05 PM
Saturday, September 19, 2020 1:59 PM -
If popo = 1 Then e.Graphics.DrawString(" Page 1", prFont, Brush2, margins.Left, 1100) e.HasMorePages = True popo += 1 Exit Sub ElseIf popo = 2 Then e.Graphics.DrawString(" Page 2", prFont, Brush2, margins.Left, 1100) e.HasMorePages = False End If
There are many lines which included in page 1 (Popo = 1) while I write only one line which
e.Graphics.DrawString(" Page 1", prFont, Brush2, margins.Left, 1100)
and ditto for second page (popo = 2)
Kind Regards,
Hany Metry
Hi
Have you set a breakpoint and single stepped through the code? If so, were the variable values as expected?
BTW: you DID post a code block without a starting For in a For...Next loop.
Regards Les, Livingston, Scotland
- Edited by leshay Saturday, September 19, 2020 2:08 PM
Saturday, September 19, 2020 2:07 PM -
If popo = 1 Then e.Graphics.DrawString(" Page 1", prFont, Brush2, margins.Left, 1100) e.HasMorePages = True popo += 1 Exit Sub ElseIf popo = 2 Then e.Graphics.DrawString(" Page 2", prFont, Brush2, margins.Left, 1100) e.HasMorePages = False End If
There are many lines which included in page 1 (Popo = 1) while I write only one line which
e.Graphics.DrawString(" Page 1", prFont, Brush2, margins.Left, 1100)
and ditto for second page (popo = 2)
Kind Regards,
Hany Metry
Hi
Have you set a breakpoint and single stepped through the code? If so, were the variable values as expected?
BTW: you DID post a code block without a starting For in a For...Next loop.
Regards Les, Livingston, Scotland
Hi Leshay
I did a break point and all the value as expected but the printer print only one page and the two pages are printed in one page.
Hany Metry
Saturday, September 19, 2020 2:20 PM -
Hi
Which line did you set the breakpoint on?
Regards Les, Livingston, Scotland
Saturday, September 19, 2020 2:23 PM -
Hi,
I did a break points at all if statements which as IF Pope = 1 and else if pope =2 as well as all e.HasMorePages = true.
Kind Regards,
Hany Metry
Saturday, September 19, 2020 2:29 PM -
Hi
So, each time it hit e.HasMorePages, was it correct to be there?
Regards Les, Livingston, Scotland
Saturday, September 19, 2020 2:31 PM -
Hi
So, each time it hit e.HasMorePages, was it correct to be there?
Regards Les, Livingston, Scotland
I add e.HasMorePages=true at evey place at start of code of the page and at end of the code of the page and also same error.
I guess that the code does not print any thing unless end of sub and then read e.hasmorepages = false then print in one page.
How to make the code print first page before end of sub?
Hany Metry
Saturday, September 19, 2020 3:08 PM -
Hi
Did you know that given an infinite number of monkeys and a computer that one of them would write a program that would understand your ramblings, because I sure don't!
Regards Les, Livingston, Scotland
- Edited by leshay Saturday, September 19, 2020 3:49 PM
Saturday, September 19, 2020 3:12 PM -
Hi
Single step is your friend ...............
Regards Les, Livingston, Scotland
Saturday, September 19, 2020 4:39 PM -
Hi,
The below code print the pages at first page and print the second page empty.
Friend Sub PDoc1_PrintPage(ByVal sender As Object, ByVal e As System.Drawing.Printing.PrintPageEventArgs) Handles PrintDocument1.PrintPage If popo = 1 Then e.HasMorePages = True e.Graphics.DrawString(" Page 1", prFont, Brush2, margins.Left, 1100) popo += 1 Exit Sub ElseIf popo = 2 Then e.Graphics.DrawString(" Page 2", prFont, Brush2, margins.Left, 1100) e.HasMorePages = True popo += 1 Exit Sub ElseIf popo > 2 Then e.HasMorePages = False End If End Sub
Please advice the mistake in the above code.
Kind regards,
Hany Sabry
Hany Metry
Well, you did not answer my earlier question:
*
What do you expect the
, margins.Left, 1100)
portion of your code to do?
Regards Les, Livingston, Scotland
Saturday, September 19, 2020 5:20 PM -
Hi
Single step is your friend ...............
Regards Les, Livingston, Scotland
Hany Metry
Saturday, September 19, 2020 5:57 PM -
Hi
Single step is your friend ...............
Regards Les, Livingston, Scotland
Please, read the subject of the question and if you have an answer, then reply and if you have not dont reply.
Hany Metry
Regards Les, Livingston, Scotland
Saturday, September 19, 2020 6:12 PM -
Please, read the subject of the question and if you have an answer, then reply and if you have not dont reply.
Hany Metry
Hi, Hany Metry. You spent 3-4 days just spamming this forum/thread with inane updates on your shopping experience for a new printer. That was 3-4 days immediately following the answer that I and others gave you.
I'm thinking of a word. It starts with tro and ends with ll. I mean it's either that or the one that starts with fu and ends with cktard. It's just plain one or the other.
Before you can learn anything new you have to learn that there's stuff you don't know.
Saturday, September 19, 2020 9:53 PM -
Hi Hany Metry,
Maybe you need the following code to print multiple pages.
Here's the code you can refer to.
Private strToPrint As String Private lineCount As Integer Dim charactersOnPage As Integer = 0 Dim linesPerPage As Integer = 0 Dim popo As Integer = 1 Dim boolValue As Boolean = True Dim num As Integer Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click OpenFileDialog1.Title = "Open Text File" OpenFileDialog1.Filter = "txt files (*.txt)|*.txt|All files (*.*)|*.*" OpenFileDialog1.InitialDirectory = Environment.SpecialFolder.MyDocuments If OpenFileDialog1.ShowDialog() = System.Windows.Forms.DialogResult.OK Then ' count the number of lines within a text file. lineCount = File.ReadLines(OpenFileDialog1.FileName).Count() End If End Sub Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click PrintDocument1.DocumentName = TextBox1.Text Dim stream As New FileStream(PrintDocument1.DocumentName, FileMode.Open) Try Dim reader As New StreamReader(stream) Try strToPrint = reader.ReadToEnd() Finally reader.Dispose() End Try Finally stream.Dispose() End Try Dim pd As PrintDialog = New PrintDialog() pd.PrinterSettings.PrinterName = "Microsoft Print to PDF" If pd.ShowDialog() = System.Windows.Forms.DialogResult.OK Then PrintDocument1.Print() End If End Sub Private Sub PrintDocument1_PrintPage(sender As Object, e As Printing.PrintPageEventArgs) Handles PrintDocument1.PrintPage If boolValue Then e.Graphics.MeasureString(strToPrint, Me.Font, e.MarginBounds.Size, StringFormat.GenericTypographic, charactersOnPage, linesPerPage) ' Determine how many pages to print. num = If(lineCount > linesPerPage, lineCount / linesPerPage + 1, 1) End If If popo < num Then e.Graphics.DrawString(strToPrint, Me.Font, Brushes.Black, e.MarginBounds, StringFormat.GenericTypographic) strToPrint = strToPrint.Substring(charactersOnPage) e.HasMorePages = True boolValue = False popo += 1 Else e.Graphics.DrawString(strToPrint, Me.Font, Brushes.Black, e.MarginBounds, StringFormat.GenericTypographic) boolValue = True popo = 1 End If End Sub
Hope it could be helpful.
Best Regards,
Xingyu Zhao
MSDN Community Support
Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.Monday, September 21, 2020 7:33 AM -
Hi All,
That is my question
https://social.msdn.microsoft.com/Forums/vstudio/en-US/100d9b5f-ca25-4cbe-b59b-5f1b600e2896/the-code-print-the-two-pages-in-the-first-page-how-to-make-it-print-two-separate-pages?forum=vbgeneral
You can answer here or at other thread.
Kind Regards,
Hany Metry
Tuesday, September 22, 2020 12:43 PM -
Hi Hany Metry,
I have given my reply in the new thread, please let me know if it works.
Best Regards,
Xingyu Zhao
MSDN Community Support
Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.Wednesday, September 23, 2020 1:31 AM