Today, I was looking for a method to Print the text that is in a RichTextbox. The printing of the text is no problem but keeping the formatting is a little more troublesome.
The first solutions you find use VB6 to accomplish this (by using an ActiveX control. But that was just not happening in my project.
Then, helped by an article on MSDN, I found the solution, which is actually just a com call and the same as what the others are doing. The code is in our wiki because I don’t want to bore you guys with all the details.
I used that to make an extension method for the richtextbox, which makes printing the contents of it a lot easier. Just do this
vbnet
RichTextBox1.Print()
or
vbnet
RichTextBox1.PrintPreview()
I also made it so that you can add a simple Header and Footer. Of course, you could make simple variations on them.
Here is an example of it at work in my home-made queryanalyser.
And then with this simple little code:
CType(Me.TabControl1.SelectedTab.Controls(0), System.Windows.Forms.RichTextBox).PrintPreview(Me.TabControl1.SelectedTab.Text, "Printed On: " & Now.ToString("dd/MM/yyyy HH:mm"))
It gives this as a result:
<p>
Don’t forget, you can always ask questions about this in our <a href="http://forum.lessthandot.com/viewforum.php?f=39">VB.Net Forum</a>
</p>