473,403 Members | 2,222 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,403 software developers and data experts.

printing from a rich text box

can anybody help me print from a rich text box? i tried the way they showed
on the MSDN web page, but it did not work. I am using VB.net 2003...any help
would be appreciated
Nov 21 '05 #1
4 13967
hi this should help,

make a new class, call it TextPrint and then copy paste this:

'to actualy print something use this:
'Dim x As New TextPrint("my text")
'x.Print()

Imports System.Windows.Forms
Imports System.Drawing
Public Class TextPrint
Inherits Printing.PrintDocument
Private fntPrintFont As Font
Private strText As String
Public Sub New(ByVal Text As String)
MyBase.New()
strText = Text
End Sub
Public Property Text() As String
Get
Return strText
End Get
Set(ByVal Value As String)
strText = Value
End Set
End Property
Protected Overrides Sub OnBeginPrint(ByVal ev _
As Printing.PrintEventArgs)

MyBase.OnBeginPrint(ev)
If fntPrintFont Is Nothing Then
fntPrintFont = New Font("Times New Roman", 12)
End If
End Sub
Public Property Font() As Font
Get
Return fntPrintFont
End Get
Set(ByVal Value As Font)
fntPrintFont = Value
End Set
End Property
Protected Overrides Sub OnPrintPage(ByVal ev _
As Printing.PrintPageEventArgs)

MyBase.OnPrintPage(ev)
Static intCurrentChar As Integer
Dim intPrintAreaHeight, intPrintAreaWidth, _
intMarginLeft, intMarginTop As Integer
With MyBase.DefaultPageSettings
intPrintAreaHeight = .PaperSize.Height - _
.Margins.Top - .Margins.Bottom
intPrintAreaWidth = .PaperSize.Width - _
.Margins.Left - .Margins.Right
intMarginLeft = .Margins.Left 'X
intMarginTop = .Margins.Top 'Y
End With
If MyBase.DefaultPageSettings.Landscape Then
Dim intTemp As Integer
intTemp = intPrintAreaHeight
intPrintAreaHeight = intPrintAreaWidth
intPrintAreaWidth = intTemp
End If
Dim intLineCount As Int32 = _
CInt(intPrintAreaHeight / Font.Height)
Dim rectPrintingArea As New RectangleF(intMarginLeft, _
intMarginTop, intPrintAreaWidth, intPrintAreaHeight)
Dim objSF As New StringFormat(StringFormatFlags.LineLimit)
Dim intLinesFilled, intCharsFitted As Int32
ev.Graphics.MeasureString(Mid(strText, _
UpgradeZeros(intCurrentChar)), Font, _
New SizeF(intPrintAreaWidth, _
intPrintAreaHeight), objSF, _
intCharsFitted, intLinesFilled)
ev.Graphics.DrawString(Mid(strText, _
UpgradeZeros(intCurrentChar)), Font, _
Brushes.Black, rectPrintingArea, objSF)
intCurrentChar += intCharsFitted
If intCurrentChar < strText.Length Then
ev.HasMorePages = True
Else
ev.HasMorePages = False
intCurrentChar = 0
End If
End Sub
Public Function UpgradeZeros(ByVal Input As Integer) As Integer
If Input = 0 Then
Return 1
Else
Return Input
End If
End Function
End Class
hth Greetz Peter
--
Programming today is a race between software engineers striving to build
bigger and better idiot-proof programs, and the Universe trying to produce
bigger and better idiots. So far, the Universe is winning.
"iwdu15" <iw****@discussions.microsoft.com> schreef in bericht
news:02**********************************@microsof t.com...
can anybody help me print from a rich text box? i tried the way they showed on the MSDN web page, but it did not work. I am using VB.net 2003...any help would be appreciated

Nov 21 '05 #2
ng
Peter,

I found this very helpful, and interesting! But I have another problem
- I can't figure out how to send formatted text from a richtextbox.rtf
to the printer without getting the formatting codes printed. I remember
when sending columnar data to a printer was so easy! And Microsoft
documentation seems to be silent on this. Go figure!

Thanks for whatever you can offer.

T

Peter Proost wrote:
hi this should help,

make a new class, call it TextPrint and then copy paste this:

'to actualy print something use this:
'Dim x As New TextPrint("my text")
'x.Print()

Imports System.Windows.Forms
Imports System.Drawing
Public Class TextPrint
Inherits Printing.PrintDocument
Private fntPrintFont As Font
Private strText As String
Public Sub New(ByVal Text As String)
MyBase.New()
strText = Text
End Sub
Public Property Text() As String
Get
Return strText
End Get
Set(ByVal Value As String)
strText = Value
End Set
End Property
Protected Overrides Sub OnBeginPrint(ByVal ev _
As Printing.PrintEventArgs)

MyBase.OnBeginPrint(ev)
If fntPrintFont Is Nothing Then
fntPrintFont = New Font("Times New Roman", 12)
End If
End Sub
Public Property Font() As Font
Get
Return fntPrintFont
End Get
Set(ByVal Value As Font)
fntPrintFont = Value
End Set
End Property
Protected Overrides Sub OnPrintPage(ByVal ev _
As Printing.PrintPageEventArgs)

MyBase.OnPrintPage(ev)
Static intCurrentChar As Integer
Dim intPrintAreaHeight, intPrintAreaWidth, _
intMarginLeft, intMarginTop As Integer
With MyBase.DefaultPageSettings
intPrintAreaHeight = .PaperSize.Height - _
.Margins.Top - .Margins.Bottom
intPrintAreaWidth = .PaperSize.Width - _
.Margins.Left - .Margins.Right
intMarginLeft = .Margins.Left 'X
intMarginTop = .Margins.Top 'Y
End With
If MyBase.DefaultPageSettings.Landscape Then
Dim intTemp As Integer
intTemp = intPrintAreaHeight
intPrintAreaHeight = intPrintAreaWidth
intPrintAreaWidth = intTemp
End If
Dim intLineCount As Int32 = _
CInt(intPrintAreaHeight / Font.Height)
Dim rectPrintingArea As New RectangleF(intMarginLeft, _
intMarginTop, intPrintAreaWidth, intPrintAreaHeight)
Dim objSF As New StringFormat(StringFormatFlags.LineLimit)
Dim intLinesFilled, intCharsFitted As Int32
ev.Graphics.MeasureString(Mid(strText, _
UpgradeZeros(intCurrentChar)), Font, _
New SizeF(intPrintAreaWidth, _
intPrintAreaHeight), objSF, _
intCharsFitted, intLinesFilled)
ev.Graphics.DrawString(Mid(strText, _
UpgradeZeros(intCurrentChar)), Font, _
Brushes.Black, rectPrintingArea, objSF)
intCurrentChar += intCharsFitted
If intCurrentChar < strText.Length Then
ev.HasMorePages = True
Else
ev.HasMorePages = False
intCurrentChar = 0
End If
End Sub
Public Function UpgradeZeros(ByVal Input As Integer) As Integer
If Input = 0 Then
Return 1
Else
Return Input
End If
End Function
End Class
hth Greetz Peter
--
Programming today is a race between software engineers striving to build
bigger and better idiot-proof programs, and the Universe trying to produce
bigger and better idiots. So far, the Universe is winning.
"iwdu15" <iw****@discussions.microsoft.com> schreef in bericht
news:02**********************************@microso ft.com...

can anybody help me print from a rich text box? i tried the way they

showed

on the MSDN web page, but it did not work. I am using VB.net 2003...any

help

would be appreciated


Nov 21 '05 #3
Hi these links should help,

the first one has a nice working example, but it's C sharp, the second one does the same thing in VB, but no example

http://www.codeguru.com/Csharp/Cshar...icle.php/c4781

http://msdn.microsoft.com/library/de...ichTextBox.asp

hth Greetz Peter


--
Programming today is a race between software engineers striving to build bigger and better idiot-proof programs, and the Universe trying to produce bigger and better idiots. So far, the Universe is winning.

"ng" <t_*****@yahoo.com> schreef in bericht news:6l******************@bignews5.bellsouth.net.. .
Peter,

I found this very helpful, and interesting! But I have another problem - I can't figure out how to send formatted text from a richtextbox.rtf to the printer without getting the formatting codes printed. I remember when sending columnar data to a printer was so easy! And Microsoft documentation seems to be silent on this. Go figure!

Thanks for whatever you can offer.

T

Peter Proost wrote:

hi this should help,

make a new class, call it TextPrint and then copy paste this:

'to actualy print something use this:
'Dim x As New TextPrint("my text")
'x.Print()

Imports System.Windows.Forms
Imports System.Drawing
Public Class TextPrint
Inherits Printing.PrintDocument
Private fntPrintFont As Font
Private strText As String
Public Sub New(ByVal Text As String)
MyBase.New()
strText = Text
End Sub
Public Property Text() As String
Get
Return strText
End Get
Set(ByVal Value As String)
strText = Value
End Set
End Property
Protected Overrides Sub OnBeginPrint(ByVal ev _
As Printing.PrintEventArgs)

MyBase.OnBeginPrint(ev)
If fntPrintFont Is Nothing Then
fntPrintFont = New Font("Times New Roman", 12)
End If
End Sub
Public Property Font() As Font
Get
Return fntPrintFont
End Get
Set(ByVal Value As Font)
fntPrintFont = Value
End Set
End Property
Protected Overrides Sub OnPrintPage(ByVal ev _
As Printing.PrintPageEventArgs)

MyBase.OnPrintPage(ev)
Static intCurrentChar As Integer
Dim intPrintAreaHeight, intPrintAreaWidth, _
intMarginLeft, intMarginTop As Integer
With MyBase.DefaultPageSettings
intPrintAreaHeight = .PaperSize.Height - _
.Margins.Top - .Margins.Bottom
intPrintAreaWidth = .PaperSize.Width - _
.Margins.Left - .Margins.Right
intMarginLeft = .Margins.Left 'X
intMarginTop = .Margins.Top 'Y
End With
If MyBase.DefaultPageSettings.Landscape Then
Dim intTemp As Integer
intTemp = intPrintAreaHeight
intPrintAreaHeight = intPrintAreaWidth
intPrintAreaWidth = intTemp
End If
Dim intLineCount As Int32 = _
CInt(intPrintAreaHeight / Font.Height)
Dim rectPrintingArea As New RectangleF(intMarginLeft, _
intMarginTop, intPrintAreaWidth, intPrintAreaHeight)
Dim objSF As New StringFormat(StringFormatFlags.LineLimit)
Dim intLinesFilled, intCharsFitted As Int32
ev.Graphics.MeasureString(Mid(strText, _
UpgradeZeros(intCurrentChar)), Font, _
New SizeF(intPrintAreaWidth, _
intPrintAreaHeight), objSF, _
intCharsFitted, intLinesFilled)
ev.Graphics.DrawString(Mid(strText, _
UpgradeZeros(intCurrentChar)), Font, _
Brushes.Black, rectPrintingArea, objSF)
intCurrentChar += intCharsFitted
If intCurrentChar < strText.Length Then
ev.HasMorePages = True
Else
ev.HasMorePages = False
intCurrentChar = 0
End If
End Sub
Public Function UpgradeZeros(ByVal Input As Integer) As Integer
If Input = 0 Then
Return 1
Else
Return Input
End If
End Function
End Class
hth Greetz Peter
--
Programming today is a race between software engineers striving to build
bigger and better idiot-proof programs, and the Universe trying to produce
bigger and better idiots. So far, the Universe is winning.
"iwdu15" <iw****@discussions.microsoft.com> schreef in bericht
news:02**********************************@microsof t.com...
can anybody help me print from a rich text box? i tried the way they
showed
on the MSDN web page, but it did not work. I am using VB.net 2003...any
help
would be appreciated




Nov 21 '05 #4
ng
Thank you, Peter. These are both very instructional. I'm learning C#
and VB.Net.

T
Peter Proost wrote:
Hi these links should help,

the first one has a nice working example, but it's C sharp, the second
one does the same thing in VB, but no example

http://www.codeguru.com/Csharp/Cshar...icle.php/c4781

http://msdn.microsoft.com/library/de...ichTextBox.asp

hth Greetz Peter


--
Programming today is a race between software engineers striving to
build bigger and better idiot-proof programs, and the Universe trying
to produce bigger and better idiots. So far, the Universe is winning.

"ng" <t_*****@yahoo.com <mailto:t_*****@yahoo.com>> schreef in
bericht news:6l******************@bignews5.bellsouth.net.. .
Peter,

I found this very helpful, and interesting! But I have another
problem - I can't figure out how to send formatted text from a
richtextbox.rtf to the printer without getting the formatting
codes printed. I remember when sending columnar data to a printer
was so easy! And Microsoft documentation seems to be silent on
this. Go figure!

Thanks for whatever you can offer.

T

Peter Proost wrote:
hi this should help,

make a new class, call it TextPrint and then copy paste this:

'to actualy print something use this:
'Dim x As New TextPrint("my text")
'x.Print()

Imports System.Windows.Forms
Imports System.Drawing
Public Class TextPrint
Inherits Printing.PrintDocument
Private fntPrintFont As Font
Private strText As String
Public Sub New(ByVal Text As String)
MyBase.New()
strText = Text
End Sub
Public Property Text() As String
Get
Return strText
End Get
Set(ByVal Value As String)
strText = Value
End Set
End Property
Protected Overrides Sub OnBeginPrint(ByVal ev _
As Printing.PrintEventArgs)

MyBase.OnBeginPrint(ev)
If fntPrintFont Is Nothing Then
fntPrintFont = New Font("Times New Roman", 12)
End If
End Sub
Public Property Font() As Font
Get
Return fntPrintFont
End Get
Set(ByVal Value As Font)
fntPrintFont = Value
End Set
End Property
Protected Overrides Sub OnPrintPage(ByVal ev _
As Printing.PrintPageEventArgs)

MyBase.OnPrintPage(ev)
Static intCurrentChar As Integer
Dim intPrintAreaHeight, intPrintAreaWidth, _
intMarginLeft, intMarginTop As Integer
With MyBase.DefaultPageSettings
intPrintAreaHeight = .PaperSize.Height - _
.Margins.Top - .Margins.Bottom
intPrintAreaWidth = .PaperSize.Width - _
.Margins.Left - .Margins.Right
intMarginLeft = .Margins.Left 'X
intMarginTop = .Margins.Top 'Y
End With
If MyBase.DefaultPageSettings.Landscape Then
Dim intTemp As Integer
intTemp = intPrintAreaHeight
intPrintAreaHeight = intPrintAreaWidth
intPrintAreaWidth = intTemp
End If
Dim intLineCount As Int32 = _
CInt(intPrintAreaHeight / Font.Height)
Dim rectPrintingArea As New RectangleF(intMarginLeft, _
intMarginTop, intPrintAreaWidth, intPrintAreaHeight)
Dim objSF As New StringFormat(StringFormatFlags.LineLimit)
Dim intLinesFilled, intCharsFitted As Int32
ev.Graphics.MeasureString(Mid(strText, _
UpgradeZeros(intCurrentChar)), Font, _
New SizeF(intPrintAreaWidth, _
intPrintAreaHeight), objSF, _
intCharsFitted, intLinesFilled)
ev.Graphics.DrawString(Mid(strText, _
UpgradeZeros(intCurrentChar)), Font, _
Brushes.Black, rectPrintingArea, objSF)
intCurrentChar += intCharsFitted
If intCurrentChar < strText.Length Then
ev.HasMorePages = True
Else
ev.HasMorePages = False
intCurrentChar = 0
End If
End Sub
Public Function UpgradeZeros(ByVal Input As Integer) As Integer
If Input = 0 Then
Return 1
Else
Return Input
End If
End Function
End Class
hth Greetz Peter
--
Programming today is a race between software engineers striving to build
bigger and better idiot-proof programs, and the Universe trying to produce
bigger and better idiots. So far, the Universe is winning.
"iwdu15" <iw****@discussions.microsoft.com> schreef in bericht
news:02**********************************@micros oft.com...

can anybody help me print from a rich text box? i tried the way they

showed

on the MSDN web page, but it did not work. I am using VB.net 2003...any

help

would be appreciated


Nov 21 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Alfredo Agosti | last post by:
Hi folks, I have an Access 2000 db with a memo field. Into the memo field I put text with bold attributes, URL etc etc What I need to to is converting the rich text contained into the memo...
1
by: PC User | last post by:
I found this Rich Text Editor and I've been trying to recreate it in my own application. I've had trouble with the COMCTL.ImageListCtrl and the COMCTL.Toolbar to recreate the toolbar. And I've...
1
by: Peter | last post by:
I have written a small terminal app that reads and writes to the serial port. In particular as the data is read from the serial port it is appended to the rich text box. The problem I am...
4
by: ng | last post by:
How in the world can I print a richtextbox rtf without including the special characters like crlf and the font definition and such? Microsoft documentation is silent on this. Thanks so much! T
3
by: Stanley | last post by:
Hi all! I have trouble with printing rich text box that has multi fonts format. I have found some printing example in vb.net on web, but they can only print one font format!! I'm new to printing in...
1
by: tomi.trescak | last post by:
Hi I have a problem with storing rich text in MySQL. I store rich text in MySQL (in column with type "text") which i get from Rich Textbox control. When i do reverse processing by trying to...
0
by: Howard | last post by:
In VB6 this is easy, One method of the RTB does it. I use this to teach first year students how to make a simple word processor Now I have to migrate to vb 2005 express. The RTB here does not...
4
by: Neil | last post by:
Just found out that the Microsoft Rich Textbox does not support full text justification, since it's based on Version 1.0 of the RichEdit Window Class, and full text justification is only available...
16
by: Neil | last post by:
I posted a few days ago that it seems to me that the Access 2007 rich text feature does not support: a) full text justification; b) programmatic manipulation. I was hoping that someone might...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.