Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How to print RichTextbox with colored parts

Status
Not open for further replies.

GPerk

Programmer
Jul 6, 2002
161
0
0
US
I am using the following code to color certain parts of the text in a RichTexbox.

Private Sub SetRed()
Dim I, L As Integer, S As String
L = 106
For I = 2 To nStk + 2
With RTB
.SelectionStart = I * L + 14
.SelectionLength = 16
S = .Text.Substring(.SelectionStart, 8)
If S.Contains("-") Then
.SelectionColor = Color.Red
Else
.SelectionColor = Color.Green
End If
End With
Next I
End Sub

This works OK when the data is shown in the RichTexbox.
But when I send the RichTextbox.Text to the printer, all of it prints in black.

How can I get it to print the colored text the same as it appears in the RichTextbox?
 
While doing more searchs on the net I found a few solutions, most were very long and dense.

But the simple way of getting it printed is to save the rtf
to a file, thusly:

Private Sub btnSaveRTF_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSaveRTF.Click
RTB.SaveFile("MyRTB.rtf")
End Sub

Then you call call it up in Wordpad, look at it and/or print it.

But if you want to do it from the original program,
add this:

Private Sub btnPrintRtfFile_Click(ByVal sender As
System.Object, ByVal e As System.EventArgs) Handles btnPrintRtfFile.Click
Dim PSI As New ProcessStartInfo
PSI.UseShellExecute = True
PSI.Verb = "print"
PSI.WindowStyle = ProcessWindowStyle.Hidden
PSI.FileName = "MyRTB.rtf"
Process.Start(PSI)
End Sub

Thus, in just 2 clicks the user can save and print the RichTextbox data.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top