I have a textBox that is 4 pages long that I would like to print, but it only prints 1 page...  
Author Message
robertlamour





PostPosted: Visual Basic Express Edition, I have a textBox that is 4 pages long that I would like to print, but it only prints 1 page... Top

I have a textBox that is 4 pages long that I would like to print, but it only prints 1 page...How do I get the print message to print more than one page of a textBox

Private Sub pdPrint_PrintPage(ByVal sender As System.Object, _

ByVal e As System.Drawing.Printing.PrintPageEventArgs) Handles pdPrint.PrintPage

cdColor.ShowDialog()

fdFont.ShowDialog()

txtDocument.Font = fdFont.Font

e.Graphics.DrawString(txtDocument.Text, txtDocument.Font, New SolidBrush(cdColor.Color), 10, 10)

End Sub



Visual Studio Express Editions31  
 
 
nobugz





PostPosted: Visual Basic Express Edition, I have a textBox that is 4 pages long that I would like to print, but it only prints 1 page... Top

You'll need to break-up txtDocument.Text in chunks that will fit on each page, use Graphics.MeasureString to determine how to break it up. Set e.MorePages = True to ask the PrintDocument to call your PrintPage event again for the next page.



 
 
ahmedilyas





PostPosted: Visual Basic Express Edition, I have a textBox that is 4 pages long that I would like to print, but it only prints 1 page... Top

wow, thanks for that NoBugz, dont have experience in the printing section, ideal thing to note down. Cheers

 
 
Tall Dude





PostPosted: Visual Basic Express Edition, I have a textBox that is 4 pages long that I would like to print, but it only prints 1 page... Top

' If you 'cut and paste', remove empty lines the forum posting engine inserts.

' PrintDocument1 added from the toolbox

' Textbox1 added from the toolbox

' Button1/2/3 added from the toolbox

Public Class Form1

Private stringToPrint As String

Private myfont As Font = Me.Font

Private fdfont As New FontDialog

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

'Define the text to be printed

stringToPrint = TextBox1.Text

' Launch the PrintDocument1, PrintPage Event

PrintDocument1.Print()

End Sub

Private Sub PrintDocument1_PrintPage1(ByVal sender As Object, _

ByVal e As System.Drawing.Printing.PrintPageEventArgs) _

Handles PrintDocument1.PrintPage

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, myfont, e.MarginBounds.Size, _

StringFormat.GenericTypographic, charactersOnPage, linesPerPage)

' Draws the string within the bounds of the page

e.Graphics.DrawString(stringToPrint, myfont, Brushes.Black, _

e.MarginBounds, StringFormat.GenericTypographic)

' Remove the portion of the string that has been printed.

stringToPrint = stringToPrint.Substring(charactersOnPage)

' Check to see if more pages are to be printed.

e.HasMorePages = stringToPrint.Length > 0

End Sub

' Add an optional button2 to the form for a print preview

'Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click

' Dim pp As New PrintPreviewDialog

' pp.Document = PrintDocument1

' stringToPrint = TextBox1.Text

' pp.ShowDialog()

'End Sub

' Button3 is a setup button

Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click

If fdfont.ShowDialog = Windows.Forms.DialogResult.OK Then

TextBox1.Font = fdfont.Font

myfont = fdfont.Font

End If

End Sub

End Class



 
 
Neotech





PostPosted: Visual Basic Express Edition, I have a textBox that is 4 pages long that I would like to print, but it only prints 1 page... Top

Hi. While we're here if i had an rtb is there a way to print that using the same method but with formatting etc




Thanks !


 
 
SJWhiteley





PostPosted: Visual Basic Express Edition, I have a textBox that is 4 pages long that I would like to print, but it only prints 1 page... Top

With the Rich Text Box, you will have to do a similar thing (measuring, etc. ) but with a much finer scale. You will need to go through looking at the formatting of each word, or even letter - depends on how you are implementing the rich text box.

Once you've measured it, you can see if it fits on the line or needs to go to the next line. If it goes on the next line, does it go on the next page...that sort of thing. It's a bit more complcated if you have different fonts or font sizes - you may have to keep measuring words until a line is created, so you can see how much space (vertical and horizontal) the 'line' takes up, and plot each word in that line all at one go. Again, you may need to do this character by character.

The process is pretty much the same as what Tall Dude has posted.

Once you've figured out the technique, it's actually not that hard, and doesn't take as long as you might think (spooling all this stuff to the printer takes way longer).

If you want a dirty, lazy way of doing it, you could try creating a temporary rich text file and activating wordpad as a process.



 
 
Neotech





PostPosted: Visual Basic Express Edition, I have a textBox that is 4 pages long that I would like to print, but it only prints 1 page... Top

Im not with the printing..... could someone make a small sln with a rtb which basically prints it all with formatting and a printer dialog Thanks!!!


 
 
Tall Dude





PostPosted: Visual Basic Express Edition, I have a textBox that is 4 pages long that I would like to print, but it only prints 1 page... Top

Download my editor at

http://users.adelphia.net/~gcumbia/printrtfnow.zip

(The server has been slow today BTW)