|
-
Feb 25th, 2003, 08:13 PM
#1
Thread Starter
Junior Member
Print Rich Textbox?
Is there any easy way to print an multiline rich textbox to an printer?
-
Feb 26th, 2003, 08:08 AM
#2
Fanatic Member
Ive just done something similar, and let me tell you
there is no easy way
Add a PrintDocument to your program, also add PrintPreviewDialog so you can view a preview without actually printing it ( saves alot of paper and ink ).
On your print preview button, add this code
VB Code:
PrintPreviewDialog1.Document = PrintDocument1
PrintPreviewDialog1.ShowDialog()
And that will call
VB Code:
Private Sub PrintDocument1_PrintPage(ByVal sender As Object, ByVal e As System.Drawing.Printing.PrintPageEventArgs) Handles PrintDocument1.PrintPage
which is where you do all your printing.
The most basic thing you could do is
VB Code:
Private Sub PrintDocument1_PrintPage(ByVal sender As Object, ByVal e As System.Drawing.Printing.PrintPageEventArgs) Handles PrintDocument1.PrintPage
e.Graphics.DrawString(RichTextBox1.Text, RichTextBox1.Font, Brushes.Black, e.MarginBounds.Left, e.MarginBounds.Top)
But that wont take into account the text fitting on the page. You have to manully determine when your text reaches the end of the paper width and height, to start printing on a new page.
And if your RichTextBox contains formmated text, you got some serious work involved.
Printing is no easy task IMO (Ive spent the last 2 days non stop coding working out a way to print a richtextbox when the contents was just plain text ( all the characters in there was the same font ) and my print function is about 100 lines!
Do a google search for printing in VB.NET, because printing is a big subject
-
Feb 26th, 2003, 08:28 AM
#3
Fanatic Member
Heres a picture of my printing a rich text box
The RichTextBox contained just normal text. I wrote a function to convert it into a layout so you could see the HEX values of each caracter.
With that I had to determine how many characters would fit in the width of the page, if it was too many, I had to reformat the layout to make sure everything fitted.
Once i worked that out I start printing the contents one line at a time, when I reached the bottom of the page, I had to goto a new page, reset any position variables I was using, and continue printing off where I left off.
All that while taking into account, paper size, margin size, landscape or portrait and fotn size.
My print function now is an extremly complicated function and was very hard to do. But after all my problems fitted everything on, I think the headaches have paid off
-
Mar 1st, 2003, 12:22 PM
#4
Fanatic Member
If your still interested, i just came across this which looks like an easy way of doing it
http://msdn.microsoft.com/library/de...ichTextBox.asp
-
Aug 1st, 2003, 10:28 AM
#5
Hyperactive Member
in vb6 it would look like this ........
Code:
'print the document
On Error Resume Next
If ActiveForm Is Nothing Then Exit Sub
With dlgCommonDialog
.DialogTitle = "Print"
.CancelError = True
.Flags = cdlPDReturnDC + cdlPDNoPageNums
If ActiveForm.rtfText.SelLength = 0 Then
.Flags = .Flags + cdlPDAllPages
Else
.Flags = .Flags + cdlPDSelection
End If
.ShowPrinter
Me.ActiveForm.Timer10.Enabled = True
If Err <> MSComDlg.cdlCancel Then
ActiveForm.rtfText.SelPrint .hdc
End If
End With
It is the mark of an instructed mind to rest satisfied with the degree of precision which the nature of the subject admits, and not to seek exactness when only an approximation of the truth is possible.
-Aristotle As quoted in Rapid Development, chapter 8, page 167.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|